blib/lib/Data/FormValidator/Constraints.pm

filter: Statementstmt 79.2% 278 / 351 Branchbran 50.0% 75 / 150 Conditioncond 42.4% 65 / 153 MC/DCmcdc 10.2% 8 / 78 Subroutinesub 85.3% 64 / 75 Podpod 59.3% 19 / 32 totaltotal 60.6% 509 / 839 SCAR 62.7
CC
96
Cov
64%
match_cc_number
38.8
match_cc_exp
13.4
match_ip_address
13.4
CRAP 529.1
« blib/lib/Data/FormValidator/Constraints/Upload.pm ↑ Summary blib/lib/Data/FormValidator/Results.pm »
1 #
2 # Constraints.pm - Standard constraints for use in Data::FormValidator.
3 #
4 # This file is part of Data::FormValidator.
5 #
6 # Author: Francis J. Lacoste
7 # Maintainer: Mark Stosberg <mark@summersault.com>
8 #
9 # Copyright (C) 1999,2000 iNsu Innovations Inc.
10 # Copyright (C) 2001 Francis J. Lacoste
11 # Parts Copyright 1996-1999 by Michael J. Heins
12 # Parts Copyright 1996-1999 by Bruce Albrecht
13 #
14 # Parts of this module are based on work by
15 # Bruce Albrecht, contributed to MiniVend.
16 #
17 # Parts also based on work by Michael J. Heins
18 #
19 # This program is free software; you can redistribute it and/or modify
20 # it under the terms same terms as perl itself.
21 #
22 package Data::FormValidator::Constraints;
23 59 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
24 59 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 59 our $AUTOLOAD;
26  
27 59 our $VERSION = 4.85;
28  
29 BEGIN {
30 59 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 59 ▶ my @closures = (qw/
Subroutine BEGIN: called CC=4 SCAR=20.4
32 american_phone
33 cc_exp
34 cc_number
35 cc_type
36 email
37 ip_address
38 phone
39 postcode
40 province
41 state
42 state_or_province
43 zip
44 zip_or_postcode/);
45  
46 # This be optimized with some of the voodoo that CGI.pm
47 # uses to AUTOLOAD dynamic functions.
48 59 for my $func (@closures) {
49 # cc_number is defined statically
50 767 ▶ unless ($func eq 'cc_number') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($func eq "cc_number") 708 59 767
51 # Notice we have to escape some characters
52 # in the subroutine, which is really a string here.
53  
54 708 local $SIG{__DIE__} = \&confess;
55 708 my $code = qq!
56 sub $func {
57 return sub {
58 my \$dfv = shift;
59 use Scalar::Util ();
60 die "first arg to $func was not an object. Must be called as a constraint_method."
61 unless ( Scalar::Util::blessed(\$dfv) && \$dfv->can('name_this') );
62  
63 \$dfv->name_this('$func') unless \$dfv->get_current_constraint_name();
64 no strict 'refs';
65 return &{"match_$func"}(\@_);
66 }
67 }
68 !;
69  
70 708 ▶ eval "package Data::FormValidator::Constraints; $code";
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine BEGIN: called CC=1 SCAR=2.3
Subroutine __ANON__: called CC=4 SCAR=21.1
Subroutine american_phone: not called CC=1 SCAR=2.3
Subroutine cc_exp: not called CC=1 SCAR=2.3
Subroutine cc_type: not called CC=1 SCAR=2.3
Subroutine email: called CC=1 SCAR=2.3
Subroutine ip_address: not called CC=1 SCAR=2.3
Subroutine phone: not called CC=1 SCAR=2.3
Subroutine postcode: not called CC=1 SCAR=2.3
Subroutine province: not called CC=1 SCAR=2.3
Subroutine state: not called CC=1 SCAR=2.3
Subroutine state_or_province: not called CC=1 SCAR=2.3
Subroutine zip: not called CC=1 SCAR=2.3
Subroutine zip_or_postcode: not called CC=1 SCAR=2.3
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Pod documented
Branch: 24 branches
BranchTrueFalseTotal
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 1 1
unless $dfv->get_current_constraint_name 1 0 1
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
unless builtin::blessed($dfv) and $dfv->can("name_this") 0 0 0
unless $dfv->get_current_constraint_name 0 0 0
Condition2%1/36
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
001
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
builtin::blessed($dfv) and $dfv->can("name_this")
!ll&&!rl&&r
000
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->can("name_this")
builtin::blessed($dfv) $dfv->can("name_this")
Truth table33%1/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($dfv)B= $dfv->can("name_this")
ABresult
0X0
100
111
71 708 ▶ die "couldn't create $func: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 708 708
72 }
73 }
74  
75 59 my @FVs = (qw/
76 FV_length_between
77 FV_min_length
78 FV_max_length
79 FV_eq_with
80 FV_num_values
81 FV_num_values_between
82 /);
83  
84 59 our @EXPORT_OK = (
85 @closures,
86 @FVs,
87 qw(
88 valid_american_phone
89 valid_cc_exp
90 valid_cc_number
91 valid_cc_type
92 valid_email
93 valid_ip_address
94 valid_phone
95 valid_postcode
96 valid_province
97 valid_state
98 valid_state_or_province
99 valid_zip
100 valid_zip_or_postcode
101 match_american_phone
102 match_cc_exp
103 match_cc_number
104 match_cc_type
105 match_email
106 match_ip_address
107 match_phone
108 match_postcode
109 match_province
110 match_state
111 match_state_or_province
112 match_zip
113 match_zip_or_postcode)
114 );
115  
116 59 our %EXPORT_TAGS = (
117 # regexp common is correctly empty here, because we handle the case on the fly with the import function below.
118 regexp_common => [],
119 closures => [ @closures, @FVs ],
120 validators => [qw/
121 valid_american_phone
122 valid_cc_exp
123 valid_cc_number
124 valid_cc_type
125 valid_email
126 valid_ip_address
127 valid_phone
128 valid_postcode
129 valid_province
130 valid_state
131 valid_state_or_province
132 valid_zip
133 valid_zip_or_postcode
134 /],
135 matchers => [qw/
136 match_american_phone
137 match_cc_exp
138 match_cc_number
139 match_cc_type
140 match_email
141 match_ip_address
142 match_phone
143 match_postcode
144 match_province
145 match_state
146 match_state_or_province
147 match_zip
148 match_zip_or_postcode
149 /],
150 );
151  
152 sub import {
153 # This is Regexp::Common support.
154 # Here we are handling cases that look like this:
155 #
156 # my_field => FV_foo_bar(-zoo=>'queue'),
157 126 ▶ if (grep { m/^:regexp_common$/ } @_) {
Subroutine import: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (grep {/^:regexp_common$/;} @_) 1 125 126
158 1 require Regexp::Common;
159 1 import Regexp::Common 'RE_ALL';
160  
161 1 for my $sub (grep { m/^RE_/} keys %Data::FormValidator::Constraints:: ) {
162 59 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
163 173 my $new_name = $sub;
164 173 $new_name =~ s/^RE_/FV_/;
165 173 *{caller() . "::$new_name"} = sub {
166 6 ▶ my @params = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
167 return sub {
168 9 ▶ my $dfv = shift;
Subroutine __ANON__: called CC=2 SCAR=7.0
169 9 ▶ $dfv->name_this($new_name) unless $dfv->get_current_constraint_name();
Branch: 1 branch
BranchTrueFalseTotal
unless $dfv->get_current_constraint_name 9 0 9
170  
171 59 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
172 9 my $re = &$sub(-keep=>1,@params);
173 9 my ($match) = ($dfv->get_current_constraint_value =~ qr/^($re)$/);
174 9 return $dfv->untainted_constraint_value($match);
175 }
176 6 }
177 173 }
178 }
179  
180 126 Data::FormValidator::Constraints->export_to_level(1,@_);
181 }
182  
183 }
184  
185  
186 # sub DESTROY {}
187  
188 =pod
189  
190 =head1 NAME
191  
192 Data::FormValidator::Constraints - Basic sets of constraints on input profile.
193  
194 =head1 SYNOPSIS
195  
196 use Data::FormValidator::Constraints qw(:closures);
197  
198 In an Data::FormValidator profile:
199  
200 constraint_methods => {
201 email => email(),
202 phone => american_phone(),
203 first_names => {
204 constraint_method => FV_max_length(3),
205 name => 'my_custom_name',
206 },
207 },
208 msgs => {
209 constraints => {
210 my_custom_name => 'My message',
211 },
212 },
213  
214  
215  
216 =head1 DESCRIPTION
217  
218 These are the builtin constraints that can be specified by name in the input
219 profiles.
220  
221 Be sure to check out the SEE ALSO section for even more pre-packaged
222 constraints you can use.
223  
224 =cut
225  
226 sub AUTOLOAD {
227 34 ▶ my $name = $AUTOLOAD;
Subroutine AUTOLOAD: called CC=3 SCAR=11.3
228  
229 59 ▶ no strict qw/refs/;
Subroutine BEGIN: called CC=1 SCAR=0.0
230  
231 34 $name =~ m/^(.*::)(valid_|RE_)(.*)/;
232  
233 34 my ($pkg,$prefix,$sub) = ($1,$2,$3);
234  
235 #warn "hello! my ($pkg,$prefix,$sub) = ($1,$2,$3);";
236  
237 # Since all the valid_* routines are essentially identical we're
238 # going to generate them dynamically from match_ routines with the same names.
239 34 ▶ if ((defined $prefix) and ($prefix eq 'valid_')) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefix and $prefix eq "valid_") 34 0 34
Condition33%1/3
defined $prefix and $prefix eq "valid_"
!ll&&!rl&&r
0034
MC/DC0%0/2
defined $prefix and $prefix eq "valid_"
defined $prefix $prefix eq "valid_"
Truth table33%1/3
A and B
A= defined $prefixB= $prefix eq "valid_"
ABresult
0X0
100
111
240 34 return defined &{$pkg.'match_' . $sub}(@_);
241 }
242 }
243  
244 =head2 FV_length_between(1,23)
245  
246 =head2 FV_max_length(23)
247  
248 =head2 FV_min_length(1)
249  
250 use Data::FormValidator::Constraints qw(
251 FV_length_between
252 FV_min_length
253 FV_max_length
254 );
255  
256 constraint_methods => {
257  
258 # specify a min and max, inclusive
259 last_name => FV_length_between(1,23),
260  
261 }
262  
263 Specify a length constraint for a field.
264  
265 These constraints have a different naming convention because they are higher-order
266 functions. They take input and return a code reference to a standard constraint
267 method. A constraint name of C<length_between>, C<min_length>, or C<max_length> will be set,
268 corresponding to the function name you choose.
269  
270 The checks are all inclusive, so a max length of '100' will allow the length 100.
271  
272 Length is measured in perl characters as opposed to bytes or anything else.
273  
274 This constraint I<will> untaint your data if you have untainting turned on. However,
275 a length check alone may not be enough to insure the safety of the data you are receiving.
276 Using additional constraints to check the data is encouraged.
277  
278 =cut
279  
280 sub FV_length_between {
281 8 ▶ my ($min,$max) = @_;
Subroutine FV_length_between: called CC=3 SCAR=11.2
Pod documented
282 8 ▶ if (not (defined $min and defined $max)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $min and defined $max) 0 8 8
Condition33%1/3
defined $min and defined $max
!ll&&!rl&&r
008
MC/DC0%0/2
defined $min and defined $max
defined $min defined $max
Truth table33%1/3
A and B
A= defined $minB= defined $max
ABresult
0X0
100
111
283 0 croak "min and max are required";
284 }
285 return sub {
286 8 ▶ my ($dfv,$value) = @_;
Subroutine __ANON__: called CC=4 SCAR=13.9
287 8 ▶ $dfv->name_this('length_between') unless $dfv->get_current_constraint_name();
Branch: 1 branch
BranchTrueFalseTotal
unless $dfv->get_current_constraint_name 7 1 8
288 8 ▶ return undef if ( ( length($value) > $max ) || ( length($value) < $min) );
Branch: 1 branch
BranchTrueFalseTotal
if length $value > $max or length $value < $min 5 3 8
Condition100%3/3
length $value > $max or length $value < $min
l!l&&r!l&&!r
233
MC/DC100%2/2
length $value > $max or length $value < $min
length $value > $max length $value < $min
Truth table100%3/3
A or B
A= length $value > $maxB= length $value < $min
ABresult
1X1
011
000
289 # Use a regexp to untaint
290 3 $value=~/(.*)/s;
291 3 return $dfv->untainted_constraint_value($1);
292 }
293 8 }
294  
295 sub FV_max_length {
296 6 ▶ my ($max) = @_;
Subroutine FV_max_length: called CC=2 SCAR=6.9
Pod documented
297 6 ▶ croak "max is required" unless defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 0 6 6
298 return sub {
299 6 ▶ my ($dfv,$value) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
300 6 ▶ $dfv->name_this('max_length') unless $dfv->get_current_constraint_name();
Branch: 1 branch
BranchTrueFalseTotal
unless $dfv->get_current_constraint_name 5 1 6
301 6 ▶ return undef if ( length($value) > $max );
Branch: 1 branch
BranchTrueFalseTotal
if length $value > $max 4 2 6
302 # Use a regexp to untaint
303 2 $value=~/(.*)/s;
304 2 return $dfv->untainted_constraint_value($1);
305 }
306 6 }
307  
308 sub FV_min_length {
309 5 ▶ my ($min) = @_;
Subroutine FV_min_length: called CC=2 SCAR=7.0
Pod documented
310 5 ▶ croak "min is required" unless defined $min;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $min 0 5 5
311 return sub {
312 4 ▶ my ($dfv,$value) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
313 4 ▶ $dfv->name_this('min_length') unless $dfv->get_current_constraint_name();
Branch: 1 branch
BranchTrueFalseTotal
unless $dfv->get_current_constraint_name 4 0 4
314 4 ▶ return undef if ( length($value) < $min );
Branch: 1 branch
BranchTrueFalseTotal
if length $value < $min 2 2 4
315 # Use a regexp to untaint
316 2 $value=~/(.*)/s;
317 2 return $dfv->untainted_constraint_value($1);
318 }
319 5 }
320  
321 =head2 FV_eq_with
322  
323 use Data::FormValidator::Constraints qw( FV_eq_with );
324  
325 constraint_methods => {
326 password => FV_eq_with('password_confirm'),
327 }
328  
329 Compares the current field to another field.
330 A constraint name of C<eq_with> will be set.
331  
332 =cut
333  
334 sub FV_eq_with {
335 2 ▶ my ($other_field) = @_;
Subroutine FV_eq_with: called CC=1 SCAR=0.0
Pod documented
336 return sub {
337 2 ▶ my $dfv = shift;
Subroutine __ANON__: called CC=3 SCAR=11.2
338 2 ▶ $dfv->name_this('eq_with') unless $dfv->get_current_constraint_name();
Branch: 1 branch
BranchTrueFalseTotal
unless $dfv->get_current_constraint_name 2 0 2
339  
340 2 my $curr_val = $dfv->get_current_constraint_value;
341  
342 2 my $data = $dfv->get_filtered_data;
343 # Sometimes the data comes through both ways...
344 2 ▶ my $other_val = (ref $data->{$other_field}) ? $data->{$other_field}[0] : $data->{$other_field};
Branch: 1 branch
BranchTrueFalseTotal
ref $data->{$other_field} ? : 0 2 2
345  
346 2 return ($curr_val eq $other_val);
347 }
348  
349 2 }
350  
351 =head2 FV_num_values
352  
353 use Data::FormValidator::Constraints qw ( FV_num_values );
354  
355 constraint_methods => {
356 attachments => FV_num_values(4),
357 }
358  
359 Checks the number of values in the array named by this param.
360 Note that this is useful for making sure that only one value was passed for a
361 given param (by supplying a size argument of 1).
362 A constraint name of C<num_values> will be set.
363  
364 =cut
365  
366 sub FV_num_values {
367 2 ▶ my $size = shift || croak 'size argument is required';
Subroutine FV_num_values: called CC=2 SCAR=8.4
Pod documented
Condition33%1/3
shift() || croak('size argument is required')
l!l&&r!l&&!r
200
MC/DC0%0/2
shift() || croak('size argument is required')
shift() croak('size argument is required')
Truth table33%1/3
A || B
A= shift()B= croak('size argument is required')
ABresult
1X1
011
000
368 return sub {
369 3 ▶ my $dfv = shift;
Subroutine __ANON__: called CC=4 SCAR=16.8
370 3 $dfv->name_this('num_values');
371 3 my $param = $dfv->get_current_constraint_field();
372 3 my $value = $dfv->get_filtered_data()->{$param};
373  
374 # If there's an arrayref of values provided, test the number of them found
375 # against the number of them of required
376 3 ▶ if (defined $value and ref $value eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value and ref $value eq 'ARRAY') { } 3 0 3
elsif ($size == 1) { } 0 0 0
Condition33%1/3
defined $value and ref $value eq "ARRAY"
!ll&&!rl&&r
003
MC/DC0%0/2
defined $value and ref $value eq "ARRAY"
defined $value ref $value eq "ARRAY"
Truth table33%1/3
A and B
A= defined $valueB= ref $value eq "ARRAY"
ABresult
0X0
100
111
377 3 my $num_values_found = scalar @$value;
378 3 return ($num_values_found == $size);
379 }
380 # If a size of 1 was requested, there was not an arrayref of values,
381 # there must be exactly one value.
382 elsif ($size == 1) {
383 0 return 1;
384 }
385 # Any other case is failure.
386 else {
387 0 return 0;
388 }
389 }
390 2 }
391  
392 =head2 FV_num_values_between
393  
394 use Data::FormValidator::Constraints qw ( FV_num_values_between );
395  
396 constraint_methods => {
397 attachments => FV_num_values_between(1,4),
398 }
399  
400 Checks that the number of values in the array named by this param is between
401 the supplied bounds (inclusively).
402 A constraint name of C<num_values_between> will be set.
403  
404 =cut
405  
406 sub FV_num_values_between {
407 2 ▶ my ($min, $max) = @_;
Subroutine FV_num_values_between: called CC=3 SCAR=13.6
Pod documented
408 2 ▶ croak 'min and max arguments are required' unless $min && $max;
Branch: 1 branch
BranchTrueFalseTotal
unless $min and $max 0 2 2
Condition33%1/3
$min and $max
!ll&&!rl&&r
002
MC/DC0%0/2
$min and $max
$min $max
Truth table33%1/3
A and B
A= $minB= $max
ABresult
0X0
100
111
409 return sub {
410 2 ▶ my $dfv = shift;
Subroutine __ANON__: called CC=5 SCAR=20.9
411 2 $dfv->name_this('num_values_between');
412 2 my $param = $dfv->get_current_constraint_field();
413 2 my $value = $dfv->get_filtered_data()->{$param};
414  
415 2 my $num_values = scalar @$value;
416  
417 2 ▶ return ($num_values >= $min) && ($num_values <= $max) if ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "ARRAY" 2 0 2
Condition66%2/3
$num_values >= $min && $num_values <= $max
!ll&&!rl&&r
101
MC/DC50%1/2
$num_values >= $min && $num_values <= $max
$num_values >= $min $num_values <= $max
Truth table66%2/3
A && B
A= $num_values >= $minB= $num_values <= $max
ABresult
0X0
100
111
418 0 ▶ return 1 if $min == 0 && $max >= 2; # scalar, size could be 1
Branch: 1 branch
BranchTrueFalseTotal
if $min == 0 and $max >= 2 0 0 0
Condition0%0/3
$min == 0 and $max >= 2
!ll&&!rl&&r
000
MC/DC0%0/2
$min == 0 and $max >= 2
$min == 0 $max >= 2
Truth table0%0/3
A and B
A= $min == 0B= $max >= 2
ABresult
0X0
100
111
419 0 return 0; # scalar, size can't be 1
420 }
421 2 }
422  
423 =head2 email
424  
425 Checks if the email LOOKS LIKE an email address. This should be sufficient
426 99% of the time.
427  
428 Look elsewhere if you want something super fancy that matches every possible variation
429 that is valid in the RFC, or runs out and checks some MX records.
430  
431 =cut
432  
433 # Many of the following validators are taken from
434 # MiniVend 3.14. (http://www.minivend.com)
435 # Copyright 1996-1999 by Michael J. Heins <mike@heins.net>
436  
437 sub match_email {
438 36 ▶ my $in_email = shift;
Subroutine match_email: called CC=3 SCAR=11.0
Pod undocumented
439  
440 36 require Email::Valid;
441 36 my $valid_email;
442  
443 # The extra check that the result matches the input prevents
444 # an address like this from being considered valid: Joe Smith <joe@smith.com>
445 36 ▶ if ( ($valid_email = Email::Valid->address($in_email) )
Branch: 1 branch
BranchTrueFalseTotal
if ($valid_email = 'Email::Valid'->address($in_email) and $valid_email eq $in_email) { } 10 26 36
Condition100%3/3
$valid_email = "Email::Valid"->address($in_email) and $valid_email eq $in_email
!ll&&!rl&&r
25110
MC/DC100%2/2
$valid_email = "Email::Valid"->address($in_email) and $valid_email eq $in_email
$valid_email = "Email::Valid"->address($in_email) $valid_email eq $in_email
Truth table100%3/3
A and B
A= $valid_email = "Email::Valid"->address($in_email)B= $valid_email eq $in_email
ABresult
0X0
100
111
446 and ($valid_email eq $in_email)) {
447 10 return $valid_email;
448 }
449 else {
450 26 return undef;
451 }
452 }
453  
454 59 my $state = <<EOF;
455 AL AK AZ AR CA CO CT DE FL GA HI ID IL IN IA KS KY LA ME MD
456 MA MI MN MS MO MT NE NV NH NJ NM NY NC ND OH OK OR PA PR RI
457 SC SD TN TX UT VT VA WA WV WI WY DC AP FP FPO APO GU VI
458 EOF
459  
460 59 my $province = <<EOF;
461 AB BC MB NB NF NL NS NT NU ON PE QC SK YT YK
462 EOF
463  
464 =head2 state_or_province
465  
466 This one checks if the input correspond to an american state or a canadian
467 province.
468  
469 =cut
470  
471 sub match_state_or_province {
472 4 ▶ my $match;
Subroutine match_state_or_province: called CC=2 SCAR=7.6
Pod undocumented
473 4 ▶ if ($match = match_state(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match = match_state(@_)) { } 0 4 4
474 0 return $match;
475 }
476 else {
477 4 return match_province(@_);
478 }
479 }
480  
481 =head2 state
482  
483 This one checks if the input is a valid two letter abbreviation of an
484 American state.
485  
486 =cut
487  
488 sub match_state {
489 11 ▶ my $val = shift;
Subroutine match_state: called CC=2 SCAR=6.9
Pod undocumented
490 11 ▶ if ($state =~ /\b($val)\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state =~ /\b($val)\b/i) { } 2 9 11
491 2 return $1;
492 }
493 9 else { return undef; }
494 }
495  
496 =head2 province
497  
498 This checks if the input is a two letter Canadian province
499 abbreviation.
500  
501 =cut
502  
503 sub match_province {
504 10 ▶ my $val = shift;
Subroutine match_province: called CC=2 SCAR=6.9
Pod undocumented
505 10 ▶ if ($province =~ /\b($val)\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($province =~ /\b($val)\b/i) { } 4 6 10
506 4 return $1;
507 }
508 6 else { return undef; }
509 }
510  
511 =head2 zip_or_postcode
512  
513 This constraints checks if the input is an American zipcode or a
514 Canadian postal code.
515  
516 =cut
517  
518 sub match_zip_or_postcode {
519 4 ▶ my $match;
Subroutine match_zip_or_postcode: called CC=2 SCAR=6.9
Pod undocumented
520 4 ▶ if ($match = match_zip(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match = match_zip(@_)) { } 2 2 4
521 2 return $match;
522 }
523 else {
524 2 return match_postcode(@_)
525 };
526 }
527 =pod
528  
529 =head2 postcode
530  
531 This constraints checks if the input is a valid Canadian postal code.
532  
533 =cut
534  
535 sub match_postcode {
536 6 ▶ my $val = shift;
Subroutine match_postcode: called CC=2 SCAR=6.9
Pod undocumented
537 #$val =~ s/[_\W]+//g;
538 6 ▶ if ($val =~ /^([ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy][_\W]*\d[_\W]*[A-Za-z][_\W]*[- ]?[_\W]*\d[_\W]*[A-Za-z][_\W]*\d[_\W]*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^([ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy][_\W]*\d[_\W]*[A-Za-z][_\W]*[- ]?[_\W]*\d[_\W]*[A-Za-z][_\W]*\d[_\W]*)$/) { } 2 4 6
539 2 return $1;
540 }
541 4 else { return undef; }
542 }
543  
544 =head2 zip
545  
546 This input validator checks if the input is a valid american zipcode :
547 5 digits followed by an optional mailbox number.
548  
549 =cut
550  
551 sub match_zip {
552 12 ▶ my $val = shift;
Subroutine match_zip: called CC=2 SCAR=6.9
Pod undocumented
553 12 ▶ if ($val =~ /^(\s*\d{5}(?:[-]\d{4})?\s*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^(\s*\d{5}(?:[-]\d{4})?\s*)$/) { } 6 6 12
554 6 return $1;
555 }
556 6 else { return undef; }
557 }
558  
559 =head2 phone
560  
561 This one checks if the input looks like a phone number, (if it
562 contains at least 6 digits.)
563  
564 =cut
565  
566 sub match_phone {
567 6 ▶ my $val = shift;
Subroutine match_phone: called CC=2 SCAR=6.9
Pod undocumented
568  
569 6 ▶ if ($val =~ /^((?:\D*\d\D*){6,})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^((?:\D*\d\D*){6,})$/) { } 4 2 6
570 4 return $1;
571 }
572 2 else { return undef; }
573 }
574  
575 =head2 american_phone
576  
577 This constraints checks if the number is a possible North American style
578 of phone number : (XXX) XXX-XXXX. It has to contains 7 or more digits.
579  
580 =cut
581  
582 sub match_american_phone {
583 5 ▶ my $val = shift;
Subroutine match_american_phone: called CC=2 SCAR=6.9
Pod undocumented
584  
585 5 ▶ if ($val =~ /^((?:\D*\d\D*){7,})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^((?:\D*\d\D*){7,})$/) { } 2 3 5
586 2 return $1;
587 }
588 3 else { return undef; }
589 }
590  
591  
592 =head2 cc_number
593  
594 This constraint references the value of a credit card type field.
595  
596 constraint_methods => {
597 cc_no => cc_number({fields => ['cc_type']}),
598 }
599  
600  
601 The number is checked only for plausibility, it checks if the number could
602 be valid for a type of card by checking the checksum and looking at the number
603 of digits and the number of digits of the number.
604  
605 This functions is only good at catching typos. IT DOESN'T
606 CHECK IF THERE IS AN ACCOUNT ASSOCIATED WITH THE NUMBER.
607  
608 =cut
609  
610 # This one is taken from the contributed program to
611 # MiniVend by Bruce Albrecht
612  
613 # XXX raise exception on bad/missing params?
614 sub cc_number {
615 1 ▶ my $attrs = shift;
Subroutine cc_number: called CC=6 SCAR=20.5
Pod documented
616 return undef unless $attrs && ref($attrs) eq 'HASH'
617 1 ▶ && exists $attrs->{fields} && ref($attrs->{fields}) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs and ref $attrs eq "HASH" and exists $attrs->{"fields"} and ref $attrs->{"fields"} eq "ARRAY" 0 1 1
Condition33%3/9
$attrs and ref $attrs eq "HASH" and exists $attrs->{"fields"} and ref $attrs->{"fields"} eq "ARRAY"
!ll&&!rl&&r
001
$attrs and ref $attrs eq "HASH" and exists $attrs->{"fields"}
!ll&&!rl&&r
001
$attrs and ref $attrs eq "HASH"
!ll&&!rl&&r
001
MC/DC0%0/4
$attrs and ref $attrs eq "HASH" and exists $attrs->{"fields"} and ref $attrs->{"fields"} eq "ARRAY"
$attrs ref $attrs eq "HASH" exists $attrs->{"fields"} ref $attrs->{"fields"} eq "ARRAY"
Truth table20%1/5
A and B and C and D
A= $attrsB= ref $attrs eq "HASH"C= exists $attrs->{"fields"}D= ref $attrs->{"fields"} eq "ARRAY"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
618  
619 1 my ($cc_type_field) = @{ $attrs->{fields} };
620 1 ▶ return undef unless $cc_type_field;
Branch: 1 branch
BranchTrueFalseTotal
unless $cc_type_field 0 1 1
621  
622 return sub {
623 12 ▶ my $dfv = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
624 12 my $data = $dfv->get_filtered_data;
625  
626 return match_cc_number(
627 $dfv->get_current_constraint_value,
628 12 $data->{$cc_type_field}
629 );
630 1 };
631 }
632  
633 sub match_cc_number {
634 28 ▶ my ( $the_card, $card_type ) = @_;
Subroutine match_cc_number: called CC=28 SCAR=36.6
Pod undocumented
635 28 my $orig_card = $the_card; #used for return match at bottom
636 28 my ($index, $digit, $product);
637 28 my $multiplier = 2; # multiplier is either 1 or 2
638 28 my $the_sum = 0;
639  
640 28 ▶ return undef if length($the_card) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if length $the_card == 0 0 28 28
641  
642 # check card type
643 28 ▶ return undef unless $card_type =~ /^[admv]/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $card_type =~ /^[admv]/i 0 28 28
644  
645 28 ▶ return undef if ($card_type =~ /^v/i && substr($the_card, 0, 1) ne "4") ||
Branch: 1 branch
BranchTrueFalseTotal
if $card_type =~ /^v/i and substr($the_card, 0, 1) ne 4 or $card_type =~ /^m/i and substr($the_card, 0, 1) ne 5 or $card_type =~ /^d/i and substr($the_card, 0, 4) ne 6011 or $card_type =~ /^a/i and substr($the_card, 0, 2) ne 34 and substr($the_card, 0, 2) ne 37 2 24 26
Condition79%19/24
$card_type =~ /^v/i and substr($the_card, 0, 1) ne 4 or $card_type =~ /^m/i and substr($the_card, 0, 1) ne 5 or $card_type =~ /^d/i and substr($the_card, 0, 4) ne 6011 or $card_type =~ /^a/i and substr($the_card, 0, 2) ne 34 and substr($the_card, 0, 2) ne 37
l!l&&r!l&&!r
4024
$card_type =~ /^v/i and substr($the_card, 0, 1) ne 4 or $card_type =~ /^m/i and substr($the_card, 0, 1) ne 5 or $card_type =~ /^d/i and substr($the_card, 0, 4) ne 6011
l!l&&r!l&&!r
2224
$card_type =~ /^v/i and substr($the_card, 0, 1) ne 4 or $card_type =~ /^m/i and substr($the_card, 0, 1) ne 5
l!l&&r!l&&!r
0226
$card_type =~ /^v/i and substr($the_card, 0, 1) ne 4
!ll&&!rl&&r
2080
$card_type =~ /^m/i and substr($the_card, 0, 1) ne 5
!ll&&!rl&&r
2062
$card_type =~ /^d/i and substr($the_card, 0, 4) ne 6011
!ll&&!rl&&r
2042
$card_type =~ /^a/i and substr($the_card, 0, 2) ne 34 and substr($the_card, 0, 2) ne 37
!ll&&!rl&&r
1860
$card_type =~ /^a/i and substr($the_card, 0, 2) ne 34
!ll&&!rl&&r
1806
MC/DC22%2/9
$card_type =~ /^v/i and substr($the_card, 0, 1) ne 4 or $card_type =~ /^m/i and substr($the_card, 0, 1) ne 5 or $card_type =~ /^d/i and substr($the_card, 0, 4) ne 6011 or $card_type =~ /^a/i and substr($the_card, 0, 2) ne 34 and substr($the_card, 0, 2) ne 37
$card_type =~ /^v/i substr($the_card, 0, 1) ne 4 $card_type =~ /^m/i substr($the_card, 0, 1) ne 5 $card_type =~ /^d/i substr($the_card, 0, 4) ne 6011 $card_type =~ /^a/i substr($the_card, 0, 2) ne 34 substr($the_card, 0, 2) ne 37
Truth table7%3/39
A and B or C and D or E and F or G and H and I
A= $card_type =~ /^v/iB= substr($the_card, 0, 1) ne 4C= $card_type =~ /^m/iD= substr($the_card, 0, 1) ne 5E= $card_type =~ /^d/iF= substr($the_card, 0, 4) ne 6011G= $card_type =~ /^a/iH= substr($the_card, 0, 2) ne 34I= substr($the_card, 0, 2) ne 37
ABCDEFGHIresult
11XXXXXXX1
0X11XXXXX1
1011XXXXX1
0X0X11XXX1
0X1011XXX1
100X11XXX1
101011XXX1
0X0X0X1111
0X0X101111
0X100X1111
0X10101111
100X0X1111
100X101111
10100X1111
1010101111
0X0X0X0XX0
0X0X0X10X0
0X0X0X1100
0X0X100XX0
0X0X1010X0
0X0X101100
0X100X0XX0
0X100X10X0
0X100X1100
0X10100XX0
0X101010X0
0X10101100
100X0X0XX0
100X0X10X0
100X0X1100
100X100XX0
100X1010X0
100X101100
10100X0XX0
10100X10X0
10100X1100
1010100XX0
10101010X0
1010101100
646 ($card_type =~ /^m/i && substr($the_card, 0, 1) ne "5") ||
647 ($card_type =~ /^d/i && substr($the_card, 0, 4) ne "6011") ||
648 ($card_type =~ /^a/i && substr($the_card, 0, 2) ne "34" &&
649 substr($the_card, 0, 2) ne "37");
650  
651 # check for valid number of digits.
652 24 $the_card =~ s/\s//g; # strip out spaces
653 24 ▶ return undef if $the_card !~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $the_card =~ /^\d+$/ 0 24 24
654  
655 24 $digit = substr($the_card, 0, 1);
656 24 $index = length($the_card)-1;
657 24 ▶ return undef if ($digit == 3 && $index != 14) ||
Branch: 1 branch
BranchTrueFalseTotal
if $digit == 3 and $index != 14 or $digit == 4 and $index != 12 and $index != 15 or $digit == 5 and $index != 15 or $digit == 6 and $index != 13 and $index != 15 0 24 24
Condition55%15/27
$digit == 3 and $index != 14 or $digit == 4 and $index != 12 and $index != 15 or $digit == 5 and $index != 15 or $digit == 6 and $index != 13 and $index != 15
l!l&&r!l&&!r
0024
$digit == 3 and $index != 14 or $digit == 4 and $index != 12 and $index != 15 or $digit == 5 and $index != 15
l!l&&r!l&&!r
0024
$digit == 3 and $index != 14 or $digit == 4 and $index != 12 and $index != 15
l!l&&r!l&&!r
0024
$digit == 3 and $index != 14
!ll&&!rl&&r
1860
$digit == 4 and $index != 12 and $index != 15
!ll&&!rl&&r
1680
$digit == 4 and $index != 12
!ll&&!rl&&r
1608
$digit == 5 and $index != 15
!ll&&!rl&&r
1860
$digit == 6 and $index != 13 and $index != 15
!ll&&!rl&&r
2040
$digit == 6 and $index != 13
!ll&&!rl&&r
2004
MC/DC0%0/10
$digit == 3 and $index != 14 or $digit == 4 and $index != 12 and $index != 15 or $digit == 5 and $index != 15 or $digit == 6 and $index != 13 and $index != 15
$digit == 3 $index != 14 $digit == 4 $index != 12 $index != 15 $digit == 5 $index != 15 $digit == 6 $index != 13 $index != 15
Truth table1%1/57
A and B or C and D and E or F and G or H and I and J
A= $digit == 3B= $index != 14C= $digit == 4D= $index != 12E= $index != 15F= $digit == 5G= $index != 15H= $digit == 6I= $index != 13J= $index != 15
ABCDEFGHIJresult
11XXXXXXXX1
0X111XXXXX1
10111XXXXX1
0X0XX11XXX1
0X10X11XXX1
0X11011XXX1
100XX11XXX1
1010X11XXX1
1011011XXX1
0X0XX0X1111
0X0XX101111
0X10X0X1111
0X10X101111
0X1100X1111
0X110101111
100XX0X1111
100XX101111
1010X0X1111
1010X101111
101100X1111
10110101111
0X0XX0X0XX0
0X0XX0X10X0
0X0XX0X1100
0X0XX100XX0
0X0XX1010X0
0X0XX101100
0X10X0X0XX0
0X10X0X10X0
0X10X0X1100
0X10X100XX0
0X10X1010X0
0X10X101100
0X1100X0XX0
0X1100X10X0
0X1100X1100
0X110100XX0
0X1101010X0
0X110101100
100XX0X0XX0
100XX0X10X0
100XX0X1100
100XX100XX0
100XX1010X0
100XX101100
1010X0X0XX0
1010X0X10X0
1010X0X1100
1010X100XX0
1010X1010X0
1010X101100
101100X0XX0
101100X10X0
101100X1100
10110100XX0
101101010X0
10110101100
658 ($digit == 4 && $index != 12 && $index != 15) ||
659 ($digit == 5 && $index != 15) ||
660 ($digit == 6 && $index != 13 && $index != 15);
661  
662  
663 # calculate checksum.
664 24 ▶ for ($index--; $index >= 0; $index --)
Branch: 1 branch
BranchTrueFalseTotal
if ($index >= 0) 354 24 378
665 {
666 354 $digit=substr($the_card, $index, 1);
667 354 $product = $multiplier * $digit;
668 354 ▶ $the_sum += $product > 9 ? $product - 9 : $product;
Branch: 1 branch
BranchTrueFalseTotal
$product > 9 ? : 50 304 354
669 354 $multiplier = 3 - $multiplier;
670 }
671 24 $the_sum %= 10;
672 24 ▶ $the_sum = 10 - $the_sum if $the_sum;
Branch: 1 branch
BranchTrueFalseTotal
if $the_sum 22 2 24
673  
674 # return whether checksum matched.
675 24 ▶ if ($the_sum == substr($the_card, -1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($the_sum == substr($the_card, -1)) { } 18 6 24
676 18 ▶ if ($orig_card =~ /^([\d\s]*)$/) { return $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($orig_card =~ /^([\d\s]*)$/) { } 18 0 18
677 0 else { return undef; }
678 }
679 else {
680 6 return undef;
681 }
682 }
683  
684 =head2 cc_exp
685  
686 This one checks if the input is in the format MM/YY or MM/YYYY and if
687 the MM part is a valid month (1-12) and if that date is not in the past.
688  
689 =cut
690  
691 sub match_cc_exp {
692 4 ▶ my $val = shift;
Subroutine match_cc_exp: called CC=10 SCAR=25.9
Pod undocumented
693 4 my ($matched_month, $matched_year);
694  
695 4 my ($month, $year) = split('/', $val);
696 4 ▶ return undef if $month !~ /^(\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $month =~ /^(\d+)$/ 2 2 4
697 2 $matched_month = $1;
698  
699 2 ▶ return undef if $year !~ /^(\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $year =~ /^(\d+)$/ 0 2 2
700 2 $matched_year = $1;
701  
702 2 ▶ return undef if $month <1 || $month > 12;
Branch: 1 branch
BranchTrueFalseTotal
if $month < 1 or $month > 12 0 2 2
Condition33%1/3
$month < 1 or $month > 12
l!l&&r!l&&!r
002
MC/DC0%0/2
$month < 1 or $month > 12
$month < 1 $month > 12
Truth table33%1/3
A or B
A= $month < 1B= $month > 12
ABresult
1X1
011
000
703 2 ▶ $year += ($year < 70) ? 2000 : 1900 if $year < 1900;
Branch: 2 branches
BranchTrueFalseTotal
if $year < 1900 2 0 2
$year < 70 ? : 2 0 2
704 2 my @now=localtime();
705 2 $now[5] += 1900;
706 2 ▶ return undef if ($year < $now[5]) || ($year == $now[5] && $month <= $now[4]);
Branch: 1 branch
BranchTrueFalseTotal
if $year < $now[5] or $year == $now[5] and $month <= $now[4] 0 2 2
Condition33%2/6
$year < $now[5] or $year == $now[5] and $month <= $now[4]
l!l&&r!l&&!r
002
$year == $now[5] and $month <= $now[4]
!ll&&!rl&&r
200
MC/DC0%0/3
$year < $now[5] or $year == $now[5] and $month <= $now[4]
$year < $now[5] $year == $now[5] $month <= $now[4]
Truth table0%0/4
A or B and C
A= $year < $now[5]B= $year == $now[5]C= $month <= $now[4]
ABCresult
1XX1
0111
00X0
0100
707  
708 2 return "$matched_month/$matched_year";
709 }
710  
711 =head2 cc_type
712  
713 This one checks if the input field starts by M(asterCard), V(isa),
714 A(merican express) or D(iscovery).
715  
716 =cut
717  
718 sub match_cc_type {
719 4 ▶ my $val = shift;
Subroutine match_cc_type: called CC=2 SCAR=6.9
Pod undocumented
720 4 ▶ if ($val =~ /^([MVAD].*)$/i) { return $1; }
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^([MVAD].*)$/i) { } 2 2 4
721 2 else { return undef; }
722 }
723  
724 =head2 ip_address
725  
726 This checks if the input is formatted like a dotted decimal IP address (v4).
727 For other kinds of IP address method, See L<Regexp::Common::net> which provides
728 several more options. L<REGEXP::COMMON SUPPORT> explains how we easily integrate
729 with Regexp::Common.
730  
731 =cut
732  
733 # contributed by Juan Jose Natera Abreu <jnatera@net-uno.net>
734  
735 sub match_ip_address {
736 6 ▶ my $val = shift;
Subroutine match_ip_address: called CC=10 SCAR=25.9
Pod undocumented
737 6 ▶ if ($val =~ m/^((\d+)\.(\d+)\.(\d+)\.(\d+))$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^((\d+)\.(\d+)\.(\d+)\.(\d+))$/) { } 4 2 6
738 4 ▶ if
Branch: 1 branch
BranchTrueFalseTotal
if ($2 >= 0 and $2 <= 255 and ($3 >= 0 and $3 <= 255) and ($4 >= 0 and $4 <= 255) and ($5 >= 0 and $5 <= 255)) { } 3 1 4
Condition52%11/21
$2 >= 0 and $2 <= 255 and ($3 >= 0 and $3 <= 255) and ($4 >= 0 and $4 <= 255) and ($5 >= 0 and $5 <= 255)
!ll&&!rl&&r
103
$2 >= 0 and $2 <= 255 and ($3 >= 0 and $3 <= 255) and ($4 >= 0 and $4 <= 255)
!ll&&!rl&&r
103
$2 >= 0 and $2 <= 255 and ($3 >= 0 and $3 <= 255)
!ll&&!rl&&r
103
$2 >= 0 and $2 <= 255
!ll&&!rl&&r
013
$3 >= 0 and $3 <= 255
!ll&&!rl&&r
003
$4 >= 0 and $4 <= 255
!ll&&!rl&&r
003
$5 >= 0 and $5 <= 255
!ll&&!rl&&r
003
MC/DC12%1/8
$2 >= 0 and $2 <= 255 and ($3 >= 0 and $3 <= 255) and ($4 >= 0 and $4 <= 255) and ($5 >= 0 and $5 <= 255)
$2 >= 0 $2 <= 255 $3 >= 0 $3 <= 255 $4 >= 0 $4 <= 255 $5 >= 0 $5 <= 255
Truth table22%2/9
A and B and C and D and E and F and G and H
A= $2 >= 0B= $2 <= 255C= $3 >= 0D= $3 <= 255E= $4 >= 0F= $4 <= 255G= $5 >= 0H= $5 <= 255
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
1110XXXX0
11110XXX0
111110XX0
1111110X0
111111100
111111111
739 (($2 >= 0 && $2 <= 255) && ($3 >= 0 && $3 <= 255) && ($4 >= 0 && $4 <= 255) && ($5 >= 0 && $5 <= 255)) {
740 3 return $1;
741 }
742 1 else { return undef; }
743 }
744 2 else { return undef; }
745 }
746  
747  
748 59 1;
749  
750 __END__
« blib/lib/Data/FormValidator/Constraints/Upload.pm ↑ Summary blib/lib/Data/FormValidator/Results.pm »