File Coverage

blib/lib/Data/FormValidator/Constraints/NumberPhone.pm
Criterion Covered Total %
statement 29 29 100.0
branch 9 10 90.0
condition 3 3 100.0
subroutine 8 8 100.0
pod 2 2 100.0
total 51 52 98.0


line stmt bran cond sub pod time code
1             package Data::FormValidator::Constraints::NumberPhone;
2              
3             ###############################################################################
4             # Required inclusions;
5 1     1   95865 use strict;
  1         16  
  1         27  
6 1     1   5 use warnings;
  1         2  
  1         26  
7 1     1   446 use Number::Phone;
  1         39047  
  1         5  
8              
9             ###############################################################################
10             # Version number.
11             our $VERSION = '0.05';
12              
13             ###############################################################################
14             # Allow our methods to be exported.
15 1     1   52 use base qw(Exporter);
  1         2  
  1         77  
16 1     1   7 use vars qw(@EXPORT_OK);
  1         1  
  1         230  
17             our @EXPORT_OK = qw(
18             FV_american_phone
19             FV_telephone
20             );
21              
22             ###############################################################################
23             # Subroutine: FV_american_phone()
24             ###############################################################################
25             # Creates a constraint closure that returns true if the constrained value
26             # appears to be a valid North American telephone number (Canada, or US)
27             sub FV_american_phone {
28 7     7 1 5530 return FV_telephone(qw( CA US ));
29             }
30              
31             ###############################################################################
32             # Subroutine: FV_telephone(@countries)
33             # Creates a constraint closure that returns true if the constrained value
34             # appears to be a valid telephone number.
35             #
36             # REQUIRES a list of Country Codes (e.g. "CA", "US", "UK"), to specify which
37             # countries should be considered valid. By default, *NO* countries are
38             # considered valid (and thus, no numbers are considered valid by default).
39             sub FV_telephone {
40 14     14 1 4994 my @countries = @_;
41              
42             return sub {
43 14     14   4835 my $dfv = shift;
44 14         40 my $val = $dfv->get_current_constraint_value;
45              
46             # No country? Not valid; you didn't tell us what to validate it for.
47 14 100       78 return 0 unless @countries;
48              
49             # Add leading "+" if it looks like we've got a long distance prefix
50 13 100       53 $val = "+$val" if ($val =~ /^\s*1/);
51              
52             # Try to instantiate the telephone number using any of the provided
53             # Countries.
54 13         26 foreach my $country (@countries) {
55 17         80 my $ph = Number::Phone->new($country => $val);
56 17 100       317321 next unless $ph;
57 10 50       46 next unless $ph->is_valid;
58 10 100 100     108 next unless ($ph->country && (uc($ph->country) eq uc($country)));
59 6         196 return 1;
60             }
61              
62 7         79 return 0;
63 14         126 };
64             }
65              
66             1;
67              
68             =head1 NAME
69              
70             Data::FormValidator::Constraints::NumberPhone - Data constraints, using Number::Phone
71              
72             =head1 SYNOPSIS
73              
74             use Data::FormValidator::Constraints::NumberPhone qw(FV_american_phone);
75              
76             constraint_methods => {
77             phone => FV_american_phone(),
78             canada_phone => FV_american_phone(qw( CA )),
79             },
80              
81             =head1 DESCRIPTION
82              
83             This module implements methods to help validate data using
84             C and C.
85              
86             =head1 METHODS
87              
88             =over
89              
90             =item FV_american_phone()
91              
92             Creates a constraint closure that returns true if the constrained value
93             appears to be a valid North American telephone number (Canada, or US)
94              
95             =item FV_telephone(@countries)
96              
97             Creates a constraint closure that returns true if the constrained value
98             appears to be a valid telephone number.
99              
100             REQUIRES a list of Country Codes (e.g. "CA", "US", "UK"), to specify which
101             countries should be considered valid. By default, *NO* countries are
102             considered valid (and thus, no numbers are considered valid by default).
103              
104             =back
105              
106             =head1 AUTHOR
107              
108             Graham TerMarsch (cpan@howlingfrog.com)
109              
110             =head1 COPYRIGHT
111              
112             Copyright (C) 2012, Graham TerMarsch. All Rights Reserved.
113              
114             =head1 SEE ALSO
115              
116             =over
117              
118             =item L
119              
120             =item L
121              
122             =back
123              
124             =cut