File Coverage

blib/lib/String/Validator/Phone/NANP.pm
Criterion Covered Total %
statement 82 84 97.6
branch 14 16 87.5
condition n/a
subroutine 18 18 100.0
pod 8 9 88.8
total 122 127 96.0


line stmt bran cond sub pod time code
1             package String::Validator::Phone::NANP;
2              
3 6     6   159136 use 5.006;
  6         26  
  6         270  
4 6     6   54 use strict;
  6         12  
  6         275  
5 6     6   34 use warnings;
  6         21  
  6         204  
6 6     6   9550 use String::Validator::Common ;
  6         6561  
  6         185  
7 6     6   6019 use Number::Phone ;
  6         2213095  
  6         55  
8              
9             =head1 NAME
10              
11             String::Validator::Phone::NANP - Check a Phone Number (North American Numbering Plan)!
12              
13             =head1 VERSION
14              
15             Version 0.96
16              
17             =cut
18              
19             our $VERSION = '0.96';
20              
21             sub new {
22 6     6 1 3653 my $class = shift ;
23 6         25 my $self = { @_ } ;
24 6     6   541 use base ( 'String::Validator::Common' ) ;
  6         17  
  6         9090  
25 6 100       39 unless ( defined $self->{ alphanum } ) { $self->{ alphanum } = 0 } ;
  2         10  
26             # disable length checking.
27 6         22 $self->{ min_len } = 0 ; $self->{ max_len } = 0 ;
  6         19  
28 6         21 bless $self, $class ;
29 6         24 return $self ;
30             }
31              
32             # Private method to change to area-exchange-num format.
33              
34             sub _clean {
35 53     53   35579 my $num = uc( shift @_ ) ;
36 53         106 my $alphanum = shift @_ ;
37 53 100       262 if ( $alphanum ) {
38 31         393 $num =~ s/\W//g ;
39 31         99 $num =~ tr/ABCDEFGHIJKLMNOPQRSTUVWXYZ/22233344455566677778889999/ ;
40             }
41 22         184 else { $num =~ s/\D//g ; }
42 53         278 $num =~ s/^1// ;
43 53         172 my $area = substr( $num, 0, 3, '' ) ;
44 53         95 my $exch = substr( $num, 0, 3, '' ) ;
45 53         230 return qq /$area-$exch-$num/ ;
46             }
47              
48             # North American Numbers have a length of 10.
49             # Returns 1 if this is true 0 and increases err if not.
50             sub _must_be10 {
51 53     53   34548 my $self = shift ;
52 53         367 my $num = shift ;
53 53         415 my $num2 = $num ;
54 53         335 $num2 =~ s/\W//g ;
55 53         115 my $l = length $num2 ;
56 53 100       151 if ( 10 == $l ) { return 1 }
  40         258  
57 13         105 else { $self->IncreaseErr(
58             "Not a 10 digit Area-Number $num .. $num2 = $l." ) }
59 13         241 return 0 ;
60             }
61              
62             sub _Init {
63 21     21   134 my $self = shift ;
64 21         65 $self->{ error } = 0 ;
65 21         58 for ( qw / string original international errstring areacode exchange local /) {
66 147         386 $self->{ $_ } = '' ; }
67             } ;
68              
69             sub Check {
70 21     21 1 37903 my ( $self, $string1, $string2 ) = @_ ;
71 21 50       121 if ( $self->Start( $string1, $string2 ) ) {
72 0         0 return $self->{ error } }
73 21         265 $self->{ string } = &_clean( $string1, $self->{alphanum} ) ;
74 21 100       86 unless( $self->_must_be10( $self->{ string } ) ) {
75 4         7 $self->{ string } = '' ; return $self->{ error } }
  4         19  
76 17         39 $self->{ original } = $string1 ;
77             #Number::Phone requires a leading 1.;
78             ( $self->{ areacode }, $self->{ exchange }, $self->{ local } ) =
79 17         111 split /\-/, $self->{ string };
80 17         70 $self->{ international } = '1-' . $self->{ string } ;
81 17         110 my $Phone = Number::Phone->new( $self->{ international } ) ;
82 17 100       53834 unless ( $Phone ) {
83 2         66 $self->IncreaseErr( 'Invalid Number, perhaps non-existent Area Code' ) }
84             else {
85 15 50       107 unless ( $Phone->is_valid ) {
86 0         0 $self->IncreaseErr( 'Invalid Number, perhaps non-existent Area Code' ) }
87             }
88 17         450 return $self->{ error } ;
89             }
90              
91             # sub String # is inherited from String::Validator::Common.
92              
93 5     5 1 1149 sub Original { my $self = shift ; return $self->{ original } ; } ;
  5         51  
94              
95             sub Areacode {
96 8     8 1 13 my $self = shift ;
97 8         41 return ( $self->{ areacode } ) ;
98             }
99            
100             sub Exchange {
101 8     8 0 16 my $self = shift ;
102 8         34 return ( $self->{ exchange } ) ;
103             }
104              
105             sub Local {
106 8     8 1 135 my $self = shift ;
107 8         43 return ( $self->{ local } ) ;
108             }
109              
110 5     5 1 2635 sub International { my $self = shift ; return $self->{ international } }
  5         36  
111              
112             sub Parens {
113 4     4 1 10 my $self = shift ;
114 4         11 my $area = $self->Areacode() ;
115 4         11 my $exchange = $self->Exchange() ;
116 4         9 my $local = $self->Local() ;
117 4         30 return "($area) $exchange-$local" ;
118             }
119              
120             sub Number_Phone {
121 7     7 1 660 my $self = shift ;
122 7 100       29 unless( $self->{ international } ) { return 0 } ;
  1         7  
123 6         32 return Number::Phone->new( $self->{ international } ) ;
124             }
125              
126             =head1 SYNOPSIS
127              
128             String::Validator::Phone::NANP is part of the String Validator Collection. It checks a string
129             against validation rules for phone numbers from countries participating in the North American
130             Numbering Plan, which includes the United States and Canada.
131              
132             =head1 String::Validator Methods and Usage
133              
134             Provides and conforms to the standard String::Validator methods,
135             please see String::Validator for general documentation, and
136             String::Validator::Common for information on the base String::Validator Class.
137              
138             =head1 Methods Specific to String::Validator::Phone::NANP
139              
140             =head2 Parameters to New with (default) behaviour.
141              
142             alphanum (OFF) : Allow Alphanumeric formats.
143              
144             =head2 Original, String, International, Areacode, Parens, Local
145              
146             Returns:
147              
148             Original: the Orignial string provided,
149              
150             String: the internal representations of the phone number, which
151             is in the format of AREA-EXCHANGE-NUMBER, (the most commonly used representation in the United
152             States).
153              
154             International: Prepends 1- in front of the string.
155              
156             Areacode, Exchange, Local: Returns each of the 3 components of a number
157              
158             Parens: Formats the number (AREA) EXCHANGE-LOCAL.
159              
160             =head2 Number_Phone
161              
162             Returns a Number::Phone::NANP object based on the current phone number, if the last
163             number evaluated was not valid it returns 0.
164              
165             =head1 Example
166              
167             use String::Validator::Phone::NANP ;
168             my $Validator = String::Validator::Phone::NANP->new( alphanum => 1 ) ;
169            
170             if ( $Validator->IsNot_Valid( '6464') { say $Validator->Errstr() }
171             # IsNot_Valid returns Errstr on failure.
172             # So the preceding and following are the same.
173             my $badone = $Validator->IsNot_Valid( '999') ;
174             if ( $badone ) { say "$badone' } ;
175            
176             if ( $Validator->Is_Valid( '646-SG7-6464' ) { say "good" }
177             say $Validator->Areacode ; # print the Areacode.
178             my $PhoneNum = $Validator->Number_Phone ; # Get a Number Phone object.
179             say $PhoneNum->country; # Prints the two letter country code of the number.
180              
181             =head1 ToDo
182              
183             The major TO DO items are to provide String::Validator::Phone modules for other numbering
184             schemes and to fully encapsulate Number::Phone.
185              
186             =head1 AUTHOR
187              
188             John Karr, C<< >>
189              
190             =head1 BUGS
191              
192             Please report any bugs or feature requests to C, or through
193             the web interface at L. I will be notified, and then you'll
194             automatically be notified of progress on your bug as I make changes.
195              
196             =head1 SUPPORT
197              
198             You can find documentation for this module with the perldoc command.
199              
200             perldoc String::Validator::Phone
201              
202              
203             You can also look for information at:
204              
205             =over 4
206              
207             =item * RT: CPAN's request tracker (report bugs here)
208              
209             L
210              
211             =item * AnnoCPAN: Annotated CPAN documentation
212              
213             L
214              
215             =item * CPAN Ratings
216              
217             L
218              
219             =item * Search CPAN
220              
221             L
222              
223             =back
224              
225              
226             =head1 ACKNOWLEDGEMENTS
227              
228              
229             =head1 LICENSE AND COPYRIGHT
230              
231             Copyright 2012 John Karr.
232              
233             This program is free software; you can redistribute it and/or modify
234             it under the terms of the GNU General Public License as published by
235             the Free Software Foundation, version 3 or at your option
236             any later version.
237              
238             This program is distributed in the hope that it will be useful,
239             but WITHOUT ANY WARRANTY; without even the implied warranty of
240             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
241             GNU General Public License for more details.
242              
243             A copy of the GNU General Public License is available in the source tree;
244             if not, write to the Free Software Foundation, Inc.,
245             59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
246              
247              
248             =cut
249              
250             1; # End of String::Validator::Phone::NANP