File Coverage

blib/lib/String/Validator/Phone/NANP.pm
Criterion Covered Total %
statement 88 89 98.8
branch 15 16 93.7
condition 2 3 66.6
subroutine 19 19 100.0
pod 9 9 100.0
total 133 136 97.7


line stmt bran cond sub pod time code
1             package String::Validator::Phone::NANP;
2             $String::Validator::Phone::NANP::VERSION = '2.04';
3 7     7   468757 use 5.008;
  7         82  
4 7     7   43 use strict;
  7         13  
  7         141  
5 7     7   32 use warnings;
  7         15  
  7         290  
6 7     7   3774 use String::Validator::Common 2.01;
  7         242213  
  7         229  
7 7     7   3874 use Number::Phone 3.9001;
  7         618819  
  7         57  
8              
9             # ABSTRACT: Validate North American Phone Numbers
10              
11              
12             my $phonenanp_messages = {
13             phonenanp_not10 => sub {
14             my $D = shift; # num1, num2, len
15             return "Not a 10 digit Area-Number $D->{num1} .. $D->{num2} = $D->{len}.";
16             },
17             phonenanp_badarea => 'Invalid Number, perhaps non-existent Area Code',
18             };
19              
20             sub new {
21 7     7 1 5761 my $class = shift ;
22 7         26 my $self = { @_ } ;
23 7     7   1057 use base ( 'String::Validator::Common' ) ;
  7         22  
  7         7160  
24 7 100       42 unless ( defined $self->{ alphanum } ) { $self->{ alphanum } = 0 } ;
  2         8  
25             # disable length checking.
26 7         21 $self->{ min_len } = 0 ; $self->{ max_len } = 0 ;
  7         18  
27 7         21 bless $self, $class ;
28             $self->{messages}
29             = String::Validator::Common::_Messages(
30 7         78 $phonenanp_messages, $self->{language}, $self->{custom_messages} );
31 7         305 return $self ;
32             }
33              
34             # Private method to change to area-exchange-num format.
35              
36             sub _clean {
37 56     56   16979 my $num = uc( shift @_ ) ;
38 56         224 my $alphanum = shift @_ ;
39 56 100       151 if ( $alphanum ) {
40 32         220 $num =~ s/\W//g ;
41 32         90 $num =~ tr/ABCDEFGHIJKLMNOPQRSTUVWXYZ/22233344455566677778889999/ ;
42             }
43 24         202 else { $num =~ s/\D//g ; }
44 56         157 $num =~ s/^1// ;
45 56         170 my $area = substr( $num, 0, 3, '' ) ;
46 56         159 my $exch = substr( $num, 0, 3, '' ) ;
47 56         289 return qq /$area-$exch-$num/ ;
48             }
49              
50             # North American Numbers have a length of 10.
51             # Returns 1 if this is true 0 and increases err if not.
52             sub _must_be10 {
53 56     56   17515 my $self = shift ;
54 56         102 my $num = shift ;
55 56         104 my $num2 = $num ;
56 56         280 $num2 =~ s/\W//g ;
57 56         124 my $l = length $num2 ;
58 56 100       160 if ( 10 == $l ) { return 1 }
  42         142  
59             else { $self->IncreaseErr(
60 14         92 $self->{messages}{phonenanp_not10}->({
61             num1 => $num, num2 => $num2, len => $l }));
62             }
63 14         158 return 0 ;
64             }
65              
66             sub _Init {
67 25     25   162 my $self = shift ;
68 25         56 $self->{ error } = 0 ;
69 25         68 for ( qw / string original international errstring areacode exchange local /) {
70 175         399 $self->{ $_ } = '' ; }
71             } ;
72              
73             sub _AreaOK {
74 19     19   36 my $self = shift;
75 19         36 my $ac = shift;
76 19 100 66     148 if ( $ac =~ /\d11/ or $ac < 200 ) {
77             $self->IncreaseErr(
78 3         37 $self->{messages}{phonenanp_badarea} );
79 3         36 return 0;
80 16         51 } else { return 1 }
81             }
82              
83             sub Check {
84 25     25 1 37813 my ( $self, $string1, $string2 ) = @_ ;
85 25 100       103 if ( $self->Start( $string1, $string2 ) ) {
86 1         17 return $self->{ error } }
87 24         323 $self->{ string } = &_clean( $string1, $self->{alphanum} ) ;
88 24 100       85 unless( $self->_must_be10( $self->{ string } ) ) {
89 5         12 $self->{ string } = '' ; return $self->{ error } }
  5         23  
90 19         64 $self->{ original } = $string1 ;
91             #Number::Phone requires a leading 1.;
92             ( $self->{ areacode }, $self->{ exchange }, $self->{ local } ) =
93 19         92 split /\-/, $self->{ string };
94 19         58 $self->{ international } = '1-' . $self->{ string } ;
95 19         62 $self->_AreaOK( $self->{ areacode });
96 19         86 my $Phone = Number::Phone->new( $self->{ international } ) ;
97 19 50       1077787 unless ( $Phone ) {
98 0         0 $self->IncreaseErr( $self->{messages}{phonenanp_badarea} ) }
99 19         191 return $self->{ error } ;
100             }
101              
102             # sub String # is inherited from String::Validator::Common.
103              
104 5     5 1 1211 sub Original { my $self = shift ; return $self->{ original } ; } ;
  5         33  
105              
106             sub Areacode {
107 8     8 1 15 my $self = shift ;
108 8         34 return ( $self->{ areacode } ) ;
109             }
110              
111             sub Exchange {
112 9     9 1 610 my $self = shift ;
113 9         42 return ( $self->{ exchange } ) ;
114             }
115              
116             sub Local {
117 8     8 1 17 my $self = shift ;
118 8         26 return ( $self->{ local } ) ;
119             }
120              
121 5     5 1 2296 sub International { my $self = shift ; return $self->{ international } }
  5         27  
122              
123             sub Parens {
124 4     4 1 8 my $self = shift ;
125 4         11 my $area = $self->Areacode() ;
126 4         15 my $exchange = $self->Exchange() ;
127 4         10 my $local = $self->Local() ;
128 4         20 return "($area) $exchange-$local" ;
129             }
130              
131             sub Number_Phone {
132 7     7 1 636 my $self = shift ;
133 7 100       22 unless( $self->{ international } ) { return 0 } ;
  1         11  
134 6         22 return Number::Phone->new( $self->{ international } ) ;
135             }
136              
137              
138             1; # End of String::Validator::Phone::NANP
139              
140             __END__