blib/lib/Data/FormValidator/Constraints/NumberPhone.pm

filter: Statementstmt 100% 32 / 32 Branchbran 90.0% 9 / 10 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 94.7% 54 / 57 CC 8 SCAR 15.6
CC
8
Cov
96%
__ANON__
8.0
FV_telephone
1.0
BEGIN
1.0
CRAP 4.8
↑ Summary
1 package Data::FormValidator::Constraints::NumberPhone;
2  
3 ###############################################################################
4 # Required inclusions;
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Number::Phone;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 ###############################################################################
10 # Version number.
11 2 our $VERSION = '0.06';
12  
13 ###############################################################################
14 # Allow our methods to be exported.
15 2 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use vars qw(@EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 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 6 ▶sub FV_american_phone {
Subroutine FV_american_phone: called CC=1 SCAR=0.0
Pod documented
28 6 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 13 ▶sub FV_telephone {
Subroutine FV_telephone: called CC=1 SCAR=0.0
Pod documented
40 13 my @countries = @_;
41  
42 13 ▶ return sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
43 13 my $dfv = shift;
44 13 my $val = $dfv->get_current_constraint_value;
45  
46 # No country? Not valid; you didn't tell us what to validate it for.
47 13 ▶ return 0 unless @countries;
Branch: 1 branch
BranchTrueFalseTotal
unless @countries 1 12 13
48  
49 # Add leading "+" if it looks like we've got a long distance prefix
50 12 ▶ $val = "+$val" if ($val =~ /^\s*1/);
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^\s*1/ 1 11 12
51  
52 # Try to instantiate the telephone number using any of the provided
53 # Countries.
54 12 foreach my $country (@countries) {
55 15 my $ph = Number::Phone->new($country => $val);
56 15 ▶ next unless $ph;
Branch: 1 branch
BranchTrueFalseTotal
unless $ph 6 9 15
57 9 ▶ next unless $ph->is_valid;
Branch: 1 branch
BranchTrueFalseTotal
unless $ph->is_valid 0 9 9
58 9 ▶ next unless ($ph->country && (uc($ph->country) eq uc($country)));
Branch: 1 branch
BranchTrueFalseTotal
unless $ph->country and uc $ph->country eq uc $country 3 6 9
Condition66%2/3
$ph->country and uc $ph->country eq uc $country
!ll&&!rl&&r
036
MC/DC50%1/2
$ph->country and uc $ph->country eq uc $country
$ph->country uc $ph->country eq uc $country
Truth table66%2/3
A and B
A= $ph->countryB= uc $ph->country eq uc $country
ABresult
0X0
100
111
59 6 return 1;
60 }
61  
62 6 return 0;
63 13 };
64 }
65  
66 2 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<Data::FormValidator> and C<Number::Phone>.
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<Data::FormValidator>
119  
120 =item L<Number::Phone>
121  
122 =back
123  
124 =cut
↑ Summary