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   2978 use strict;
  5         12  
  5         146  
4 5     5   31 use warnings;
  5         10  
  5         221  
5              
6             our $VERSION = '1.0';
7              
8 5     5   58 use Number::Phone::Lib;
  5         35  
  5         76  
9              
10             sub _regex_variable {
11 36     36   98 my ($var, $qr, $subs) = @_;
12 36         74 $subs =~ s/"/\\"/;
13 36         82 $subs = "\"$subs\"";
14 36         234 $var =~ s/$qr/$subs/xee;
  36         2366  
15 36         172 return $var;
16             }
17              
18             sub _maybe_add_country {
19 24     24   61 my ($object, $number, $national) = @_;
20 24 100       83 $number = '+' . $object->country_code() . ' ' . $number unless $national;
21 24         342 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   86 my ($class, $object, $national) = @_;
30              
31 24 100       235 if(!$object->isa('Number::Phone::StubCountry')) {
32 3         14 $object = Number::Phone::Lib->new($object->format())
33             }
34              
35 24         60 my $number = $object->{number};
36 24         36 foreach my $formatter (@{$object->{formatters}}) {
  24         69  
37 152         314 my ($leading_digits, $pattern) = map { $formatter->{$_} } qw(leading_digits pattern);
  304         650  
38 152 100 100     5844 if ((!$leading_digits || $number =~ /^($leading_digits)/x) && $number =~ /^$pattern$/x) {
      100        
39 24         82 my $format;
40 24 100 100     106 if ($national && $formatter->{national_rule}) {
41 12         64 $format = _regex_variable($formatter->{format}, qr/(\$\d)/, $formatter->{national_rule});
42             } else {
43 12   66     51 $format = $formatter->{intl_format} || $formatter->{format};
44             }
45 24         305 $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