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   115855 use strict;
  1         17  
  1         33  
6 1     1   8 use warnings;
  1         2  
  1         32  
7 1     1   553 use Number::Phone;
  1         48269  
  1         7  
8              
9             ###############################################################################
10             # Version number.
11             our $VERSION = '0.04';
12              
13             ###############################################################################
14             # Allow our methods to be exported.
15 1     1   66 use base qw(Exporter);
  1         2  
  1         96  
16 1     1   7 use vars qw(@EXPORT_OK);
  1         3  
  1         301  
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 6822 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 5750 my @countries = @_;
41              
42             return sub {
43 14     14   5762 my $dfv = shift;
44 14         49 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       91 return 0 unless @countries;
48              
49             # Add leading "+" if it looks like we've got a long distance prefix
50 13 100       71 $val = "+$val" if ($val =~ /^\s*1/);
51              
52             # Try to instantiate the telephone number using any of the provided
53             # Countries.
54 13         43 foreach my $country (@countries) {
55 17         98 my $ph = Number::Phone->new($country => $val);
56 17 100       391238 next unless $ph;
57 10 50       60 next unless $ph->is_valid;
58 10 100 100     136 next unless ($ph->country && (uc($ph->country) eq uc($country)));
59 6         221 return 1;
60             }
61              
62 7         98 return 0;
63 14         150 };
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             L,
117             L.
118              
119             =cut