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   2433 use strict;
  5         16  
  5         151  
4 5     5   29 use warnings;
  5         12  
  5         201  
5              
6             our $VERSION = '1.0';
7              
8 5     5   54 use Number::Phone::Lib;
  5         49  
  5         104  
9              
10             sub _regex_variable {
11 36     36   100 my ($var, $qr, $subs) = @_;
12 36         89 $subs =~ s/"/\\"/;
13 36         76 $subs = "\"$subs\"";
14 36         218 $var =~ s/$qr/$subs/xee;
  36         2388  
15 36         166 return $var;
16             }
17              
18             sub _maybe_add_country {
19 24     24   55 my ($object, $number, $national) = @_;
20 24 100       87 $number = '+' . $object->country_code() . ' ' . $number unless $national;
21 24         257 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   56 my ($class, $object, $national) = @_;
30              
31 24 100       141 if(!$object->isa('Number::Phone::StubCountry')) {
32 3         13 $object = Number::Phone::Lib->new($object->format())
33             }
34              
35 24         51 my $number = $object->{number};
36 24         40 foreach my $formatter (@{$object->{formatters}}) {
  24         58  
37 152         306 my ($leading_digits, $pattern) = map { $formatter->{$_} } qw(leading_digits pattern);
  304         723  
38 152 100 100     5734 if ((!$leading_digits || $number =~ /^($leading_digits)/x) && $number =~ /^$pattern$/x) {
      100        
39 24         57 my $format;
40 24 100 100     91 if ($national && $formatter->{national_rule}) {
41 12         58 $format = _regex_variable($formatter->{format}, qr/(\$\d)/, $formatter->{national_rule});
42             } else {
43 12   66     43 $format = $formatter->{intl_format} || $formatter->{format};
44             }
45 24         276 $number = _regex_variable($number, qr/^$pattern$/, $format);
46 24         93 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