blib/lib/Data/FormValidator/Results.pm

filter: Statementstmt 84.3% 453 / 537 Branchbran 72.8% 220 / 302 Conditioncond 74.1% 89 / 120 MC/DCmcdc 56.5% 39 / 69 Subroutinesub 95.5% 43 / 45 Podpod 0.0% 0 / 20 totaltotal 77.2% 844 / 1093 CC 248 SCAR 53.3
CC
248
Cov
79%
_process
485.4
meta
30.0
_get_input_as_hash
25.5
CRAP 206.8
↑ Summary blib/lib/Data/FormValidator/Constraints/Upload.pm »
1 #
2 # Results.pm - Object which contains validation result.
3 #
4 # This file is part of FormValidator.
5 #
6 # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM>
7 # Maintainer: Mark Stosberg <mark@summersault.com>
8 #
9 # Copyright (C) 2000 iNsu Innovations Inc.
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms same terms as perl itself.
13 #
14 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 package Data::FormValidator::Results;
17 63 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 63 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 63 ▶use Data::FormValidator::Filters ':filters';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 62 ▶use Data::FormValidator::Constraints qw(:validators :matchers);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 use overload
22 62 ▶ 'bool' => \&_bool_overload_based_on_success,
Subroutine BEGIN: called CC=1 SCAR=0.0
23 62 fallback => 1;
24  
25 59 our $VERSION = 4.88;
26  
27 =pod
28  
29 =head1 NAME
30  
31 Data::FormValidator::Results - results of form input validation.
32  
33 =head1 SYNOPSIS
34  
35 my $results = Data::FormValidator->check(\%input_hash, \%dfv_profile);
36  
37 # Print the name of missing fields
38 if ( $results->has_missing ) {
39 for my $f ( $results->missing ) {
40 print $f, " is missing\n";
41 }
42 }
43  
44 # Print the name of invalid fields
45 if ( $results->has_invalid ) {
46 for my $f ( $results->invalid ) {
47 print $f, " is invalid: ", $results->invalid( $f ), "\n";
48 }
49 }
50  
51 # Print unknown fields
52 if ( $results->has_unknown ) {
53 for my $f ( $results->unknown ) {
54 print $f, " is unknown\n";
55 }
56 }
57  
58 # Print valid fields
59 for my $f ( $results->valid() ) {
60 print $f, " = ", $results->valid( $f ), "\n";
61 }
62  
63 =head1 DESCRIPTION
64  
65 This object is returned by the L<Data::FormValidator> C<check> method.
66 It can be queried for information about the validation results.
67  
68 =cut
69  
70 127 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
71 127 my $proto = shift;
72 127 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
01270
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
73 127 my ($profile, $data) = @_;
74  
75 127 my $self = bless {}, $class;
76  
77 127 $self->_process( $profile, $data );
78  
79 122 $self;
80 }
81  
82 127 ▶sub _process {
Subroutine _process: called CC=125 SCAR=61.8
83 127 my ($self, $profile, $data) = @_;
84  
85 # Copy data and assumes that all is valid to start with
86  
87 127 my %data = $self->_get_input_as_hash($data);
88 127 my %valid = %data;
89 127 my @missings = ();
90 127 my @unknown = ();
91  
92 # msgs() method will need access to the profile
93 127 $self->{profile} = $profile;
94  
95 127 my %imported_validators;
96  
97 # import valid_* subs from requested packages
98 127 for my $package (_arrayify($profile->{validator_packages})) {
99 6 ▶ if ( !exists $imported_validators{$package} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $imported_validators{$package}) 6 0 6
100 6 local $SIG{__DIE__} = \&confess;
101 6 eval "require $package";
102 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
103 0 die "Couldn't load validator package '$package': $@";
104 }
105  
106 # Perl will die with a nice error message if the package can't be found
107 # No need to go through extra effort here. -mls :)
108 6 my $package_ref = qualify_to_ref("${package}::");
109 my @subs = grep(/^(valid_|match_|filter_)/,
110 6 keys(%{*{$package_ref}}));
111 6 for my $sub (@subs) {
112 # is it a sub? (i.e. make sure it's not a scalar, hash, etc.)
113 53 my $subref = *{qualify_to_ref("${package}::$sub")}{CODE};
114 53 ▶ if (defined $subref) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $subref) 53 0 53
115 53 *{qualify_to_ref($sub)} = $subref;
116 }
117 }
118 6 $imported_validators{$package} = 1;
119 }
120 }
121  
122 # Apply unconditional filters
123 127 for my $filter (_arrayify($profile->{filters})) {
124 7 ▶ if (defined $filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter) 7 0 7
125 # Qualify symbolic references
126 7 ▶ $filter = (ref $filter eq 'CODE' ? $filter : *{qualify_to_ref("filter_$filter")}{CODE}) ||
Branch: 1 branch
BranchTrueFalseTotal
ref $filter eq 'CODE' ? : 1 6 7
Condition100%2/2
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'}) || die("No filter found named: '${filter}'")
l!l
61
MC/DC100%1/1
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'}) || die("No filter found named: '${filter}'")
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'})
Truth table100%2/2
A
A= (ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'})
Aresult
11
00
127 die "No filter found named: '$filter'";
128 6 for my $field ( keys %valid ) {
129 # apply filter, modifying %valid by reference, skipping undefined values
130 13 _filter_apply(\%valid,$field,$filter);
131 }
132 }
133 }
134  
135 # Apply specific filters
136 126 ▶ while ( my ($field,$filters) = each %{$profile->{field_filters} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$profile{"field_filters"};}) 7 125 132
137 7 for my $filter ( _arrayify($filters)) {
138 7 ▶ if (defined $filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter) 7 0 7
139 # Qualify symbolic references
140 7 ▶ $filter = (ref $filter eq 'CODE' ? $filter : *{qualify_to_ref("filter_$filter")}{CODE}) ||
Branch: 1 branch
BranchTrueFalseTotal
ref $filter eq 'CODE' ? : 2 5 7
Condition100%2/2
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'}) || die("No filter found named '${filter}'")
l!l
61
MC/DC100%1/1
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'}) || die("No filter found named '${filter}'")
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'})
Truth table100%2/2
A
A= (ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'})
Aresult
11
00
141 die "No filter found named '$filter'";
142  
143 # apply filter, modifying %valid by reference
144 6 _filter_apply(\%valid,$field,$filter);
145 }
146 }
147 }
148  
149 # add in specific filters from the regexp map
150 125 ▶ while ( my ($re,$filters) = each %{$profile->{field_filter_regexp_map} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$profile{"field_filter_regexp_map"};}) 6 124 130
151 6 my $sub = _create_sub_from_RE($re);
152  
153 6 for my $filter ( _arrayify($filters)) {
154 6 ▶ if (defined $filter) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filter) 6 0 6
155 # Qualify symbolic references
156 6 ▶ $filter = (ref $filter eq 'CODE' ? $filter : *{qualify_to_ref("filter_$filter")}{CODE}) ||
Branch: 1 branch
BranchTrueFalseTotal
ref $filter eq 'CODE' ? : 0 6 6
Condition100%2/2
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'}) || die("No filter found named '${filter}'")
l!l
51
MC/DC100%1/1
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'}) || die("No filter found named '${filter}'")
(ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'})
Truth table100%2/2
A
A= (ref $filter eq 'CODE' ? $filter : *{&qualify_to_ref("filter_$filter");}{'CODE'})
Aresult
11
00
157 die "No filter found named '$filter'";
158  
159 59 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
160  
161 # find all the keys that match this RE and apply filters to them
162 5 for my $field (grep { $sub->($_) } (keys %valid)) {
163 # apply filter, modifying %valid by reference
164 7 _filter_apply(\%valid,$field,$filter);
165 }
166 }
167 }
168 }
169  
170 # store the filtered data away for later use
171 124 $self->{__FILTERED_DATA} = \%valid;
172  
173 124 my %required = map { $_ => 1 } _arrayify($profile->{required});
174 124 my %optional = map { $_ => 1 } _arrayify($profile->{optional});
175  
176 # loop through and add fields to %required and %optional based on regular expressions
177 124 my $required_re = _create_sub_from_RE($profile->{required_regexp});
178 124 my $optional_re = _create_sub_from_RE($profile->{optional_regexp});
179  
180 124 for my $k (keys %valid) {
181 256 ▶ if ($required_re && $required_re->($k)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($required_re and &$required_re($k)) 1 255 256
Condition100%3/3
$required_re and &$required_re($k)
!ll&&!rl&&r
24961
MC/DC100%2/2
$required_re and &$required_re($k)
$required_re &$required_re($k)
Truth table100%3/3
A and B
A= $required_reB= &$required_re($k)
ABresult
0X0
100
111
182 1 $required{$k} = 1;
183 }
184  
185 256 ▶ if ($optional_re && $optional_re->($k)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($optional_re and &$optional_re($k)) 6 250 256
Condition100%3/3
$optional_re and &$optional_re($k)
!ll&&!rl&&r
24556
MC/DC100%2/2
$optional_re and &$optional_re($k)
$optional_re &$optional_re($k)
Truth table100%3/3
A and B
A= $optional_reB= &$optional_re($k)
ABresult
0X0
100
111
186 6 $optional{$k} = 1;
187 }
188 }
189  
190 # handle "require_some"
191 124 ▶ while (my ($field, $dependent_require_some) = each %{$profile->{dependent_require_some}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$profile{"dependent_require_some"};}) 0 124 124
192 0 ▶ if (defined $valid{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valid{$field}) 0 0 0
193 0 ▶ if (ref $dependent_require_some eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dependent_require_some eq 'CODE') { } 0 0 0
194 0 for my $value (_arrayify($valid{$field})) {
195 0 my $returned_require_some = $dependent_require_some->($self, $value);
196  
197 0 ▶ if (ref($returned_require_some) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $returned_require_some eq "HASH") 0 0 0
198 0 foreach my $key (keys %$returned_require_some) {
199 0 $profile->{require_some}->{$key} = $returned_require_some->{$key};
200 }
201 }
202 }
203 } else {
204 0 ▶ if (ref($dependent_require_some) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dependent_require_some eq "HASH") 0 0 0
205 0 foreach my $key (keys %$dependent_require_some) {
206 0 $profile->{require_some}->{$key} = $dependent_require_some->{$key};
207 }
208 }
209 }
210 }
211 }
212  
213 124 my %require_some;
214 124 ▶ while ( my ( $field, $deps) = each %{$profile->{require_some}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$profile{"require_some"};}) 3 124 127
215 3 for my $dep (_arrayify($deps)){
216 11 $require_some{$dep} = 1;
217 }
218 }
219  
220  
221 # Remove all empty fields
222 124 for my $field (keys %valid) {
223 256 ▶ if (ref $valid{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $valid{$field}) { } 20 236 256
224 20 ▶ if ( ref $valid{$field} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $valid{$field} eq "ARRAY") 18 2 20
225 18 ▶ for (my $i = 0; $i < scalar @{ $valid{$field} }; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$valid{$field};}) 38 18 56
226 38 ▶ $valid{$field}->[$i] = undef unless (defined $valid{$field}->[$i] and length $valid{$field}->[$i] and $valid{$field}->[$i] !~ /^\x00$/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $valid{$field}[$i] and length $valid{$field}[$i] and not $valid{$field}[$i] =~ /^\x00$/ 4 34 38
Condition83%5/6
defined $valid{$field}[$i] and length $valid{$field}[$i] and not $valid{$field}[$i] =~ /^\x00$/
!ll&&!rl&&r
4034
defined $valid{$field}[$i] and length $valid{$field}[$i]
!ll&&!rl&&r
3134
MC/DC66%2/3
defined $valid{$field}[$i] and length $valid{$field}[$i] and not $valid{$field}[$i] =~ /^\x00$/
defined $valid{$field}[$i] length $valid{$field}[$i] not $valid{$field}[$i] =~ /^\x00$/
Truth table75%3/4
A and B and not(C)
A= defined $valid{$field}[$i]B= length $valid{$field}[$i]C= not $valid{$field}[$i] =~ /^\x00$/
ABCresult
0XX0
10X0
1100
1111
227 }
228 # If all fields are empty, we delete it.
229 18 ▶ delete $valid{$field} unless grep { defined $_ } @{$valid{$field}};
Branch: 1 branch
BranchTrueFalseTotal
unless grep {defined $_;} @{$valid{$field};} 2 16 18
230  
231 }
232 }
233 else {
234 236 ▶ delete $valid{$field} unless (defined $valid{$field} and length $valid{$field} and $valid{$field} !~ /^\x00$/);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $valid{$field} and length $valid{$field} and not $valid{$field} =~ /^\x00$/ 3 233 236
Condition83%5/6
defined $valid{$field} and length $valid{$field} and not $valid{$field} =~ /^\x00$/
!ll&&!rl&&r
30233
defined $valid{$field} and length $valid{$field}
!ll&&!rl&&r
21233
MC/DC66%2/3
defined $valid{$field} and length $valid{$field} and not $valid{$field} =~ /^\x00$/
defined $valid{$field} length $valid{$field} not $valid{$field} =~ /^\x00$/
Truth table75%3/4
A and B and not(C)
A= defined $valid{$field}B= length $valid{$field}C= not $valid{$field} =~ /^\x00$/
ABCresult
0XX0
10X0
1100
1111
235 }
236 }
237  
238 # Check if the presence of some fields makes other optional fields required.
239 124 ▶ while ( my ( $field, $deps) = each %{$profile->{dependencies}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$profile{"dependencies"};}) 15 124 139
240 15 ▶ if (defined $valid{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valid{$field}) 11 4 15
241 11 ▶ if (ref($deps) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $deps eq 'HASH') { } 6 5 11
elsif (ref $deps eq 'CODE') { } 4 1 5
242 6 for my $key (keys %$deps) {
243 # Handle case of a key with a single value given as an arrayref
244 # There is probably a better, more general solution to this problem.
245 12 my $val_to_compare;
246 12 ▶ if ((ref $valid{$field} eq 'ARRAY') and (scalar @{ $valid{$field} } == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $valid{$field} eq 'ARRAY' and scalar @{$valid{$field};} == 1) { } 2 10 12
Condition66%2/3
ref $valid{$field} eq "ARRAY" and scalar @{$valid{$field};} == 1
!ll&&!rl&&r
1002
MC/DC50%1/2
ref $valid{$field} eq "ARRAY" and scalar @{$valid{$field};} == 1
ref $valid{$field} eq "ARRAY" scalar @{$valid{$field};} == 1
Truth table66%2/3
A and B
A= ref $valid{$field} eq "ARRAY"B= scalar @{$valid{$field};} == 1
ABresult
0X0
100
111
247 2 $val_to_compare = $valid{$field}->[0];
248 }
249 else {
250 10 $val_to_compare = $valid{$field}
251 }
252  
253 12 ▶ if($val_to_compare eq $key){
Branch: 1 branch
BranchTrueFalseTotal
if ($val_to_compare eq $key) 6 6 12
254 6 for my $dep (_arrayify($deps->{$key})){
255 11 $required{$dep} = 1;
256 }
257 }
258 }
259 }
260 elsif (ref $deps eq "CODE") {
261 4 for my $val (_arrayify($valid{$field})) {
262 5 my $returned_deps = $deps->($self, $val);
263  
264 5 for my $dep (_arrayify($returned_deps)) {
265 2 $required{$dep} = 1;
266 }
267 }
268 }
269 else {
270 1 for my $dep (_arrayify($deps)){
271 2 $required{$dep} = 1;
272 }
273 }
274 }
275 }
276  
277 # check dependency groups
278 # the presence of any member makes them all required
279 124 for my $group (values %{ $profile->{dependency_groups} }) {
280 0 my $require_all = 0;
281 0 for my $field (_arrayify($group)) {
282 0 ▶ $require_all = 1 if $valid{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $valid{$field} 0 0 0
283 }
284 0 ▶ if ($require_all) {
Branch: 1 branch
BranchTrueFalseTotal
if ($require_all) 0 0 0
285 0 map { $required{$_} = 1 } _arrayify($group);
286 }
287 }
288  
289 124 my $dependency_re;
290  
291 124 foreach my $re (keys %{$profile->{dependencies_regexp}}) {
292 0 my $sub = _create_sub_from_RE($re);
293  
294 $dependency_re->{$re} = {
295 sub => $sub,
296 0 value => $profile->{dependencies_regexp}->{$re},
297 };
298 }
299  
300 124 ▶ if ($dependency_re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dependency_re) 0 124 124
301 0 foreach my $k (keys %valid) {
302 0 foreach my $re (keys %$dependency_re) {
303 0 ▶ if ($dependency_re->{$re}->{sub}->($k)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dependency_re->{$re}{"sub"}->($k)) 0 0 0
304 0 my $deps = $dependency_re->{$re}->{value};
305  
306 0 ▶ if (ref($deps) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $deps eq 'HASH') { } 0 0 0
elsif (ref $deps eq 'CODE') { } 0 0 0
307 0 for my $key (keys %$deps) {
308 # Handle case of a key with a single value given as an arrayref
309 # There is probably a better, more general solution to this problem.
310 0 my $val_to_compare;
311  
312 0 ▶ if ((ref $valid{$k} eq 'ARRAY') and (scalar @{ $valid{$k} } == 1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $valid{$k} eq 'ARRAY' and scalar @{$valid{$k};} == 1) { } 0 0 0
Condition0%0/3
ref $valid{$k} eq "ARRAY" and scalar @{$valid{$k};} == 1
!ll&&!rl&&r
000
MC/DC0%0/2
ref $valid{$k} eq "ARRAY" and scalar @{$valid{$k};} == 1
ref $valid{$k} eq "ARRAY" scalar @{$valid{$k};} == 1
Truth table0%0/3
A and B
A= ref $valid{$k} eq "ARRAY"B= scalar @{$valid{$k};} == 1
ABresult
0X0
100
111
313 0 $val_to_compare = $valid{$k}->[0];
314 } else {
315 0 $val_to_compare = $valid{$k}
316 }
317  
318 0 ▶ if($val_to_compare eq $key){
Branch: 1 branch
BranchTrueFalseTotal
if ($val_to_compare eq $key) 0 0 0
319 0 for my $dep (_arrayify($deps->{$key})){
320 0 $required{$dep} = 1;
321 }
322 }
323 }
324 } elsif (ref $deps eq "CODE") {
325 0 for my $val (_arrayify($valid{$k})) {
326 0 my $returned_deps = $deps->($self, $val, $k);
327  
328 0 for my $dep (_arrayify($returned_deps)) {
329 0 $required{$dep} = 1;
330 }
331 }
332 } else {
333 0 for my $dep (_arrayify($deps)){
334 0 $required{$dep} = 1;
335 }
336 }
337 }
338 }
339 }
340 }
341  
342 # Check if the presence of some fields makes other fields optional.
343 124 ▶ while (my ($field, $dependent_optional) = each %{$profile->{dependent_optionals}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$profile{"dependent_optionals"};}) 0 124 124
344 0 ▶ if (defined $valid{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $valid{$field}) 0 0 0
345 0 ▶ if (ref $dependent_optional eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dependent_optional eq 'CODE') { } 0 0 0
346 0 for my $value (_arrayify($valid{$field})) {
347 0 my $returned_optionals = $dependent_optional->($self, $value);
348  
349  
350  
351 0 foreach my $optional (_arrayify($returned_optionals)) {
352 0 $optional{$optional} = 1;
353 }
354 }
355 } else {
356 0 foreach my $optional (_arrayify($dependent_optional)){
357 0 $optional{$optional} = 1;
358 }
359 }
360 }
361 }
362  
363 # Find unknown
364 @unknown =
365 124 ▶ grep { not (exists $optional{$_} or exists $required{$_} or exists $require_some{$_} ) } keys %valid;
Condition100%6/6
exists $optional{$_} || exists $required{$_} || exists $require_some{$_}
l!l&&r!l&&!r
202346
exists $optional{$_} or exists $required{$_}
l!l&&r!l&&!r
2018249
MC/DC100%3/3
exists $optional{$_} || exists $required{$_} || exists $require_some{$_}
exists $optional{$_} exists $required{$_} exists $require_some{$_}
Truth table100%4/4
A or B || C
A= exists $optional{$_}B= exists $required{$_}C= exists $require_some{$_}
ABCresult
1XX1
01X1
0011
0000
366 # and remove them from the list
367 124 for my $field ( @unknown ) {
368 46 delete $valid{$field};
369 }
370  
371 # Add defaults from defaults_regexp_map
372 124 my %private_defaults;
373 124 my @all_possible = keys %optional, keys %required, keys %require_some;
374 124 ▶ while ( my ($re,$value) = each %{$profile->{defaults_regexp_map}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$profile{"defaults_regexp_map"};}) 1 124 125
375 # We only add defaults for known fields.
376 1 for (@all_possible) {
377 3 ▶ $private_defaults{$_} = $value if m/$re/;
Branch: 1 branch
BranchTrueFalseTotal
if /$re/ 2 1 3
378 }
379 }
380  
381 # Fill defaults
382 my %combined_defaults = (
383 %private_defaults,
384 124 ▶ %{ $profile->{defaults} || {} }
Condition100%2/2
$profile->{'defaults'} || {}
l!l
1123
MC/DC100%1/1
$profile->{'defaults'} || {}
$profile->{'defaults'}
Truth table100%2/2
A
A= $profile->{'defaults'}
Aresult
11
00
385 );
386 124 ▶ while ( my ($field,$value) = each %combined_defaults ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %combined_defaults) 3 124 127
387 3 ▶ unless(exists $valid{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $valid{$field}) 3 0 3
388 3 ▶ if (ref($value) && ref($value) eq "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value and ref $value eq 'CODE') { } 1 2 3
Condition66%2/3
ref $value and ref $value eq "CODE"
!ll&&!rl&&r
201
MC/DC50%1/2
ref $value and ref $value eq "CODE"
ref $value ref $value eq "CODE"
Truth table66%2/3
A and B
A= ref $valueB= ref $value eq "CODE"
ABresult
0X0
100
111
389 1 $valid{$field} = $value->($self);
390 } else {
391 2 $valid{$field} = $value;
392 }
393 }
394 }
395  
396 # Check for required fields
397 124 for my $field ( keys %required ) {
398 212 ▶ push @missings, $field unless exists $valid{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid{$field} 29 183 212
399 }
400  
401 # Check for the absence of require_some fields
402 124 ▶ while ( my ( $field, $deps) = each %{$profile->{require_some}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$profile{"require_some"};}) 3 124 127
403 3 my $enough_required_fields = 0;
404 3 my @deps = _arrayify($deps);
405 # num fields to require is first element in array if looks like a digit, 1 otherwise.
406 3 ▶ my $num_fields_to_require = ($deps[0] =~ m/^\d+$/) ? $deps[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
$deps[0] =~ /^\d+$/ ? : 2 1 3
407 3 for my $dep (@deps){
408 11 ▶ $enough_required_fields++ if exists $valid{$dep};
Branch: 1 branch
BranchTrueFalseTotal
if exists $valid{$dep} 3 8 11
409 }
410 3 ▶ push @missings, $field unless ($enough_required_fields >= $num_fields_to_require);
Branch: 1 branch
BranchTrueFalseTotal
unless $enough_required_fields >= $num_fields_to_require 1 2 3
411 }
412  
413 # add in the constraints from the regexp maps
414 # We don't want to modify the profile, so we use a new variable.
415 124 ▶ $profile->{constraints} ||= {};
Condition100%2/2
$profile->{"constraints"} ||= {}
l!l
6856
MC/DC100%1/1
$profile->{"constraints"} ||= {}
$profile->{"constraints"}
Truth table100%2/2
A
A= $profile->{"constraints"}
Aresult
11
00
416 my $private_constraints = {
417 124 %{ $profile->{constraints} },
418 _add_constraints_from_map($profile,'constraint',\%valid),
419 };
420 124 ▶ $profile->{constraint_methods} ||= {};
Condition100%2/2
$profile->{"constraint_methods"} ||= {}
l!l
5767
MC/DC100%1/1
$profile->{"constraint_methods"} ||= {}
$profile->{"constraint_methods"}
Truth table100%2/2
A
A= $profile->{"constraint_methods"}
Aresult
11
00
421 my $private_constraint_methods = {
422 124 %{ $profile->{constraint_methods} },
423 _add_constraints_from_map($profile,'constraint_method',\%valid),
424 };
425  
426 #Decide which fields to untaint
427 124 my ($untaint_all, %untaint_hash);
428 124 ▶ if (defined $profile->{untaint_regexp_map} or defined $profile->{untaint_constraint_fields} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $profile->{'untaint_regexp_map'} or defined $profile->{'untaint_constraint_fields'}) { } 3 121 124
elsif (defined $profile->{'untaint_all_constraints'} and $profile->{'untaint_all_constraints'} == 1) { } 10 222 232
Condition66%2/3
defined $profile->{"untaint_regexp_map"} or defined $profile->{"untaint_constraint_fields"}
l!l&&r!l&&!r
03121
MC/DC50%1/2
defined $profile->{"untaint_regexp_map"} or defined $profile->{"untaint_constraint_fields"}
defined $profile->{"untaint_regexp_map"} defined $profile->{"untaint_constraint_fields"}
Truth table66%2/3
A or B
A= defined $profile->{"untaint_regexp_map"}B= defined $profile->{"untaint_constraint_fields"}
ABresult
1X1
011
000
429 # first deal with untaint_constraint_fields
430 3 ▶ if (defined($profile->{untaint_constraint_fields})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $profile->{"untaint_constraint_fields"}) 3 0 3
431 3 ▶ if (ref $profile->{untaint_constraint_fields} eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $profile->{'untaint_constraint_fields'} eq 'ARRAY') { } 3 0 3
elsif ($valid{$profile->{'untaint_constraint_fields'}}) { } 0 0 0
432 3 for my $field (@{$profile->{untaint_constraint_fields}}) {
433 3 $untaint_hash{$field} = 1;
434 }
435 }
436 elsif ($valid{$profile->{untaint_constraint_fields}}) {
437 0 $untaint_hash{$profile->{untaint_constraint_fields}} = 1;
438 }
439 }
440  
441 # now look at untaint_regexp_map
442 3 ▶ if(defined($profile->{untaint_regexp_map})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $profile->{"untaint_regexp_map"}) 0 3 3
443 0 my @untaint_regexes;
444 0 ▶ if(ref $profile->{untaint_regexp_map} eq "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $profile->{'untaint_regexp_map'} eq 'ARRAY') { } 0 0 0
445 0 @untaint_regexes = @{$profile->{untaint_regexp_map}};
446 }
447 else {
448 0 push(@untaint_regexes, $profile->{untaint_regexp_map});
449 }
450  
451 0 for my $regex (@untaint_regexes) {
452 # look at both constraints and constraint_methods
453 0 for my $field (keys %$private_constraints, keys %$private_constraint_methods) {
454 0 ▶ next if($untaint_hash{$field});
Branch: 1 branch
BranchTrueFalseTotal
if $untaint_hash{$field} 0 0 0
455 0 ▶ $untaint_hash{$field} = 1 if( $field =~ $regex );
Branch: 1 branch
BranchTrueFalseTotal
if $field =~ /$regex/ 0 0 0
456 }
457 }
458 }
459 }
460 ▶ elsif ((defined($profile->{untaint_all_constraints}))
Condition66%2/3
defined $profile->{"untaint_all_constraints"} and $profile->{"untaint_all_constraints"} == 1
!ll&&!rl&&r
111010
MC/DC50%1/2
defined $profile->{"untaint_all_constraints"} and $profile->{"untaint_all_constraints"} == 1
defined $profile->{"untaint_all_constraints"} $profile->{"untaint_all_constraints"} == 1
Truth table66%2/3
A and B
A= defined $profile->{"untaint_all_constraints"}B= $profile->{"untaint_all_constraints"} == 1
ABresult
0X0
100
111
461 && ($profile->{untaint_all_constraints} == 1)) {
462 10 $untaint_all = 1;
463 }
464  
465 124 $self->_check_constraints($private_constraints,\%valid,$untaint_all,\%untaint_hash);
466  
467 122 my $force_method_p = 1;
468 122 $self->_check_constraints($private_constraint_methods,\%valid,$untaint_all,\%untaint_hash, $force_method_p);
469  
470 # add back in missing optional fields from the data hash if we need to
471 122 for my $field ( keys %data ) {
472 250 ▶ if ($profile->{missing_optional_valid} and $optional{$field} and (not exists $valid{$field})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($profile->{"missing_optional_valid"} and $optional{$field} and not exists $valid{$field}) 2 248 250
Condition100%6/6
$profile->{"missing_optional_valid"} and $optional{$field} and not exists $valid{$field}
!ll&&!rl&&r
24532
$profile->{"missing_optional_valid"} and $optional{$field}
!ll&&!rl&&r
23785
MC/DC100%3/3
$profile->{"missing_optional_valid"} and $optional{$field} and not exists $valid{$field}
$profile->{"missing_optional_valid"} $optional{$field} not exists $valid{$field}
Truth table100%4/4
A and B and not(C)
A= $profile->{"missing_optional_valid"}B= $optional{$field}C= not exists $valid{$field}
ABCresult
0XX0
10X0
1100
1111
473 2 $valid{$field} = undef;
474 }
475 }
476  
477 # all invalid fields are removed from valid hash
478 122 for my $field (keys %{ $self->{invalid} }) {
479 71 delete $valid{$field};
480 }
481  
482 122 my ($missing,$invalid);
483  
484 122 ▶ $self->{valid} ||= {};
Condition50%1/2
$self->{"valid"} ||= {}
l!l
0122
MC/DC0%0/1
$self->{"valid"} ||= {}
$self->{"valid"}
Truth table50%1/2
A
A= $self->{"valid"}
Aresult
11
00
485 122 $self->{valid} = { %valid , %{$self->{valid}} };
486 122 $self->{missing} = { map { $_ => 1 } @missings };
487 122 $self->{unknown} = { map { $_ => $data{$_} } @unknown };
488  
489 }
490  
491 =pod
492  
493 =head1 success();
494  
495 This method returns true if there were no invalid or missing fields,
496 else it returns false.
497  
498 As a shorthand, When the $results object is used in boolean context, it is overloaded
499 to use the value of success() instead. That allows creation of a syntax like this one used
500 in C<CGI::Application::Plugin::ValidateRM>:
501  
502 my $results = $self->check_rm('form_display','_form_profile') || return $self->dfv_error_page;
503  
504 =cut
505  
506 25 ▶sub success {
Subroutine success: called CC=2 SCAR=6.9
Pod undocumented
507 25 my $self = shift;
508 25 ▶ return !($self->has_invalid || $self->has_missing);
Condition100%3/3
$self->has_invalid || $self->has_missing
l!l&&r!l&&!r
1159
MC/DC100%2/2
$self->has_invalid || $self->has_missing
$self->has_invalid $self->has_missing
Truth table100%3/3
A || B
A= $self->has_invalidB= $self->has_missing
ABresult
1X1
011
000
509 }
510  
511 =head1 valid( [[field] [, value]] );
512  
513 In list context with no arguments, it returns the list of fields which
514 contain valid values:
515  
516 @all_valid_field_names = $r->valid;
517  
518 In a scalar context with no arguments, it returns an hash reference which
519 contains the valid fields as keys and their input as values:
520  
521 $all_valid_href = $r->valid;
522  
523 If called with one argument in scalar context, it returns the value of that
524 C<field> if it contains valid data, C<undef> otherwise. The value will be an
525 array ref if the field had multiple values:
526  
527 $value = $r->valid('field');
528  
529 If called with one argument in list context, it returns the values of C<field>
530 as an array:
531  
532 @values = $r->valid('field');
533  
534 If called with two arguments, it sets C<field> to C<value> and returns C<value>.
535 This form is useful to alter the results from within some constraints.
536 See the L<Data::FormValidator::Constraints> documentation.
537  
538 $new_value = $r->valid('field',$new_value);
539  
540 =cut
541  
542 81 ▶sub valid {
Subroutine valid: called CC=5 SCAR=16.1
Pod undocumented
543 81 my $self = shift;
544 81 my $key = shift;
545 81 my $val = shift;
546 81 ▶ $self->{valid}{$key} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 81 81
547  
548 81 ▶ if (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 42 39 81
549 42 ▶ return wantarray ? _arrayify($self->{valid}{$key}) : $self->{valid}{$key};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 40 42
550 }
551  
552 # If we got this far, there were no arguments passed.
553 39 ▶ return wantarray ? keys %{ $self->{valid} } : $self->{valid};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 38 39
554 }
555  
556  
557 =pod
558  
559 =head1 has_missing()
560  
561 This method returns true if the results contain missing fields.
562  
563 =cut
564  
565 51 ▶sub has_missing {
Subroutine has_missing: called CC=1 SCAR=0.0
Pod undocumented
566 51 return scalar keys %{$_[0]{missing}};
567 }
568  
569 =pod
570  
571 =head1 missing( [field] )
572  
573 In list context it returns the list of fields which are missing.
574 In a scalar context, it returns an array reference to the list of missing fields.
575  
576 If called with an argument, it returns true if that C<field> is missing,
577 undef otherwise.
578  
579 =cut
580  
581 44 ▶sub missing {
Subroutine missing: called CC=3 SCAR=11.0
Pod undocumented
582 44 ▶ return $_[0]{missing}{$_[1]} if (defined $_[1]);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_[1] 11 33 44
583  
584 33 ▶ wantarray ? keys %{$_[0]{missing}} : [ keys %{$_[0]{missing}} ];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 32 33
585 }
586  
587  
588 =pod
589  
590 =head1 has_invalid()
591  
592 This method returns true if the results contain fields with invalid
593 data.
594  
595 =cut
596  
597 65 ▶sub has_invalid {
Subroutine has_invalid: called CC=1 SCAR=0.0
Pod undocumented
598 65 return scalar keys %{$_[0]{invalid}};
599 }
600  
601 =pod
602  
603 =head1 invalid( [field] )
604  
605 In list context, it returns the list of fields which contains invalid value.
606  
607 In a scalar context, it returns an hash reference which contains the invalid
608 fields as keys, and references to arrays of failed constraints as values.
609  
610 If called with an argument, it returns the reference to an array of failed
611 constraints for C<field>.
612  
613 =cut
614  
615 34 ▶sub invalid {
Subroutine invalid: called CC=3 SCAR=11.0
Pod undocumented
616 34 my $self = shift;
617 34 my $field = shift;
618 34 ▶ return $self->{invalid}{$field} if defined $field;
Branch: 1 branch
BranchTrueFalseTotal
if defined $field 13 21 34
619  
620 21 ▶ wantarray ? keys %{$self->{invalid}} : $self->{invalid};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 19 21
621 }
622  
623 =pod
624  
625 =head1 has_unknown()
626  
627 This method returns true if the results contain unknown fields.
628  
629 =cut
630  
631 0 ▶sub has_unknown {
Subroutine has_unknown: not called CC=1 SCAR=6.9
Pod undocumented
632 0 return scalar keys %{$_[0]{unknown}};
633  
634 }
635  
636 =pod
637  
638 =head1 unknown( [field] )
639  
640 In list context, it returns the list of fields which are unknown.
641 In a scalar context, it returns an hash reference which contains the unknown
642 fields and their values.
643  
644 If called with an argument, it returns the value of that C<field> if it
645 is unknown, undef otherwise.
646  
647 =cut
648  
649 29 ▶sub unknown {
Subroutine unknown: called CC=4 SCAR=13.9
Pod undocumented
650 29 ▶ return (wantarray ? _arrayify($_[0]{unknown}{$_[1]}) : $_[0]{unknown}{$_[1]})
Branch: 2 branches
BranchTrueFalseTotal
if defined $_[1] 4 25 29
wantarray ? : 2 2 4
651 if (defined $_[1]);
652  
653 25 ▶ wantarray ? keys %{$_[0]{unknown}} : $_[0]{unknown};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 25 0 25
654 }
655  
656  
657 =pod
658  
659 =head1 msgs([config parameters])
660  
661 This method returns a hash reference to error messages. The exact format
662 is determined by parameters in the C<msgs> area of the validation profile,
663 described in the L<Data::FormValidator> documentation.
664  
665 B<NOTE:> the C<msgs> parameter in the profile can take a code reference as a
666 value, allowing complete control of how messages are generated. If such a code
667 reference was provided there, it will be called here instead of the usual
668 processing, described below. It will receive as arguments the L<Data::FormValidator::Results>
669 object and a hash reference of control parameters.
670  
671 The hashref passed in should contain the same options that you can define in
672 the validation profile. This allows you to separate the controls for message
673 display from the rest of the profile. While validation profiles may be
674 different for every form, you may wish to format messages the same way across
675 many projects.
676  
677 Controls passed into the <msgs> method will be applied first, followed by ones
678 applied in the profile. This allows you to keep the controls you pass to
679 C<msgs> as "global" and override them in a specific profile if needed.
680  
681 =cut
682  
683 17 ▶sub msgs {
Subroutine msgs: called CC=3 SCAR=11.0
Pod undocumented
684 17 my $self = shift;
685 17 ▶ my $msgs = $self->{profile}{msgs} || {};
Condition100%2/2
$self->{'profile'}{'msgs'} || {}
l!l
152
MC/DC100%1/1
$self->{'profile'}{'msgs'} || {}
$self->{'profile'}{'msgs'}
Truth table100%2/2
A
A= $self->{'profile'}{'msgs'}
Aresult
11
00
686 17 ▶ if ((ref $msgs eq 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msgs eq 'CODE') { } 1 16 17
687 1 return $msgs->($self,@_);
688 } else {
689 16 return $self->_generate_msgs(@_);
690 }
691 }
692  
693  
694 16 ▶sub _generate_msgs {
Subroutine _generate_msgs: called CC=16 SCAR=27.8
695 16 my $self = shift;
696 16 ▶ my $controls = shift || {};
Condition100%2/2
shift() || {}
l!l
313
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
697 16 ▶ if (defined $controls and ref $controls ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $controls and ref $controls ne "HASH") 0 16 16
Condition33%1/3
defined $controls and ref $controls ne "HASH"
!ll&&!rl&&r
0160
MC/DC0%0/2
defined $controls and ref $controls ne "HASH"
defined $controls ref $controls ne "HASH"
Truth table33%1/3
A and B
A= defined $controlsB= ref $controls ne "HASH"
ABresult
0X0
100
111
698 0 die "$0: parameter passed to msgs must be a hash ref";
699 }
700  
701  
702 # Allow msgs to be called more than one to accumulate error messages
703 16 ▶ $self->{msgs} ||= {};
Condition100%2/2
$self->{"msgs"} ||= {}
l!l
313
MC/DC100%1/1
$self->{"msgs"} ||= {}
$self->{"msgs"}
Truth table100%2/2
A
A= $self->{"msgs"}
Aresult
11
00
704 16 ▶ $self->{profile}{msgs} ||= {};
Condition100%2/2
$self->{"profile"}{"msgs"} ||= {}
l!l
142
MC/DC100%1/1
$self->{"profile"}{"msgs"} ||= {}
$self->{"profile"}{"msgs"}
Truth table100%2/2
A
A= $self->{"profile"}{"msgs"}
Aresult
11
00
705 16 $self->{msgs} = { %{ $self->{msgs} }, %$controls };
706  
707 # Legacy typo support.
708 16 for my $href ($self->{msgs}, $self->{profile}{msgs}) {
709 32 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $href->{"invalid_separator"} and defined $href->{"invalid_seperator"}) 1 31 32
Condition100%3/3
not defined $href->{"invalid_separator"} and defined $href->{"invalid_seperator"}
!ll&&!rl&&r
1301
MC/DC100%2/2
not defined $href->{"invalid_separator"} and defined $href->{"invalid_seperator"}
not defined $href->{"invalid_separator"} defined $href->{"invalid_seperator"}
Truth table100%3/3
not(A) and B
A= not defined $href->{"invalid_separator"}B= defined $href->{"invalid_seperator"}
ABresult
0X0
100
111
710 (not defined $href->{invalid_separator})
711 && (defined $href->{invalid_seperator})
712 ) {
713 1 $href->{invalid_separator} = $href->{invalid_seperator};
714 }
715 }
716  
717 my %profile = (
718 prefix => '',
719 missing => 'Missing',
720 invalid => 'Invalid',
721 invalid_separator => ' ',
722  
723 format => '<span style="color:red;font-weight:bold" class="dfv_errors">* %s</span>',
724 16 %{ $self->{msgs} },
725 16 %{ $self->{profile}{msgs} },
726 );
727  
728  
729 16 my %msgs = ();
730  
731 # Add invalid messages to hash
732 # look at all the constraints, look up their messages (or provide a default)
733 # add field + formatted constraint message to hash
734 16 ▶ if ($self->has_invalid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_invalid) 9 7 16
735 9 my $invalid = $self->invalid;
736 9 for my $i ( keys %$invalid ) {
737 $msgs{$i} = join $profile{invalid_separator}, map {
738 18 ▶ _error_msg_fmt($profile{format},($profile{constraints}{$_} || $profile{invalid}))
Condition66%2/3
$profile{'constraints'}{$_} || $profile{'invalid'}
l!l&&r!l&&!r
1080
MC/DC0%0/2
$profile{'constraints'}{$_} || $profile{'invalid'}
$profile{'constraints'}{$_} $profile{'invalid'}
Truth table66%2/3
A || B
A= $profile{'constraints'}{$_}B= $profile{'invalid'}
ABresult
1X1
011
000
739 14 } @{ $invalid->{$i} };
740 }
741 }
742  
743 # Add missing messages, if any
744 16 ▶ if ($self->has_missing) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_missing) 6 10 16
745 6 my $missing = $self->missing;
746 6 for my $m (@$missing) {
747 6 $msgs{$m} = _error_msg_fmt($profile{format},$profile{missing});
748 }
749 }
750  
751 16 my $msgs_ref = prefix_hash($profile{prefix},\%msgs);
752  
753 16 ▶ if (! $self->success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->success) 12 4 16
754 12 ▶ $msgs_ref->{ $profile{any_errors} } = 1 if defined $profile{any_errors};
Branch: 1 branch
BranchTrueFalseTotal
if defined $profile{"any_errors"} 3 9 12
755 }
756  
757 16 return $msgs_ref;
758  
759 }
760  
761 =pod
762  
763 =head1 meta()
764  
765 In a few cases, a constraint may discover meta data that is useful
766 to access later. For example, when using L<Data::FormValidator::Constraints::Upload>, several bits of meta data are discovered about files in the process
767 of validating. These can include "bytes", "width", "height" and "extension".
768 The C<meta()> function is used by constraint methods to set this data. It's
769 also used to access this data. Here are some examples.
770  
771 # return all field names that have meta data
772 my @fields = $results->meta();
773  
774 # To retrieve all meta data for a field:
775 $meta_href = $results->meta('img');
776  
777 # Access a particular piece:
778 $width = $results->meta('img')->{width};
779  
780 Here's how to set some meta data. This is useful to know if you are
781 writing your own complex constraint.
782  
783 $self->meta('img', {
784 width => '50',
785 height => '60',
786 });
787  
788 This function does not currently support multi-valued fields. If it
789 does in the future, the above syntax will still work.
790  
791 =cut
792  
793 0 ▶sub meta {
Subroutine meta: not called CC=5 SCAR=34.0
Pod undocumented
794 0 my $self = shift;
795 0 my $field = shift;
796 0 my $data = shift;
797  
798 # initialize if it's the first call
799 0 ▶ $self->{__META} ||= {};
Condition0%0/2
$self->{"__META"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"__META"} ||= {}
$self->{"__META"}
Truth table0%0/2
A
A= $self->{"__META"}
Aresult
11
00
800  
801 0 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) 0 0 0
802 0 ▶ (ref $data eq 'HASH') or die 'meta: data passed not a hash ref';
Branch: 1 branch
BranchTrueFalseTotal
ref $data eq "HASH" or die "meta: data passed not a hash ref" 0 0 0
803 0 $self->{__META}{$field} = $data;
804 }
805  
806  
807 # If we are passed a field, return data for that field
808 0 ▶ if ($field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field) { } 0 0 0
809 0 return $self->{__META}{$field};
810 }
811 # Otherwise return a list of all fields that have meta data
812 else {
813 0 return keys %{ $self->{__META} };
814 }
815 }
816  
817 # These are documented in ::Constraints, in the section
818 # on writing your own routines. It was more intuitive
819 # for the user to look there.
820  
821 157 ▶sub get_input_data {
Subroutine get_input_data: called CC=2 SCAR=7.9
Pod undocumented
822 157 my $self = shift;
823 157 my %p = @_;
824 157 ▶ if ($p{as_hashref}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{'as_hashref'}) { } 0 157 157
825 0 my %hash = $self->_get_input_as_hash( $self->{__INPUT_DATA} );
826 0 return \%hash;
827 }
828 else {
829 157 return $self->{__INPUT_DATA};
830 }
831 }
832  
833 21 ▶sub get_filtered_data {
Subroutine get_filtered_data: called CC=1 SCAR=0.0
Pod undocumented
834 21 my $self = shift;
835 21 return $self->{__FILTERED_DATA};
836 }
837  
838 5 ▶sub get_current_constraint_field {
Subroutine get_current_constraint_field: called CC=1 SCAR=0.0
Pod undocumented
839 5 my $self = shift;
840 5 return $self->{__CURRENT_CONSTRAINT_FIELD};
841 }
842  
843 36 ▶sub get_current_constraint_value {
Subroutine get_current_constraint_value: called CC=1 SCAR=0.0
Pod undocumented
844 36 my $self = shift;
845 36 return $self->{__CURRENT_CONSTRAINT_VALUE};
846 }
847  
848 38 ▶sub get_current_constraint_name {
Subroutine get_current_constraint_name: called CC=1 SCAR=0.0
Pod undocumented
849 38 my $self = shift;
850 38 return $self->{__CURRENT_CONSTRAINT_NAME};
851 }
852  
853 16 ▶sub untainted_constraint_value {
Subroutine untainted_constraint_value: called CC=3 SCAR=11.0
Pod undocumented
854 16 my $self = shift;
855 16 my $match = shift;
856  
857 16 ▶ return undef unless defined $match;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $match 4 12 16
858 12 ▶ return $self->{__UNTAINT_THIS} ? $match : length $match;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'__UNTAINT_THIS'} ? : 5 7 12
859 }
860  
861 4 ▶sub set_current_constraint_name {
Subroutine set_current_constraint_name: called CC=1 SCAR=0.0
Pod undocumented
862 4 my $self = shift;
863 4 my $value = shift;
864 4 $self->{__CURRENT_CONSTRAINT_NAME} = $value;
865 }
866 # same as above
867 35 ▶sub name_this {
Subroutine name_this: called CC=1 SCAR=0.0
Pod undocumented
868 35 my $self = shift;
869 35 my $value = shift;
870 35 $self->{__CURRENT_CONSTRAINT_NAME} = $value;
871 }
872  
873 # INPUT: prefix_string, hash reference
874 # Copies the hash and prefixes all keys with prefix_string
875 # OUTPUT: hash reference
876 16 ▶sub prefix_hash {
Subroutine prefix_hash: called CC=4 SCAR=14.1
Pod undocumented
877 16 my ($pre,$href) = @_;
878 16 ▶ die "prefix_hash: need two arguments" unless (scalar @_ == 2);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 2 0 16 16
879 16 ▶ die "prefix_hash: second argument must be a hash ref" unless (ref $href eq 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $href eq "HASH" 0 16 16
880 16 my %out;
881 16 for (keys %$href) {
882 20 $out{$pre.$_} = $href->{$_};
883 }
884 16 return \%out;
885 }
886  
887  
888 # We tolerate two kinds of regular expression formats
889 # First, the preferred format made with "qr", matched using a leading paren
890 # Also, we accept the deprecated format given as strings: 'm/old/'
891 # (which must start with a slash or "m", not a paren)
892 293 ▶sub _create_sub_from_RE {
Subroutine _create_sub_from_RE: called CC=6 SCAR=18.1
893 293 ▶ my $re = shift || return undef;
Condition100%2/2
shift() || (return undef)
l!l
48245
MC/DC100%1/1
shift() || (return undef)
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
894 48 my $untaint_this = shift;
895 48 my $force_method_p = shift;
896  
897 48 my $sub;
898 # If it's "qr" style
899 48 ▶ if (substr($re,0,1) eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($re, 0, 1) eq '(') { } 41 7 48
900 75 ▶ $sub = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
901 # With methods, the value is the second argument
902 75 ▶ my $val = $force_method_p ? $_[1] : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
$force_method_p ? : 12 63 75
903 75 my ($match) = scalar ($val =~ $re);
904 75 ▶ if ($untaint_this && defined $match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($untaint_this and defined $match) { } 5 70 75
Condition66%2/3
$untaint_this and defined $match
!ll&&!rl&&r
7005
MC/DC50%1/2
$untaint_this and defined $match
$untaint_this defined $match
Truth table66%2/3
A and B
A= $untaint_thisB= defined $match
ABresult
0X0
100
111
905 # pass the value through a RE that matches anything to untaint it.
906 5 my ($untainted) = ($& =~ m/(.*)/s);
907 5 return $untainted;
908 }
909 else {
910 70 return $match;
911 }
912 41 };
913  
914 }
915 else {
916 7 local $SIG{__DIE__} = \&confess;
917 7 ▶ my $return_code = ($untaint_this) ? '; return ($& =~ m/(.*)/s)[0] if defined($`);' : '';
Branch: 1 branch
BranchTrueFalseTotal
$untaint_this ? : 0 7 7
918 # With methods, the value is the second argument
919 7 ▶ if ($force_method_p) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_method_p) { } 0 7 7
920 0 $sub = eval 'sub { $_[1] =~ '.$re.$return_code. '}';
921 }
922 else {
923 7 $sub = eval 'sub { $_[0] =~ '.$re.$return_code. '}';
924 }
925 7 ▶ die "Error compiling regular expression $re: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 7 7
926 }
927 48 return $sub;
928 }
929  
930  
931 24 ▶sub _error_msg_fmt {
Subroutine _error_msg_fmt: called CC=3 SCAR=11.4
932 24 my ($fmt,$msg) = @_;
933 24 ▶ $fmt ||=
Condition50%1/2
$fmt ||= "<span style=\"color:red;font-weight:bold\" class=\"dfv_errors\">* %s</span>"
l!l
240
MC/DC0%0/1
$fmt ||= "<span style=\"color:red;font-weight:bold\" class=\"dfv_errors\">* %s</span>"
$fmt
Truth table50%1/2
A
A= $fmt
Aresult
11
00
934 '<span style="color:red;font-weight:bold" class="dfv_errors">* %s</span>';
935 24 ▶ ($fmt =~ m/%s/) || die 'format must contain %s';
Branch: 1 branch
BranchTrueFalseTotal
$fmt =~ /%s/ or die "format must contain %s" 24 0 24
936 24 return sprintf $fmt, $msg;
937 }
938  
939  
940  
941 # takes string or array ref as input
942 # returns array
943 780 ▶sub _arrayify {
Subroutine _arrayify: called CC=5 SCAR=16.1
944 # if the input is undefined, return an empty list
945 780 my $val = shift;
946 780 ▶ defined $val or return ();
Branch: 1 branch
BranchTrueFalseTotal
defined $val or return () 409 371 780
947  
948 # if it's a reference, return an array unless it points to an empty array. -mls
949 409 ▶ if ( ref $val eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 175 234 409
950 175 local $^W = 0; # turn off warnings about undef
951 175 ▶ return grep(defined, @$val) ? @$val : ();
Branch: 1 branch
BranchTrueFalseTotal
grep(defined($_), @$val) ? : 172 3 175
952 }
953 # if it's a string, return an array unless the string is missing or empty. -mls
954 else {
955 234 ▶ return (length $val) ? ($val) : ();
Branch: 1 branch
BranchTrueFalseTotal
length $val ? : 234 0 234
956 }
957 }
958  
959 # apply filter, modifying %valid by reference
960 # We don't bother trying to filter undefined fields.
961 # This prevents warnings from Perl.
962 26 ▶sub _filter_apply {
Subroutine _filter_apply: called CC=6 SCAR=18.2
963 26 my ($valid,$field,$filter) = @_;
964 26 ▶ die 'wrong number of arguments passed to _filter_apply' unless (scalar @_ == 3);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 3 0 26 26
965 26 ▶ if (ref $valid->{$field} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $valid->{$field} eq 'ARRAY') { } 7 19 26
966 7 ▶ for (my $i = 0; $i < @{ $valid->{$field} }; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$valid->{$field};}) 22 7 29
967 22 ▶ $valid->{$field}->[$i] = $filter->( $valid->{$field}->[$i] ) if defined $valid->{$field}->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if defined $valid->{$field}[$i] 22 0 22
968 }
969 }
970 else {
971 19 ▶ $valid->{$field} = $filter->( $valid->{$field} ) if defined $valid->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if defined $valid->{$field} 19 0 19
972 }
973 }
974  
975 # =head2 _constraint_hash_build()
976 #
977 # $constraint_href = $self->_constraint_hash_build($spec,$untaint_p)
978 #
979 # Input:
980 # - $spec # Any constraint valid in the profile
981 # - $untaint # bool for whether we could try to untaint the field.
982 # - $force_method_p # bool for if it's a method ?
983 #
984 # Output:
985 # - $constraint_hashref
986 # Keys are as follows:
987 # constraint - the constraint as coderef
988 # name - the constraint name, if we know it.
989 # params - 'params', as given in the hashref style of specifying a constraint
990 # is_method - bool for whether this was a 'constraint' or 'constraint_method'
991  
992 159 ▶sub _constraint_hash_build {
Subroutine _constraint_hash_build: called CC=20 SCAR=30.2
993 159 my ($self,$constraint_spec,$untaint_this,$force_method_p) = @_;
994 159 ▶ die "_constraint_hash_build received wrong number of arguments" unless (scalar @_ == 4);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 4 0 159 159
995  
996 159 my $c = {
997 name => undef,
998 constraint => $constraint_spec,
999 };
1000 159 ▶ $c->{name} = $constraint_spec if not ref $constraint_spec;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $constraint_spec 44 115 159
1001  
1002 # constraints can be passed in directly via hash
1003 159 ▶ if (ref $c->{constraint} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c->{"constraint"} eq "HASH") 38 121 159
1004 38 ▶ $c->{constraint} = ($constraint_spec->{constraint_method} || $constraint_spec->{constraint});
Condition66%2/3
$constraint_spec->{'constraint_method'} || $constraint_spec->{'constraint'}
l!l&&r!l&&!r
5330
MC/DC0%0/2
$constraint_spec->{'constraint_method'} || $constraint_spec->{'constraint'}
$constraint_spec->{'constraint_method'} $constraint_spec->{'constraint'}
Truth table66%2/3
A || B
A= $constraint_spec->{'constraint_method'}B= $constraint_spec->{'constraint'}
ABresult
1X1
011
000
1005 38 $c->{name} = $constraint_spec->{name};
1006 38 $c->{params} = $constraint_spec->{params};
1007 38 ▶ $c->{is_method} = 1 if $constraint_spec->{constraint_method};
Branch: 1 branch
BranchTrueFalseTotal
if $constraint_spec->{"constraint_method"} 5 33 38
1008 }
1009  
1010 # Check for regexp constraint
1011 159 ▶ if ((ref $c->{constraint} eq 'Regexp')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $c->{'constraint'} eq 'Regexp' or $c->{'constraint'} =~ m[^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$]) { } 22 137 159
elsif (ref $c->{'constraint'} eq 'CODE') { } 76 61 137
Condition100%3/3
ref $c->{"constraint"} eq "Regexp" or $c->{"constraint"} =~ m[^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$]
l!l&&r!l&&!r
211137
MC/DC100%2/2
ref $c->{"constraint"} eq "Regexp" or $c->{"constraint"} =~ m[^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$]
ref $c->{"constraint"} eq "Regexp" $c->{"constraint"} =~ m[^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$]
Truth table100%3/3
A or B
A= ref $c->{"constraint"} eq "Regexp"B= $c->{"constraint"} =~ m[^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$]
ABresult
1X1
011
000
1012 or ( $c->{constraint} =~ m@^\s*(/.+/|m(.).+\2)[cgimosx]*\s*$@ )) {
1013 22 $c->{constraint} = _create_sub_from_RE($c->{constraint},$untaint_this,$force_method_p);
1014 }
1015 # check for code ref
1016 elsif (ref $c->{constraint} eq 'CODE') {
1017 # do nothing, it's already a code ref
1018 }
1019 else {
1020 # provide a default name for the constraint if we don't have one already
1021 61 ▶ if (not $c->{name} and not ref $c->{constraint}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->{"name"} or ref $c->{"constraint"}) 16 45 61
Condition66%2/3
$c->{"name"} or ref $c->{"constraint"}
l!l&&r!l&&!r
45016
MC/DC50%1/2
$c->{"name"} or ref $c->{"constraint"}
$c->{"name"} ref $c->{"constraint"}
Truth table66%2/3
A or B
A= $c->{"name"}B= ref $c->{"constraint"}
ABresult
1X1
011
000
1022 16 ▶ $c->{name} ||= $c->{constraint};
Condition33%1/3
$c->{"name"} ||= $c->{"constraint"}
l!l&&r!l&&!r
0160
MC/DC0%0/2
$c->{"name"} ||= $c->{"constraint"}
$c->{"name"} $c->{"constraint"}
Truth table33%1/3
A ||= B
A= $c->{"name"}B= $c->{"constraint"}
ABresult
1X1
011
000
1023 }
1024  
1025 #If untaint is turned on call match_* sub directly.
1026 61 ▶ if ($untaint_this) {
Branch: 1 branch
BranchTrueFalseTotal
if ($untaint_this) { } 3 58 61
1027 3 my $routine = 'match_'.$c->{constraint};
1028 3 my $match_sub = *{qualify_to_ref($routine)}{CODE};
1029 3 ▶ if ($match_sub) {
Branch: 2 branches
BranchTrueFalseTotal
if ($match_sub) { } 0 3 3
elsif ($c->{'constraint'} =~ /^RE_/) { } 2 1 3
1030 0 $c->{constraint} = $match_sub;
1031 }
1032 # If the constraint name starts with RE_, try looking for it in the Regexp::Common package
1033 elsif ($c->{constraint} =~ m/^RE_/) {
1034 2 local $SIG{__DIE__} = \&confess;
1035 2 $c->{is_method} = 1;
1036 2 ▶ $c->{constraint} = eval 'sub { &_create_regexp_common_constraint(@_)}'
Condition50%1/2
eval 'sub { &_create_regexp_common_constraint(@_)}' || die("could not create Regexp::Common constraint: $@")
l!l
20
MC/DC0%0/1
eval 'sub { &_create_regexp_common_constraint(@_)}' || die("could not create Regexp::Common constraint: $@")
eval 'sub { &_create_regexp_common_constraint(@_)}'
Truth table50%1/2
A
A= eval 'sub { &_create_regexp_common_constraint(@_)}'
Aresult
11
00
1037 || die "could not create Regexp::Common constraint: $@";
1038 } else {
1039 1 die "No untainting constraint found named $c->{constraint}";
1040 }
1041 }
1042 else {
1043 # try to use match_* first
1044 58 my $routine = 'match_'.$c->{constraint};
1045 58 ▶ if (defined *{qualify_to_ref($routine)}{CODE}) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined *{&qualify_to_ref($routine);}{'CODE'}) { } 51 7 58
elsif (my $valid_sub = *{&qualify_to_ref('valid_' . $c->{'constraint'});}{'CODE'}) { } 1 6 7
elsif ($c->{'constraint'} =~ /^RE_/) { } 5 1 6
1046 51 local $SIG{__DIE__} = \&confess;
1047 51 ▶ $c->{constraint} = eval 'sub { no strict qw/refs/; return defined &{"match_'.$c->{constraint}.'"}(@_)}';
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
1048 }
1049 # match_* doesn't exist; if it is supposed to be from the
1050 # validator_package(s) there may be only valid_* defined
1051 7 elsif (my $valid_sub = *{qualify_to_ref('valid_'.$c->{constraint})}{CODE}) {
1052 1 $c->{constraint} = $valid_sub;
1053 }
1054 # Load it from Regexp::Common
1055 elsif ($c->{constraint} =~ m/^RE_/) {
1056 5 local $SIG{__DIE__} = \&confess;
1057 5 $c->{is_method} = 1;
1058 5 ▶ $c->{constraint} = eval 'sub { return defined &_create_regexp_common_constraint(@_)}' ||
Condition50%1/2
eval 'sub { return defined &_create_regexp_common_constraint(@_)}' || die("could not create Regexp::Common constraint: $@")
l!l
50
MC/DC0%0/1
eval 'sub { return defined &_create_regexp_common_constraint(@_)}' || die("could not create Regexp::Common constraint: $@")
eval 'sub { return defined &_create_regexp_common_constraint(@_)}'
Truth table50%1/2
A
A= eval 'sub { return defined &_create_regexp_common_constraint(@_)}'
Aresult
11
00
1059 die "could not create Regexp::Common constraint: $@";
1060 }
1061 else {
1062 1 die "No constraint found named '$c->{name}'";
1063 }
1064 }
1065 }
1066  
1067 # Save the current constraint name for later
1068 157 $self->{__CURRENT_CONSTRAINT_NAME} = $c->{name};
1069  
1070 157 return $c;
1071  
1072 }
1073  
1074 # =head2 _constraint_input_build()
1075 #
1076 # @params = $self->constraint_input_build($c,$value,$data);
1077 #
1078 # Build in the input that passed into the constraint.
1079 #
1080 # =cut
1081  
1082 161 ▶sub _constraint_input_build {
Subroutine _constraint_input_build: called CC=6 SCAR=17.9
1083 161 my ($self,$c,$value,$data) = @_;
1084 161 ▶ die "_constraint_input_build received wrong number of arguments" unless (scalar @_ == 4);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 4 0 161 161
1085  
1086 161 my @params;
1087 161 ▶ if (defined $c->{params}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c->{'params'}) { } 24 137 161
1088 24 for my $fname (_arrayify($c->{params})) {
1089 # If the value is passed by reference, we treat it literally
1090 44 ▶ push @params, (ref $fname) ? $fname : $data->{$fname}
Branch: 1 branch
BranchTrueFalseTotal
ref $fname ? : 7 37 44
1091 }
1092 }
1093 else {
1094 137 push @params, $value;
1095 }
1096  
1097 161 ▶ unshift @params, $self if $c->{is_method};
Branch: 1 branch
BranchTrueFalseTotal
if $c->{"is_method"} 77 84 161
1098 161 return @params;
1099 }
1100  
1101 # =head2 _constraint_check_match()
1102 #
1103 # ($value,$failed_href) = $self->_constraint_check_match($c,\@params,$untaint_this);
1104 #
1105 # This is the routine that actually, finally, checks if a constraint passes or fails.
1106 #
1107 # Input:
1108 # - $c, a constraint hash, as returned by C<_constraint_hash_build()>.
1109 # - \@params, params to pass to the constraint, as prepared by C<_constraint_input_build()>.
1110 # - $untaint_this bool if we untaint successful constraints.
1111 #
1112 # Output:
1113 # - $value the value if successful
1114 # - $failed_href a hashref with the following keys:
1115 # - failed bool for failure or not
1116 # - name name of the failed constraint, if known.
1117  
1118 161 ▶sub _constraint_check_match {
Subroutine _constraint_check_match: called CC=5 SCAR=16.1
1119 161 my ($self,$c,$params,$untaint_this) = @_;
1120 161 ▶ die "_constraint_check_match received wrong number of arguments" unless (scalar @_ == 4);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 4 0 161 161
1121  
1122 # Store whether or not we want untainting in the object so that constraints
1123 # can do the right thing conditionally.
1124 161 $self->{__UNTAINT_THIS} = $untaint_this;
1125  
1126 161 my $match = $c->{constraint}->( @$params );
1127  
1128 # We need to make this distinction when untainting,
1129 # to allow untainting values that are defined but not true,
1130 # such as zero.
1131 161 my $success;
1132 161 ▶ if (defined $match) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $match) 137 24 161
1133 137 ▶ $success = ($untaint_this) ? length $match : $match;
Branch: 1 branch
BranchTrueFalseTotal
$untaint_this ? : 12 125 137
1134 }
1135  
1136 161 ▶ my $failed = 1 unless $success;
Branch: 1 branch
BranchTrueFalseTotal
unless $success 79 82 161
1137 return (
1138 $match,
1139 {
1140 failed => $failed,
1141 name => $self->{__CURRENT_CONSTRAINT_NAME},
1142 },
1143 161 );
1144 }
1145  
1146 # Figure out whether the data is a hash reference of a param-capable object and return it has a hash
1147 285 ▶sub _get_input_as_hash {
Subroutine _get_input_as_hash: called CC=9 SCAR=32.4
1148 285 my ($self,$data) = @_;
1149 285 $self->{__INPUT_DATA} = $data;
1150  
1151 285 require Scalar::Util;
1152  
1153 # This checks whether we have an object that supports param
1154 285 ▶ if ( Scalar::Util::blessed($data) && $data->can('param') ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($data) and $data->can('param')) { } 0 285 285
elsif (ref $data eq 'HASH') { } 285 0 285
Condition33%1/3
builtin::blessed($data) and $data->can("param")
!ll&&!rl&&r
28500
MC/DC0%0/2
builtin::blessed($data) and $data->can("param")
builtin::blessed($data) $data->can("param")
Truth table33%1/3
A and B
A= builtin::blessed($data)B= $data->can("param")
ABresult
0X0
100
111
1155 0 my %return;
1156 0 for my $k ($data->param()){
1157 # we expect param to return an array if there are multiple values
1158 0 my @v;
1159  
1160 # CGI::Simple requires us to call 'upload()' to get upload data,
1161 # while CGI/Apache::Request return it on calling 'param()'.
1162 #
1163 # This seems quirky, but there isn't a way for us to easily check if
1164 # "this field contains a file upload" or not.
1165 0 ▶ if ($data->isa('CGI::Simple')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data->isa('CGI::Simple')) { } 0 0 0
1166 0 ▶ @v = $data->upload($k) || $data->param($k);
Condition0%0/3
$data->upload($k) || $data->param($k)
l!l&&r!l&&!r
000
MC/DC0%0/2
$data->upload($k) || $data->param($k)
$data->upload($k) $data->param($k)
Truth table0%0/3
A || B
A= $data->upload($k)B= $data->param($k)
ABresult
1X1
011
000
1167 }
1168 else {
1169 # insecure
1170 0 @v = $data->multi_param($k);
1171 }
1172  
1173 # we expect param to return an array if there are multiple values
1174 0 ▶ $return{$k} = scalar(@v)>1 ? \@v : $v[0];
Branch: 1 branch
BranchTrueFalseTotal
scalar @v > 1 ? : 0 0 0
1175 }
1176 0 return %return;
1177 }
1178 # otherwise, it's already a hash reference
1179 elsif (ref $data eq 'HASH') {
1180 # be careful to actually copy array references
1181 285 my %copy = %$data;
1182 285 for (grep { ref $data->{$_} eq 'ARRAY' } keys %$data) {
1183 44 my @array_copy = @{ $data->{$_} };
1184 44 $copy{$_} = \@array_copy;
1185 }
1186  
1187 285 return %copy;
1188 }
1189 else {
1190 0 die "Data::FormValidator->validate() or check() called with invalid input data structure.";
1191 }
1192 }
1193  
1194 # A newer version of this logic now exists in Constraints.pm in the AUTOLOADing section
1195 # This is is used to support the older param passing style. Eg:
1196 #
1197 # {
1198 # constraint => 'RE_foo_bar',
1199 # params => [ \'zoo' ]
1200 # }
1201 #
1202 # Still, it's possible, the two bits of logic could be refactored into one location if you cared
1203 # to do that.
1204  
1205 7 ▶sub _create_regexp_common_constraint {
Subroutine _create_regexp_common_constraint: called CC=4 SCAR=13.9
1206 # this should work most of the time and is useful for preventing warnings
1207  
1208 # prevent name space clashes
1209 package Data::FormValidator::Constraints::RegexpCommon;
1210  
1211 7 require Regexp::Common;
1212 7 import Regexp::Common 'RE_ALL';
1213  
1214 7 my $self = shift;
1215 7 my $re_name = $self->get_current_constraint_name;
1216 # deference all input
1217 7 ▶ my @params = map {$_ = $$_ if ref $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ 4 5 9
1218  
1219 59 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
1220 7 ▶ my $re = &$re_name(-keep=>1,@params) || die 'no matching Regexp::Common routine found';
Condition50%1/2
&$re_name('-keep', 1, @params) || die('no matching Regexp::Common routine found')
l!l
70
MC/DC0%0/1
&$re_name('-keep', 1, @params) || die('no matching Regexp::Common routine found')
&$re_name('-keep', 1, @params)
Truth table50%1/2
A
A= &$re_name('-keep', 1, @params)
Aresult
11
00
1221 7 ▶ return ($self->get_current_constraint_value =~ qr/^$re$/) ? $1 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->get_current_constraint_value =~ qr/^$re$/ ? : 3 4 7
1222 }
1223  
1224 # _add_constraints_from_map($profile,'constraint',\%valid);
1225 # Returns:
1226 # - a hash to add to either 'constraints' or 'constraint_methods'
1227  
1228 248 ▶sub _add_constraints_from_map {
Subroutine _add_constraints_from_map: called CC=9 SCAR=22.0
1229 248 ▶ die "_add_constraints_from_map: need 3 arguments" unless (scalar @_ == 3);
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ == 3 0 248 248
1230 248 my ($profile, $name, $valid) = @_;
1231 248 ▶ ($name =~ m/^constraint(_method)?$/) || die "unexpected input.";
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^constraint(_method)?$/ or die "unexpected input." 248 0 248
1232  
1233 248 my $key_name = $name.'s';
1234 248 my $map_name = $name.'_regexp_map';
1235  
1236 248 my %result = ();
1237 248 for my $re (keys %{ $profile->{$map_name} }) {
1238 17 my $sub = _create_sub_from_RE($re);
1239  
1240 # find all the keys that match this RE and add a constraint for them
1241 17 for my $key (keys %$valid) {
1242 45 ▶ if ($sub->($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&$sub($key)) 28 17 45
1243 28 my $cur = $profile->{$key_name}{$key};
1244 28 my $new = $profile->{$map_name}{$re};
1245 # If they already have an arrayref of constraints, add to the list
1246 28 ▶ if (ref $cur eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $cur eq 'ARRAY') { } 2 26 28
elsif ($cur) { } 1 25 26
1247 2 push @{ $result{$key} }, @$cur, $new;
1248 }
1249 # If they have a single constraint defined, create an array ref with with this plus the new one
1250 elsif ($cur) {
1251 1 $result{$key} = [$cur,$new];
1252 }
1253 # otherwise, a new constraint is created with this as the single constraint
1254 else {
1255 25 $result{$key} = $new;
1256 }
1257 28 ▶ warn "$map_name: $key matches\n" if $profile->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $profile->{"debug"} 0 28 28
1258 }
1259 }
1260 }
1261 248 return %result;
1262 }
1263  
1264 7 ▶sub _bool_overload_based_on_success {
Subroutine _bool_overload_based_on_success: called CC=1 SCAR=0.0
1265 7 my $results = shift;
1266 7 return $results->success()
1267 }
1268  
1269 # =head2 _check_constraints()
1270 #
1271 # $self->_check_constraints(
1272 # $profile->{constraint_methods},
1273 # \%valid,
1274 # $untaint_all
1275 # \%untaint_hash
1276 # $force_method_p
1277 #);
1278 #
1279 # Input:
1280 # - 'constraints' or 'constraint_methods' hashref
1281 # - hashref of valid data
1282 # - bool to try to untaint everything
1283 # - hashref of things to untaint
1284 # - bool if all constraints should be treated as methods.
1285  
1286 246 ▶sub _check_constraints {
Subroutine _check_constraints: called CC=18 SCAR=28.9
1287 246 my ($self,
1288 $constraint_href,
1289 $valid,
1290 $untaint_all,
1291 $untaint_href,
1292 $force_method_p) = @_;
1293  
1294 246 ▶ while ( my ($field,$constraint_list) = each %$constraint_href ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$constraint_href) 149 244 393
1295 149 ▶ next unless exists $valid->{$field};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $valid->{$field} 4 145 149
1296  
1297 145 ▶ my $is_constraint_list = 1 if (ref $constraint_list eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $constraint_list eq "ARRAY" 14 131 145
1298 145 ▶ my $untaint_this = ($untaint_all || $untaint_href->{$field} || 0);
Condition100%5/5
$untaint_all || $untaint_href->{$field} || 0
l!l
20125
$untaint_all or $untaint_href->{$field}
l!l&&r!l&&!r
191125
MC/DC100%2/2
$untaint_all || $untaint_href->{$field} || 0
$untaint_all $untaint_href->{$field}
Truth table100%3/3
A or B
A= $untaint_allB= $untaint_href->{$field}
ABresult
1X1
011
000
1299  
1300 145 my @invalid_list;
1301 # used to insure we only bother recording each failed constraint once
1302 my %constraints_seen;
1303 145 for my $constraint_spec (_arrayify($constraint_list)) {
1304  
1305 # set current constraint field for use by get_current_constraint_field
1306 159 $self->{__CURRENT_CONSTRAINT_FIELD} = $field;
1307  
1308 # Initialize the current constraint name to undef, to prevent it
1309 # from being accidently shared
1310 159 $self->{__CURRENT_CONSTRAINT_NAME} = undef;
1311  
1312 159 my $c = $self->_constraint_hash_build($constraint_spec,$untaint_this, $force_method_p);
1313 157 ▶ $c->{is_method} = 1 if $force_method_p;
Branch: 1 branch
BranchTrueFalseTotal
if $force_method_p 66 91 157
1314  
1315 157 ▶ my $is_value_list = 1 if (ref $valid->{$field} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if ref $valid->{$field} eq "ARRAY" 7 150 157
1316 157 my %param_data = ( $self->_get_input_as_hash($self->get_input_data) , %$valid );
1317 157 ▶ if ($is_value_list) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_value_list) { } 7 150 157
1318 7 ▶ for (my $i = 0; $i < scalar @{ $valid->{$field}} ; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$valid->{$field};}) 14 7 21
1319 14 ▶ if( !exists $constraints_seen{\$c} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $constraints_seen{\$c}) 11 3 14
1320  
1321 11 my @params = $self->_constraint_input_build($c,$valid->{$field}->[$i],\%param_data);
1322  
1323 # set current constraint field for use by get_current_constraint_value
1324 11 $self->{__CURRENT_CONSTRAINT_VALUE} = $valid->{$field}->[$i];
1325  
1326 11 my ($match,$failed) = $self->_constraint_check_match($c,\@params,$untaint_this);
1327 11 ▶ if ($failed->{failed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed->{'failed'}) { } 4 7 11
1328 4 push @invalid_list, $failed;
1329 4 $constraints_seen{\$c} = 1;
1330 }
1331 else {
1332 7 ▶ $valid->{$field}->[$i] = $match if $untaint_this;
Branch: 1 branch
BranchTrueFalseTotal
if $untaint_this 0 7 7
1333 }
1334 }
1335 }
1336 }
1337 else {
1338 150 my @params = $self->_constraint_input_build($c,$valid->{$field},\%param_data);
1339  
1340 # set current constraint field for use by get_current_constraint_value
1341 150 $self->{__CURRENT_CONSTRAINT_VALUE} = $valid->{$field};
1342  
1343 150 my ($match,$failed) = $self->_constraint_check_match($c,\@params,$untaint_this);
1344 150 ▶ if ($failed->{failed}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($failed->{'failed'}) { } 75 75 150
1345 75 push @invalid_list, $failed
1346 }
1347 else {
1348 75 ▶ $valid->{$field} = $match if $untaint_this;
Branch: 1 branch
BranchTrueFalseTotal
if $untaint_this 12 63 75
1349 }
1350 }
1351 }
1352  
1353 143 ▶ if (@invalid_list) {
Branch: 1 branch
BranchTrueFalseTotal
if (@invalid_list) 71 72 143
1354 71 my @failed = map { $_->{name} } @invalid_list;
1355 71 push @{ $self->{invalid}{$field} }, @failed;
1356 # the older interface to validate returned things differently
1357 71 ▶ push @{ $self->{validate_invalid} }, $is_constraint_list ? [$field, @failed] : $field;
Branch: 1 branch
BranchTrueFalseTotal
$is_constraint_list ? : 12 59 71
1358 }
1359 }
1360 }
1361  
1362 59 1;
1363  
1364 __END__
↑ Summary blib/lib/Data/FormValidator/Constraints/Upload.pm »