File Coverage

blib/lib/Number/Phone/Formatter.pm
Criterion Covered Total %
statement 33 34 97.0
branch 8 8 100.0
condition 11 12 91.6
subroutine 6 6 100.0
pod n/a
total 58 60 96.6


line stmt bran cond sub pod time code
1             package Number::Phone::Formatter;
2              
3 5     5   2367 use strict;
  5         14  
  5         166  
4 5     5   26 use warnings;
  5         20  
  5         215  
5              
6             our $VERSION = '1.0';
7              
8 5     5   70 use Number::Phone::Lib;
  5         38  
  5         80  
9              
10             sub _regex_variable {
11 36     36   100 my ($var, $qr, $subs) = @_;
12 36         81 $subs =~ s/"/\\"/;
13 36         73 $subs = "\"$subs\"";
14 36         228 $var =~ s/$qr/$subs/xee;
  36         2366  
15 36         177 return $var;
16             }
17              
18             sub _maybe_add_country {
19 24     24   74 my ($object, $number, $national) = @_;
20 24 100       87 $number = '+' . $object->country_code() . ' ' . $number unless $national;
21 24         245 return $number;
22             }
23              
24             # this is used by N::P::Formatter::National(lyPreferredIntl), it wants
25             # something that looks very much like a N::P::StubCountry::XX as those
26             # will contain libphonenumber's formatting data. If we're instead passed
27             # a full-fat object, it will first create the equivalent stub.
28             sub _format {
29 24     24   52 my ($class, $object, $national) = @_;
30              
31 24 100       144 if(!$object->isa('Number::Phone::StubCountry')) {
32 3         10 $object = Number::Phone::Lib->new($object->format())
33             }
34              
35 24         70 my $number = $object->{number};
36 24         1559 foreach my $formatter (@{$object->{formatters}}) {
  24         64  
37 152         323 my ($leading_digits, $pattern) = map { $formatter->{$_} } qw(leading_digits pattern);
  304         641  
38 152 100 100     5941 if ((!$leading_digits || $number =~ /^($leading_digits)/x) && $number =~ /^$pattern$/x) {
      100        
39 24         55 my $format;
40 24 100 100     87 if ($national && $formatter->{national_rule}) {
41 12         58 $format = _regex_variable($formatter->{format}, qr/(\$\d)/, $formatter->{national_rule});
42             } else {
43 12   66     52 $format = $formatter->{intl_format} || $formatter->{format};
44             }
45 24         285 $number = _regex_variable($number, qr/^$pattern$/, $format);
46 24         88 return _maybe_add_country($object, $number, $national);
47             }
48             }
49 0           return _maybe_add_country($object, $number, $national);
50             }
51              
52             1;
53              
54             =head1 NAME
55              
56             Number::Phone::Formatter - base class for other formatters
57              
58             =head1 DESCRIPTION
59              
60             A base class containing utility functions used by the formatters.
61              
62             =head1 AUTHOR, COPYRIGHT and LICENCE
63              
64             Copyright 2018 Matthew Somerville EFE
65              
66             This software is free-as-in-speech software, and may be used,
67             distributed, and modified under the terms of either the GNU
68             General Public Licence version 2 or the Artistic Licence. It's
69             up to you which one you use. The full text of the licences can
70             be found in the files GPL2.txt and ARTISTIC.txt, respectively.
71              
72             =cut