Class/Prototype.pm

filter: Statementstmt 89.1% 715 / 802 Branchbran 69.6% 291 / 418 Conditioncond 46.3% 76 / 164 MC/DCmcdc 10.3% 11 / 106 Subroutinesub 85.1% 92 / 108 Podpod 45.9% 28 / 61 totaltotal 73.1% 1213 / 1659 CC 290 SCAR 21.2
CC
290
Cov
78%
validate_fields_specified
28.1
__ANON__
26.1
error_fields
20.0
CRAP 8.3
« Class.pm ↑ Summary Class/Base.pm »
1 # ABSTRACT: Data Validation Engine for Validation::Class Classes
2  
3 package Validation::Class::Prototype;
4 {
5 56 $Validation::Class::Prototype::VERSION = '7.10_01';
6 }
7  
8 56 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 56 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 56 our $VERSION = '7.10_01'; # VERSION
12  
13 56 ▶use base 'Validation::Class::Backwards'; # I'm pro-life
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 56 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 56 ▶use Hash::Merge 'merge';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 56 ▶use Hash::Flatten;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 56 ▶use Validation::Class::Base 'has', 'hold';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 56 ▶use Validation::Class::Params;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 56 ▶use Validation::Class::Errors;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 56 ▶use Validation::Class::Fields;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66 # is never changed so direct access is OK
67 63 ▶hold 'directives' => sub { shift->{config}->{DIRECTIVES} || {} };
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"config"}{"DIRECTIVES"} or {} 0 63 63
68  
69  
70 0 ▶hold 'errors' => sub {[]};
Subroutine __ANON__: not called CC=1 SCAR=1.2
71  
72  
73 0 ▶hold 'fields' => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
74  
75  
76 56 has 'filtering' => 'pre';
77  
78  
79 0 ▶hold 'filters' => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
80  
81  
82 has 'hash_inflator' => sub {
83
84 67 ▶ my $options = @_ > 1 ? pop @_ : {
Subroutine __ANON__: called CC=4 SCAR=14.0
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 67 67
85 hash_delimiter => '.',
86 array_delimiter => ':',
87 escape_sequence => '',
88 };
89
90 67 foreach my $option (keys %{$options}) {
91
92 201 ▶ if ($option =~ /\_/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option =~ /\_/) 201 0 201
93
94 201 my $cc_option = $option;
95
96 201 $cc_option =~ s/([a-zA-Z])\_([a-zA-Z])/$1\u$2/gi;
97
98 201 $options->{ucfirst $cc_option} = $options->{$option};
99
100 201 delete $options->{$option};
101
102 }
103
104 }
105  
106 67 return $options;
107
108 56 };
109  
110  
111 56 has 'ignore_failure' => '1';
112  
113  
114 56 has 'ignore_unknown' => '0';
115  
116  
117 0 ▶hold 'methods' => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
118  
119  
120 0 ▶hold 'mixins' => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
121  
122  
123 0 ▶hold 'params' => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
124  
125  
126 0 ▶has plugins => sub { shift->{config}->{PLUGINS} || {} };
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->{"config"}{"PLUGINS"} or {} 0 0 0
127  
128  
129 0 ▶hold 'profiles' => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
130  
131  
132 37 ▶has 'queued' => sub {[]};
Subroutine __ANON__: called CC=1 SCAR=0.0
133  
134  
135 # class relatives (child-classes) store
136 0 ▶hold 'relatives' => sub {{}};
Subroutine __ANON__: not called CC=1 SCAR=1.2
137  
138  
139 # switch: report method requirement failures
140 56 has 'report_failure' => 0;
141  
142  
143 # switch: report unknown input parameters
144 56 has 'report_unknown' => 0;
145  
146 # stash object storage
147 15 ▶has 'stashed' => sub {{}};
Subroutine __ANON__: called CC=1 SCAR=0.0
148  
149 # hash of directives by type
150 hold 'types' => sub {
151
152 63 ▶ my $self = shift;
Subroutine __ANON__: called CC=4 SCAR=13.9
153 63 my $types = { mixin => {}, field => {} };
154 63 my $dirts = $self->directives;
155
156 # build types hash from directives by their usability
157 63 ▶ while (my($name, $directive) = each(%{$dirts})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$dirts;}) 1953 63 2016
158
159 1953 ▶ $types->{mixin}->{$name} = $directive if $dirts->{$name}->{mixin};
Branch: 1 branch
BranchTrueFalseTotal
if $dirts->{$name}{"mixin"} 1323 630 1953
160 1953 ▶ $types->{field}->{$name} = $directive if $dirts->{$name}->{field};
Branch: 1 branch
BranchTrueFalseTotal
if $dirts->{$name}{"field"} 1953 0 1953
161
162 }
163
164 63 return $types;
165
166 56 };
167  
168 sub apply_filter {
169  
170 205 ▶ my ($self, $filter, $field) = @_;
Subroutine apply_filter: called CC=6 SCAR=18.0
Pod undocumented
171  
172 205 ▶ if ($self->params->has($field)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->params->has($field)) 89 116 205
173
174 89 ▶ if ($self->filters->{$filter} || "CODE" eq ref $filter) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->filters->{$filter} or "CODE" eq ref $filter) 89 0 89
Condition66%2/3
$self->filters->{$filter} or "CODE" eq ref $filter
l!l&&r!l&&!r
8720
MC/DC0%0/2
$self->filters->{$filter} or "CODE" eq ref $filter
$self->filters->{$filter} "CODE" eq ref $filter
Truth table66%2/3
A or B
A= $self->filters->{$filter}B= "CODE" eq ref $filter
ABresult
1X1
011
000
175
176 89 ▶ if ($self->params->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->params->{$field}) 77 12 89
177
178 my $code = "CODE" eq ref $filter ?
179 77 ▶ $filter : $self->filters->{$filter};
Branch: 1 branch
BranchTrueFalseTotal
'CODE' eq ref $filter ? : 2 75 77
180
181 $self->set_value(
182 77 $field => $code->($self->params->{$field})
183 );
184
185 }
186
187 }
188
189 }
190
191 205 return $self;
192  
193 }
194  
195  
196 sub apply_filters {
197
198 295 ▶ my ($self, $state) = @_;
Subroutine apply_filters: called CC=2 SCAR=7.0
Pod documented
199
200 295 ▶ $state ||= 'pre'; # state defaults to (pre) filtering
Condition50%1/2
$state ||= "pre"
l!l
2950
MC/DC0%0/1
$state ||= "pre"
$state
Truth table50%1/2
A
A= $state
Aresult
11
00
201
202 # check for and process input filters and default values
203 my $process = sub {
204
205 558 ▶ my ($name, $config) = @_;
Subroutine __ANON__: called CC=7 SCAR=19.5
206
207 558 ▶ if ($config->filtering eq $state) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->filtering eq $state) 382 176 558
208
209 # the filters directive should always be an arrayref
210 382 ▶ $config->filters([$config->filters])
Branch: 1 branch
BranchTrueFalseTotal
unless "ARRAY" eq ref $config->filters 15 367 382
211 unless "ARRAY" eq ref $config->filters;
212
213 # apply filters
214 382 $self->apply_filter($_, $name) for @{$config->filters};
215
216 # set default value - absolute last resort
217 382 ▶ if ($self->params->has($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->params->has($name)) 234 148 382
218
219 234 ▶ if (!$self->params->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->params->{$name}) 14 220 234
220
221 14 ▶ if (defined $config->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $config->{"default"}) 0 14 14
222
223 0 $self->params->{$name} =
224 $self->get_value($name);
225
226 }
227
228 }
229
230 }
231
232 }
233
234 295 };
235
236 295 $self->fields->each($process);
237
238 295 return $self;
239  
240 }
241  
242 sub apply_mixin {
243  
244 183 ▶ my ($self, $field, $mixin) = @_;
Subroutine apply_mixin: called CC=4 SCAR=13.9
Pod undocumented
245  
246 # mixin values should be in arrayref form
247
248 183 ▶ my $mixins = ref($mixin) eq "ARRAY" ? $mixin : [$mixin];
Branch: 1 branch
BranchTrueFalseTotal
ref $mixin eq 'ARRAY' ? : 6 177 183
249  
250 183 foreach my $mixin (@{$mixins}) {
251
252 189 ▶ if (defined $self->{mixins}->{$mixin}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"mixins"}{$mixin}) 148 41 189
253
254 $self->fields->{$field} = $self->merge_mixin(
255 $self->fields->{$field},
256 148 $self->{mixins}->{$mixin}
257 );
258
259 }
260
261 }
262  
263 183 return $self;
264  
265 }
266  
267 sub apply_mixin_field {
268  
269 30 ▶ my ($self, $field, $target) = @_;
Subroutine apply_mixin_field: called CC=7 SCAR=19.5
Pod undocumented
270
271 30 $self->check_field( $field, $self->fields->{$field} );
272  
273 # some overwriting restricted
274
275 my $name = $self->fields->{$target}->{name}
276 30 ▶ if defined $self->fields->{$target}->{name};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->fields->{$target}{"name"} 30 0 30
277
278 my $label = $self->fields->{$target}->{label}
279 30 ▶ if defined $self->fields->{$target}->{label};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->fields->{$target}{"label"} 23 7 30
280  
281 # merge
282
283 $self->fields->{$target} = $self->merge_field(
284 $self->fields->{$target},
285 30 $self->fields->{$field}
286 );
287
288 # restore
289  
290 30 ▶ $self->fields->{$target}->{name} = $name if defined $name;
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 30 0 30
291 30 ▶ $self->fields->{$target}->{label} = $label if defined $label;
Branch: 1 branch
BranchTrueFalseTotal
if defined $label 23 7 30
292  
293 30 foreach my $key ( keys %{$self->fields->{$field}}) {
294
295 284 ▶ $self->apply_mixin( $target, $key ) if $key eq 'mixin';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "mixin" 19 265 284
296
297 }
298  
299 30 return $self;
300  
301 }
302  
303 sub apply_validator {
304  
305 182 ▶ my ( $self, $field_name, $field ) = @_;
Subroutine apply_validator: called CC=16 SCAR=27.7
Pod undocumented
306  
307 # does field have a label, if not use field name (e.g. for errors, etc)
308
309 182 ▶ my $name = $field->{label} ? $field->{label} : $field_name;
Branch: 1 branch
BranchTrueFalseTotal
$field->{'label'} ? : 97 85 182
310 182 my $value = $field->{value} ;
311  
312 # check if required
313
314 182 ▶ my $req = $field->{required} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$field->{'required'} ? : 53 129 182
315
316 182 ▶ if (defined $field->{':toggle'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->{":toggle"}) 24 158 182
317
318 24 ▶ $req = 1 if $field->{':toggle'} eq '+';
Branch: 1 branch
BranchTrueFalseTotal
if $field->{":toggle"} eq "+" 23 1 24
319 24 ▶ $req = 0 if $field->{':toggle'} eq '-';
Branch: 1 branch
BranchTrueFalseTotal
if $field->{":toggle"} eq "-" 1 23 24
320
321 }
322
323 182 ▶ if ( $req && ( !defined $value || $value eq '' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req and !defined($value) || $value eq "") 19 163 182
Condition100%6/6
$req and !defined($value) || $value eq ""
!ll&&!rl&&r
1244810
not defined $value or $value eq ""
l!l&&r!l&&!r
11839
MC/DC66%2/3
$req and !defined($value) || $value eq ""
$req not defined $value $value eq ""
Truth table75%3/4
A and not(B) or C
A= $reqB= not defined $valueC= $value eq ""
ABCresult
0XX0
1000
11X1
1011
324
325 my $error = defined $field->{error} ?
326 19 ▶ $field->{error} : "$name is required";
Branch: 1 branch
BranchTrueFalseTotal
defined $field->{'error'} ? : 12 7 19
327
328 19 $field->{errors}->add($error);
329
330 19 return $self; # if required and fails, stop processing immediately
331
332 }
333  
334 163 ▶ if ( $req || $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req or $value) 153 10 163
Condition100%3/3
$req or $value
l!l&&r!l&&!r
3911410
MC/DC100%2/2
$req or $value
$req $value
Truth table100%3/3
A or B
A= $reqB= $value
ABresult
1X1
011
000
335  
336 # find and process all the validators
337
338 153 foreach my $key (keys %{$field}) {
339
340 1327 my $directive = $self->directives->{$key};
341
342 1327 ▶ if ($directive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($directive) 1327 0 1327
343
344 1327 ▶ if ($directive->{validator}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($directive->{"validator"}) 188 1139 1327
345
346 188 ▶ if ("CODE" eq ref $directive->{validator}) {
Branch: 1 branch
BranchTrueFalseTotal
if ("CODE" eq ref $directive->{"validator"}) 188 0 188
347
348 # execute validator directives
349 $directive->{validator}->(
350 188 $field->{$key}, $value, $field, $self
351 );
352
353 }
354
355 }
356
357 }
358
359 }
360  
361 }
362  
363 163 return $self;
364  
365 }
366  
367  
368 sub class {
369
370 12 ▶ my ( $self, $class, %args ) = @_;
Subroutine class: called CC=6 SCAR=18.0
Pod documented
371
372 12 ▶ return 0 unless defined $self->relatives->{$class};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->relatives->{$class} 0 12 12
373
374 12 my @attrs = qw(
375 hash_inflator
376 ignore_failure
377 ignore_unknown
378 report_failure
379 report_unknown
380 ); # to be copied (stash and params copied later)
381
382 12 my $delimiter = $self->hash_inflator->{'HashDelimiter'};
383
384 12 my %defaults = ( map { $_ => $self->$_ } @attrs );
385
386 12 $defaults{'stash'} = $self->stash; # copy stash
387 12 $defaults{'params'} = $self->get_params; # copy params
388
389 12 my %settings = %{ merge(\%args, \%defaults) };
390
391 12 my $child = $self->relatives->{$class}->new(%settings);
392
393 12 my $proto = $child->proto;
394  
395 12 ▶ if (defined $settings{'params'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $settings{"params"}) 12 0 12
396
397 12 $delimiter =~ s/([\.\+\-\:\,\\\/])/\\$1/g;
398
399 12 foreach my $name ($proto->params->keys) {
400
401 14 ▶ if ($name =~ /^$class$delimiter(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^$class$delimiter(.*)/) 2 12 14
402
403 2 ▶ if ($proto->fields->has($1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto->fields->has($1)) 2 0 2
404
405 2 push @{$proto->fields->{$1}->{alias}}, $name;
406
407 }
408
409 }
410
411 }
412  
413 }
414
415 12 return $child;
416  
417 }
418  
419 sub check_field {
420
421 494 ▶ my ( $self, $name, $spec ) = @_;
Subroutine check_field: called CC=3 SCAR=11.8
Pod undocumented
422  
423 494 my $directives = $self->types->{field};
424  
425 494 foreach ( keys %{$spec} ) {
426
427 # check if the field's directives are registered
428
429 3650 ▶ if ( ! defined $directives->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $directives->{$_}) 0 3650 3650
430
431 0 my $error = qq{
432 The $_ directive supplied by the $name field is not supported
433 };
434  
435 0 $self->pitch_error($error);
436
437 }
438
439 }
440  
441 494 return 1;
442  
443 }
444  
445 sub check_mixin {
446
447 74 ▶ my ( $self, $mixin, $spec ) = @_;
Subroutine check_mixin: called CC=4 SCAR=16.1
Pod undocumented
448  
449 74 my $directives = $self->types->{mixin};
450  
451 74 foreach ( keys %{$spec} ) {
452
453 200 ▶ if ( ! defined $directives->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $directives->{$_}) 0 200 200
454
455 0 my $error = qq{
456 The $_ directive supplied by the $mixin mixin is not supported
457 };
458
459 0 $self->pitch_error($error);
460
461 }
462
463 200 ▶ if ( ! $directives->{$_} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($directives->{$_}) 0 200 200
464
465 0 my $error = qq{
466 The $_ directive supplied by the $mixin mixin is empty
467 };
468
469 0 $self->pitch_error($error);
470
471 }
472
473 }
474  
475 74 return 1;
476  
477 }
478  
479  
480 sub clear_queue {
481
482 2 ▶ my $self = shift @_;
Subroutine clear_queue: called CC=2 SCAR=6.9
Pod documented
483
484 2 my @names = @{$self->queued};
485
486 2 $self->queued([]);
487
488 2 ▶ for (my $i = 0; $i < @names; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @names) 3 2 5
489
490 3 $names[$i] =~ s/^[\-\+]{1}//;
491 3 $_[$i] = $self->param($names[$i]);
492
493 }
494
495 2 return @_;
496  
497 }
498  
499  
500 sub clone {
501
502 9 ▶ my ($self, $field_name, $new_field_name, $directives) = @_;
Subroutine clone: called CC=3 SCAR=11.0
Pod documented
503
504 9 ▶ $directives ||= {};
Condition100%2/2
$directives ||= {}
l!l
72
MC/DC100%1/1
$directives ||= {}
$directives
Truth table100%2/2
A
A= $directives
Aresult
11
00
505
506 9 ▶ $directives->{name} = $new_field_name unless $directives->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $directives->{"name"} 9 0 9
507
508 # build a new field from an existing one during runtime
509 9 $self->fields->{$new_field_name} =
510 Validation::Class::Field->new($directives);
511
512 9 $self->apply_mixin_field( $field_name, $new_field_name );
513
514 9 return $self;
515
516 }
517  
518 sub configuration {
519
520 return {
521
522 ATTRIBUTES => {},
523
524 BUILDERS => [],
525
526 DIRECTIVES => {
527
528 ':toggle' => {
529
530 mixin => 0,
531 field => 1,
532 multi => 0
533
534 },
535
536 alias => {
537
538 mixin => 0,
539 field => 1,
540 multi => 1
541
542 },
543
544 between => {
545
546 mixin => 1,
547 field => 1,
548 multi => 0,
549
550 validator => \&configuration_validator_between
551
552 },
553
554 default => {
555
556 mixin => 1,
557 field => 1,
558 multi => 1
559
560 },
561
562 depends_on => {
563
564 mixin => 1,
565 field => 1,
566 multi => 1,
567
568 validator => \&configuration_validator_depends_on
569
570 },
571
572 error => {
573
574 mixin => 0,
575 field => 1,
576 multi => 0
577
578 },
579
580 errors => {
581
582 mixin => 0,
583 field => 1,
584 multi => 0
585
586 },
587
588 filters => {
589
590 mixin => 1,
591 field => 1,
592 multi => 1
593
594 },
595
596 filtering => {
597
598 mixin => 1,
599 field => 1,
600 multi => 1
601
602 },
603
604 label => {
605
606 mixin => 0,
607 field => 1,
608 multi => 0
609
610 },
611
612 length => {
613
614 mixin => 1,
615 field => 1,
616 multi => 0,
617
618 validator => \&configuration_validator_length
619
620 },
621
622 matches => {
623
624 mixin => 1,
625 field => 1,
626 multi => 0,
627
628 validator => \&configuration_validator_matches
629
630 },
631
632 max_alpha => {
633
634 mixin => 1,
635 field => 1,
636 multi => 0,
637
638 validator => \&configuration_validator_max_alpha
639
640 },
641
642 max_digits => {
643
644 mixin => 1,
645 field => 1,
646 multi => 0,
647
648 validator => \&configuration_validator_max_digits
649
650 },
651
652 max_length => {
653
654 mixin => 1,
655 field => 1,
656 multi => 0,
657
658 validator => \&configuration_validator_max_length
659
660 },
661
662 max_sum => {
663
664 mixin => 1,
665 field => 1,
666 multi => 0,
667
668 validator => \&configuration_validator_max_sum
669
670 },
671
672 max_symbols => {
673
674 mixin => 1,
675 field => 1,
676 multi => 0,
677
678 validator => \&configuration_validator_max_symbols
679
680 },
681
682 min_alpha => {
683
684 mixin => 1,
685 field => 1,
686 multi => 0,
687
688 validator => \&configuration_validator_min_alpha
689
690 },
691
692 min_digits => {
693
694 mixin => 1,
695 field => 1,
696 multi => 0,
697
698 validator => \&configuration_validator_min_digits
699
700 },
701
702 min_length => {
703
704 mixin => 1,
705 field => 1,
706 multi => 0,
707
708 validator => \&configuration_validator_min_length
709
710 },
711
712 min_sum => {
713
714 mixin => 1,
715 field => 1,
716 multi => 0,
717
718 validator => \&configuration_validator_min_sum
719
720 },
721
722 min_symbols => {
723
724 mixin => 1,
725 field => 1,
726 multi => 0,
727
728 validator => \&configuration_validator_min_symbols
729
730 },
731
732 mixin => {
733
734 mixin => 0,
735 field => 1,
736 multi => 1
737
738 },
739
740 mixin_field => {
741
742 mixin => 0,
743 field => 1,
744 multi => 0
745
746 },
747
748 name => {
749
750 mixin => 0,
751 field => 1,
752 multi => 0
753
754 },
755
756 options => {
757
758 mixin => 1,
759 field => 1,
760 multi => 0,
761
762 validator => \&configuration_validator_options
763
764 },
765
766 pattern => {
767
768 mixin => 1,
769 field => 1,
770 multi => 0,
771
772 validator => \&configuration_validator_pattern
773
774 },
775
776 readonly => {
777
778 mixin => 0,
779 field => 1,
780 multi => 0
781
782 },
783
784 required => {
785
786 mixin => 1,
787 field => 1,
788 multi => 0
789
790 },
791
792 validation => {
793
794 mixin => 0,
795 field => 1,
796 multi => 0
797
798 },
799
800 value => {
801
802 mixin => 1,
803 field => 1,
804 multi => 1
805
806 }
807
808 },
809
810 FIELDS => {},
811
812 FILTERS => {
813
814 alpha => sub {
815
816 3 ▶ $_[0] =~ s/[^A-Za-z]//g;
Subroutine __ANON__: called CC=1 SCAR=0.0
817 3 $_[0];
818
819 },
820
821 alphanumeric => sub {
822
823 21 ▶ $_[0] =~ s/[^A-Za-z0-9]//g;
Subroutine __ANON__: called CC=1 SCAR=0.0
824 21 $_[0];
825
826 },
827
828 capitalize => sub {
829
830 1 ▶ $_[0] = ucfirst $_[0];
Subroutine __ANON__: called CC=1 SCAR=0.0
831 1 $_[0] =~ s/\.\s+([a-z])/\. \U$1/g;
832 1 $_[0];
833
834 },
835
836 decimal => sub {
837
838 1 ▶ $_[0] =~ s/[^0-9\.\,]//g;
Subroutine __ANON__: called CC=1 SCAR=0.0
839 1 $_[0];
840
841 },
842
843 lowercase => sub {
844
845 14 ▶ lc $_[0];
Subroutine __ANON__: called CC=1 SCAR=0.0
846
847 },
848
849 numeric => sub {
850
851 1 ▶ $_[0] =~ s/\D//g;
Subroutine __ANON__: called CC=1 SCAR=0.0
852 1 $_[0];
853
854 },
855
856 strip => sub {
857
858 14 ▶ $_[0] =~ s/\s+/ /g;
Subroutine __ANON__: called CC=1 SCAR=0.0
859 14 $_[0] =~ s/^\s+//;
860 14 $_[0] =~ s/\s+$//;
861 14 $_[0];
862
863 },
864
865 titlecase => sub {
866
867 3 ▶ join( " ", map ( ucfirst, split( /\s/, lc $_[0] ) ) );
Subroutine __ANON__: called CC=1 SCAR=0.0
868
869 },
870
871 trim => sub {
872
873 14 ▶ $_[0] =~ s/^\s+//g;
Subroutine __ANON__: called CC=1 SCAR=0.0
874 14 $_[0] =~ s/\s+$//g;
875 14 $_[0];
876
877 },
878
879 uppercase => sub {
880
881 3 ▶ uc $_[0];
Subroutine __ANON__: called CC=1 SCAR=0.0
882
883 }
884
885 },
886
887 142 ▶ METHODS => {},
Subroutine configuration: called CC=1 SCAR=0.0
Pod undocumented
888
889 MIXINS => {},
890
891 PLUGINS => {},
892
893 PROFILES => {},
894
895 RELATIVES => {},
896
897 }
898
899 }
900  
901 sub configuration_validator_between {
902
903 6 ▶ my ($directive, $value, $field, $class) = @_;
Subroutine configuration_validator_between: called CC=7 SCAR=21.0
Pod undocumented
904
905 my ($min, $max) = "ARRAY" eq ref $directive ?
906 6 ▶ @{$directive} : split /(?:\s{1,})?[,\-]{1,}(?:\s{1,})?/, $directive;
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $directive ? : 0 6 6
907
908 6 $min = scalar($min);
909 6 $max = scalar($max);
910
911 6 $value = length($value);
912
913 6 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 6 0 6
914
915 6 ▶ unless ($value >= $min && $value <= $max) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value >= $min and $value <= $max) 1 5 6
Condition66%2/3
$value >= $min and $value <= $max
!ll&&!rl&&r
105
MC/DC50%1/2
$value >= $min and $value <= $max
$value >= $min $value <= $max
Truth table66%2/3
A and B
A= $value >= $minB= $value <= $max
ABresult
0X0
100
111
916
917 1 ▶ my $handle = $field->{label} || $field->{name};
Condition33%1/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table33%1/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
918 1 my $error = "$handle must contain between $directive characters";
919
920 1 ▶ $field->errors->add($field->{error} || $error);
Condition33%1/3
$field->{'error'} || $error
l!l&&r!l&&!r
010
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table33%1/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
921
922 1 return 0;
923
924 }
925
926 }
927
928 5 return 1;
929
930 }
931  
932 sub configuration_validator_depends_on {
933  
934 3 ▶ my ($directive, $value, $field, $class) = @_;
Subroutine configuration_validator_depends_on: called CC=11 SCAR=24.8
Pod undocumented
935
936 3 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 3 0 3
937
938 3 ▶ my $dependents = "ARRAY" eq ref $directive ?
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $directive ? : 1 2 3
939 $directive : [$directive];
940
941 3 ▶ if (@{$dependents}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$dependents;}) 3 0 3
942
943 3 my @blanks = ();
944  
945 3 foreach my $dep (@{$dependents}) {
946  
947 push @blanks,
948 $class->fields->{$dep}->{label} ||
949 $class->fields->{$dep}->{name}
950 3 ▶ if ! $class->param($dep);
Branch: 1 branch
BranchTrueFalseTotal
unless $class->param($dep) 2 1 3
Condition66%2/3
$class->fields->{$dep}{'label'} || $class->fields->{$dep}{'name'}
l!l&&r!l&&!r
110
MC/DC0%0/2
$class->fields->{$dep}{'label'} || $class->fields->{$dep}{'name'}
$class->fields->{$dep}{'label'} $class->fields->{$dep}{'name'}
Truth table66%2/3
A || B
A= $class->fields->{$dep}{'label'}B= $class->fields->{$dep}{'name'}
ABresult
1X1
011
000
951  
952 }
953
954 3 ▶ if (@blanks) {
Branch: 1 branch
BranchTrueFalseTotal
if (@blanks) 2 1 3
955  
956 2 ▶ my $handle = $field->{label} || $field->{name};
Condition66%2/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
110
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table66%2/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
957
958 2 ▶ my $error = "$handle requires " . join(", ", @blanks) .
Branch: 1 branch
BranchTrueFalseTotal
@blanks > 1 ? : 0 2 2
959 " to have " . (@blanks > 1 ? "values" : "a value");
960
961 2 ▶ $field->errors->add($field->{error} || $error);
Condition33%1/3
$field->{'error'} || $error
l!l&&r!l&&!r
020
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table33%1/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
962  
963 2 return 0;
964  
965 }
966  
967 }
968
969 }
970
971 1 return 1;
972  
973 }
974  
975 sub configuration_validator_length {
976  
977 3 ▶ my ($directive, $value, $field, $class) = @_;
Subroutine configuration_validator_length: called CC=6 SCAR=18.6
Pod undocumented
978
979 3 $value = length($value);
980
981 3 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 3 0 3
982  
983 3 ▶ unless ($value == $directive) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value == $directive) 2 1 3
984  
985 2 ▶ my $handle = $field->{label} || $field->{name};
Condition33%1/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
020
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table33%1/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
986 2 ▶ my $characters = $directive > 1 ?
Branch: 1 branch
BranchTrueFalseTotal
$directive > 1 ? : 1 1 2
987 "characters" : "character";
988
989 2 my $error = "$handle must contain exactly " .
990 "$directive $characters";
991
992 2 ▶ $field->errors->add($field->{error} || $error);
Condition33%1/3
$field->{'error'} || $error
l!l&&r!l&&!r
020
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table33%1/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
993  
994 2 return 0;
995  
996 }
997  
998 }
999  
1000 1 return 1;
1001  
1002 }
1003  
1004 sub configuration_validator_matches {
1005  
1006 4 ▶ my ( $directive, $value, $field, $class ) = @_;
Subroutine configuration_validator_matches: called CC=7 SCAR=19.9
Pod undocumented
1007  
1008 4 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 4 0 4
1009
1010 # build the regex
1011 4 my $this = $value;
1012 4 ▶ my $that = $class->param($directive) || '';
Condition100%2/2
$class->param($directive) || ''
l!l
31
MC/DC100%1/1
$class->param($directive) || ''
$class->param($directive)
Truth table100%2/2
A
A= $class->param($directive)
Aresult
11
00
1013
1014 4 ▶ unless ( $this eq $that ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this eq $that) 3 1 4
1015
1016 3 ▶ my $handle = $field->{label} || $field->{name};
Condition66%2/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
210
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table66%2/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
1017 my $handle2 = $class->fields->{$directive}->{label}
1018 3 ▶ || $class->fields->{$directive}->{name};
Condition66%2/3
$class->fields->{$directive}{'label'} || $class->fields->{$directive}{'name'}
l!l&&r!l&&!r
210
MC/DC0%0/2
$class->fields->{$directive}{'label'} || $class->fields->{$directive}{'name'}
$class->fields->{$directive}{'label'} $class->fields->{$directive}{'name'}
Truth table66%2/3
A || B
A= $class->fields->{$directive}{'label'}B= $class->fields->{$directive}{'name'}
ABresult
1X1
011
000
1019
1020 3 my $error = "$handle does not match $handle2";
1021
1022 3 ▶ $field->errors->add($field->{error} || $error);
Condition33%1/3
$field->{'error'} || $error
l!l&&r!l&&!r
030
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table33%1/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
1023
1024 3 return 0;
1025
1026 }
1027  
1028 }
1029  
1030 1 return 1;
1031  
1032 }
1033  
1034 sub configuration_validator_max_alpha {
1035  
1036 4 ▶ my ( $directive, $value, $field, $class ) = @_;
Subroutine configuration_validator_max_alpha: called CC=6 SCAR=19.1
Pod undocumented
1037  
1038 4 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 4 0 4
1039  
1040 4 my @i = ($value =~ /[a-zA-Z]/g);
1041
1042 4 ▶ unless ( @i <= $directive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@i <= $directive) 1 3 4
1043
1044 1 ▶ my $handle = $field->{label} || $field->{name};
Condition33%1/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table33%1/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
1045 1 ▶ my $characters = int( $directive ) > 1 ?
Branch: 1 branch
BranchTrueFalseTotal
int $directive > 1 ? : 1 0 1
1046 "characters" : "character";
1047
1048 1 my $error = "$handle must contain at-least "
1049 . "$directive alphabetic $characters";
1050
1051 1 ▶ $field->errors->add($field->{error} || $error);
Condition33%1/3
$field->{'error'} || $error
l!l&&r!l&&!r
010
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table33%1/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
1052
1053 1 return 0;
1054
1055 }
1056  
1057 }
1058  
1059 3 return 1;
1060  
1061 }
1062  
1063 sub configuration_validator_max_digits {
1064  
1065 4 ▶ my ( $directive, $value, $field, $class ) = @_;
Subroutine configuration_validator_max_digits: called CC=6 SCAR=19.1
Pod undocumented
1066  
1067 4 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 4 0 4
1068  
1069 4 my @i = ($value =~ /[0-9]/g);
1070  
1071 4 ▶ unless ( @i <= $directive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@i <= $directive) 1 3 4
1072  
1073 1 ▶ my $handle = $field->{label} || $field->{name};
Condition33%1/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table33%1/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
1074 1 ▶ my $characters = int( $directive ) > 1 ?
Branch: 1 branch
BranchTrueFalseTotal
int $directive > 1 ? : 1 0 1
1075 "digits" : "digit";
1076
1077 1 my $error = "$handle must contain at-least "
1078 ."$directive $characters";
1079
1080 1 ▶ $field->errors->add($field->{error} || $error);
Condition33%1/3
$field->{'error'} || $error
l!l&&r!l&&!r
010
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table33%1/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
1081
1082 1 return 0;
1083  
1084 }
1085  
1086 }
1087  
1088 3 return 1;
1089  
1090 }
1091  
1092 sub configuration_validator_max_length {
1093  
1094 50 ▶ my ( $directive, $value, $field, $class ) = @_;
Subroutine configuration_validator_max_length: called CC=6 SCAR=18.7
Pod undocumented
1095  
1096 50 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 50 0 50
1097  
1098 50 ▶ unless ( length($value) <= $directive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $value <= $directive) 7 43 50
1099  
1100 7 ▶ my $handle = $field->{label} || $field->{name};
Condition33%1/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
070
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table33%1/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
1101 7 ▶ my $characters = int( $directive ) > 1 ?
Branch: 1 branch
BranchTrueFalseTotal
int $directive > 1 ? : 7 0 7
1102 "characters" : "character";
1103
1104 7 my $error = "$handle can't contain more than "
1105 ."$directive $characters";
1106
1107 7 ▶ $field->errors->add($field->{error} || $error);
Condition66%2/3
$field->{'error'} || $error
l!l&&r!l&&!r
610
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table66%2/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
1108  
1109 7 return 0;
1110  
1111 }
1112  
1113 }
1114  
1115 43 return 1;
1116  
1117 }
1118  
1119 sub configuration_validator_max_sum {
1120  
1121 4 ▶ my ( $directive, $value, $field, $class ) = @_;
Subroutine configuration_validator_max_sum: called CC=5 SCAR=17.1
Pod undocumented
1122  
1123 4 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 4 0 4
1124  
1125 4 ▶ unless ( $value <= $directive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value <= $directive) 1 3 4
1126  
1127 1 ▶ my $handle = $field->{label} || $field->{name};
Condition33%1/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table33%1/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
1128 1 my $error = "$handle can't be greater than "
1129 ."$directive";
1130
1131 1 ▶ $field->errors->add($field->{error} || $error);
Condition33%1/3
$field->{'error'} || $error
l!l&&r!l&&!r
010
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table33%1/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
1132
1133 1 return 0;
1134  
1135 }
1136  
1137 }
1138  
1139 3 return 1;
1140  
1141 }
1142  
1143 sub configuration_validator_max_symbols {
1144  
1145 4 ▶ my ( $directive, $value, $field, $class ) = @_;
Subroutine configuration_validator_max_symbols: called CC=6 SCAR=19.1
Pod undocumented
1146  
1147 4 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 4 0 4
1148  
1149 4 my @i = ($value =~ /[^0-9a-zA-Z]/g);
1150  
1151 4 ▶ unless ( @i <= $directive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@i <= $directive) 1 3 4
1152  
1153 1 ▶ my $handle = $field->{label} || $field->{name};
Condition33%1/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table33%1/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
1154 1 ▶ my $characters = int( $directive ) > 1 ?
Branch: 1 branch
BranchTrueFalseTotal
int $directive > 1 ? : 1 0 1
1155 "symbols" : "symbol";
1156
1157 1 my $error = "$handle can't contain more than "
1158 ."$directive $characters";
1159
1160 1 ▶ $field->errors->add($field->{error} || $error);
Condition33%1/3
$field->{'error'} || $error
l!l&&r!l&&!r
010
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table33%1/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
1161  
1162 1 return 0;
1163  
1164 }
1165  
1166 }
1167  
1168 3 return 1;
1169  
1170 }
1171  
1172 sub configuration_validator_min_alpha {
1173  
1174 4 ▶ my ( $directive, $value, $field, $class ) = @_;
Subroutine configuration_validator_min_alpha: called CC=6 SCAR=19.1
Pod undocumented
1175  
1176 4 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 4 0 4
1177  
1178 4 my @i = ($value =~ /[a-zA-Z]/g);
1179  
1180 4 ▶ unless ( @i >= $directive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@i >= $directive) 1 3 4
1181  
1182 1 ▶ my $handle = $field->{label} || $field->{name};
Condition33%1/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table33%1/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
1183 1 ▶ my $characters = int( $directive ) > 1 ?
Branch: 1 branch
BranchTrueFalseTotal
int $directive > 1 ? : 1 0 1
1184 "characters" : "character";
1185
1186 1 my $error = "$handle must contain at-least "
1187 ."$directive alphabetic $characters";
1188
1189 1 ▶ $field->errors->add($field->{error} || $error);
Condition33%1/3
$field->{'error'} || $error
l!l&&r!l&&!r
010
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table33%1/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
1190  
1191 1 return 0;
1192  
1193 }
1194  
1195 }
1196  
1197 3 return 1;
1198  
1199 }
1200  
1201 sub configuration_validator_min_digits {
1202  
1203 4 ▶ my ( $directive, $value, $field, $class ) = @_;
Subroutine configuration_validator_min_digits: called CC=6 SCAR=19.1
Pod undocumented
1204  
1205 4 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 4 0 4
1206  
1207 4 my @i = ($value =~ /[0-9]/g);
1208  
1209 4 ▶ unless ( @i >= $directive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@i >= $directive) 1 3 4
1210  
1211 1 ▶ my $handle = $field->{label} || $field->{name};
Condition33%1/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table33%1/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
1212 1 ▶ my $characters = int( $directive ) > 1 ?
Branch: 1 branch
BranchTrueFalseTotal
int $directive > 1 ? : 1 0 1
1213 "digits" : "digit";
1214
1215 1 my $error = "$handle must contain at-least "
1216 ."$directive $characters";
1217
1218 1 ▶ $field->errors->add($field->{error} || $error);
Condition33%1/3
$field->{'error'} || $error
l!l&&r!l&&!r
010
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table33%1/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
1219  
1220 1 return 0;
1221  
1222 }
1223  
1224 }
1225  
1226 3 return 1;
1227  
1228 }
1229  
1230 sub configuration_validator_min_length {
1231  
1232 68 ▶ my ( $directive, $value, $field, $class ) = @_;
Subroutine configuration_validator_min_length: called CC=6 SCAR=18.3
Pod undocumented
1233  
1234 68 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 68 0 68
1235  
1236 68 ▶ unless ( length($value) >= $directive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $value >= $directive) 13 55 68
1237  
1238 13 ▶ my $handle = $field->{label} || $field->{name};
Condition66%2/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
580
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table66%2/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
1239 13 ▶ my $characters = int( $directive ) > 1 ?
Branch: 1 branch
BranchTrueFalseTotal
int $directive > 1 ? : 13 0 13
1240 "characters" : "character";
1241
1242 13 my $error = "$handle must contain at-least "
1243 ."$directive $characters";
1244
1245 13 ▶ $field->errors->add($field->{error} || $error);
Condition66%2/3
$field->{'error'} || $error
l!l&&r!l&&!r
850
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table66%2/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
1246  
1247 13 return 0;
1248  
1249 }
1250  
1251 }
1252  
1253 55 return 1;
1254  
1255 }
1256  
1257 sub configuration_validator_min_sum {
1258  
1259 4 ▶ my ( $directive, $value, $field, $class ) = @_;
Subroutine configuration_validator_min_sum: called CC=5 SCAR=17.1
Pod undocumented
1260  
1261 4 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 4 0 4
1262  
1263 4 ▶ unless ( $value >= $directive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value >= $directive) 1 3 4
1264  
1265 1 ▶ my $handle = $field->{label} || $field->{name};
Condition33%1/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table33%1/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
1266 1 my $error = "$handle can't be less than "
1267 ."$directive";
1268
1269 1 ▶ $field->errors->add($field->{error} || $error);
Condition33%1/3
$field->{'error'} || $error
l!l&&r!l&&!r
010
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table33%1/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
1270  
1271 1 return 0;
1272  
1273 }
1274  
1275 }
1276  
1277 3 return 1;
1278  
1279 }
1280  
1281 sub configuration_validator_min_symbols {
1282  
1283 4 ▶ my ( $directive, $value, $field, $class ) = @_;
Subroutine configuration_validator_min_symbols: called CC=6 SCAR=19.1
Pod undocumented
1284  
1285 4 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 4 0 4
1286  
1287 4 my @i = ($value =~ /[^0-9a-zA-Z]/g);
1288  
1289 4 ▶ unless ( @i >= $directive ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@i >= $directive) 1 3 4
1290  
1291 1 ▶ my $handle = $field->{label} || $field->{name};
Condition33%1/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
100
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table33%1/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
1292 1 ▶ my $characters = int( $directive ) > 1 ?
Branch: 1 branch
BranchTrueFalseTotal
int $directive > 1 ? : 1 0 1
1293 "symbols" : "symbol";
1294
1295 1 my $error = "$handle must contain at-least "
1296 ."$directive $characters";
1297
1298 1 ▶ $field->errors->add($field->{error} || $error);
Condition33%1/3
$field->{'error'} || $error
l!l&&r!l&&!r
010
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table33%1/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
1299  
1300 1 return 0;
1301  
1302 }
1303  
1304 }
1305  
1306 3 return 1;
1307  
1308 }
1309  
1310 sub configuration_validator_options {
1311  
1312 4 ▶ my ( $directive, $value, $field, $class ) = @_;
Subroutine configuration_validator_options: called CC=6 SCAR=19.5
Pod undocumented
1313  
1314 4 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 4 0 4
1315  
1316 # build the regex
1317 my (@options) = "ARRAY" eq ref $directive ?
1318 4 ▶ @{$directive} : split /(?:\s{1,})?[,\-]{1,}(?:\s{1,})?/, $directive;
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $directive ? : 0 4 4
1319  
1320 4 ▶ unless ( grep { $value =~ /^$_$/ } @options ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$value =~ /^$_$/;} @options) 2 2 4
1321  
1322 2 ▶ my $handle = $field->{label} || $field->{name};
Condition33%1/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
020
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table33%1/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
1323
1324 2 my $error = "$handle must be " . join " or ", @options;
1325
1326 2 ▶ $field->errors->add($field->{error} || $error);
Condition33%1/3
$field->{'error'} || $error
l!l&&r!l&&!r
020
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table33%1/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
1327  
1328 2 return 0;
1329  
1330 }
1331  
1332 }
1333  
1334 2 return 1;
1335  
1336 }
1337  
1338 sub configuration_validator_pattern {
1339  
1340 18 ▶ my ( $directive, $value, $field, $class ) = @_;
Subroutine configuration_validator_pattern: called CC=6 SCAR=18.3
Pod undocumented
1341  
1342 18 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) 18 0 18
1343  
1344 # build the regex
1345 18 my $regex = $directive;
1346  
1347 18 ▶ unless ("Regexp" eq ref $regex) {
Branch: 1 branch
BranchTrueFalseTotal
unless ("Regexp" eq ref $regex) 14 4 18
1348  
1349 14 $regex =~ s/([^#X ])/\\$1/g;
1350 14 $regex =~ s/#/\\d/g;
1351 14 $regex =~ s/X/[a-zA-Z]/g;
1352 14 $regex = qr/$regex/;
1353  
1354 }
1355  
1356 18 ▶ unless ( $value =~ $regex ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$regex/) 4 14 18
1357  
1358 4 ▶ my $handle = $field->{label} || $field->{name};
Condition33%1/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
040
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table33%1/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
1359
1360 4 my $error = "$handle does not match the "
1361 ."pattern $directive";
1362
1363 4 ▶ $field->errors->add($field->{error} || $error);
Condition33%1/3
$field->{'error'} || $error
l!l&&r!l&&!r
040
MC/DC0%0/2
$field->{'error'} || $error
$field->{'error'} $error
Truth table33%1/3
A || B
A= $field->{'error'}B= $error
ABresult
1X1
011
000
1364  
1365 4 return 0;
1366  
1367 }
1368  
1369 }
1370  
1371 14 return 1;
1372  
1373 }
1374  
1375  
1376 sub error_count {
1377
1378 190 ▶ my ($self) = @_;
Subroutine error_count: called CC=2 SCAR=6.9
Pod documented
1379
1380 190 ▶ my $count = scalar($self->get_errors) || 0;
Condition100%2/2
scalar $self->get_errors || 0
l!l
86104
MC/DC100%1/1
scalar $self->get_errors || 0
scalar $self->get_errors
Truth table100%2/2
A
A= scalar $self->get_errors
Aresult
11
00
1381
1382 190 return $count;
1383
1384 }
1385  
1386  
1387 sub error_fields {
1388
1389 0 ▶ my ($self, @fields) = @_;
Subroutine error_fields: not called CC=4 SCAR=30.0
Pod documented
1390
1391 0 my $failed = {};
1392
1393 0 ▶ @fields = $self->fields->keys unless @fields;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields 0 0 0
1394
1395 0 foreach my $name (@fields) {
1396
1397 0 my $field = $self->fields->{$name};
1398
1399 0 ▶ if ($field->{errors}->count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{"errors"}->count) 0 0 0
1400
1401 0 $failed->{$name} = $field->{errors}->list;
1402
1403 }
1404
1405 }
1406
1407 0 return $failed;
1408  
1409 }
1410  
1411  
1412 sub errors_to_string {
1413
1414 31 ▶ my ($self, $delimiter, $transformer) = @_;
Subroutine errors_to_string: called CC=1 SCAR=0.0
Pod documented
1415
1416 31 my $errors = Validation::Class::Errors->new([]); # handle combined errors
1417
1418 31 $errors->add($self->{errors}->all);
1419
1420 $self->fields->each(sub{
1421
1422 63 ▶ $errors->add($_[1]->{errors}->all);
Subroutine __ANON__: called CC=1 SCAR=0.0
1423
1424 31 });
1425
1426 31 return $errors->to_string($delimiter, $transformer);
1427  
1428 }
1429  
1430  
1431 sub flatten_params {
1432  
1433 193 ▶ my ($self, $params) = @_;
Subroutine flatten_params: called CC=3 SCAR=11.0
Pod documented
1434
1435 193 ▶ return undef unless "HASH" eq ref $params;
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $params 0 193 193
1436
1437 193 my ($ad, $hd) = @{$self->hash_inflator}{
1438 193 'ArrayDelimiter', 'HashDelimiter'
1439 };
1440
1441 193 my $serializer = Hash::Flatten->new($self->hash_inflator);
1442
1443 193 ▶ return grep({ ref $_ } values %{$params}) ?
Branch: 1 branch
BranchTrueFalseTotal
grep({ref $_;} values %{$params;}) ? : 33 160 193
1444 # do not flatten params if suspected to have already been flattened
1445 $serializer->flatten($params) : $params;
1446
1447 }
1448  
1449  
1450 sub get_errors {
1451  
1452 193 ▶ my ($self, @criteria) = @_;
Subroutine get_errors: called CC=4 SCAR=18.5
Pod documented
1453
1454 193 my $errors = Validation::Class::Errors->new([]); # handle combined errors
1455
1456 193 ▶ if (!@criteria) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @criteria) { } 193 0 193
elsif ('REGEXP' eq uc ref $criteria[0]) { } 0 0 0
1457
1458 193 $errors->add($self->{errors}->all);
1459
1460 $self->fields->each(sub{
1461
1462 471 ▶ $errors->add($_[1]->{errors}->all);
Subroutine __ANON__: called CC=1 SCAR=0.0
1463
1464 193 });
1465
1466 }
1467
1468 elsif ("REGEXP" eq uc ref $criteria[0]) {
1469
1470 0 my $query = $criteria[0];
1471
1472 0 $errors->add($self->{errors}->find($query));
1473
1474 $self->fields->each(sub{
1475
1476 0 ▶ $errors->add($_[1]->{errors}->find($query));
Subroutine __ANON__: not called CC=1 SCAR=6.9
1477
1478 0 });
1479
1480 }
1481
1482 else {
1483
1484 0 for (@criteria) {
1485
1486 0 $errors->add($self->fields->{$_}->{errors}->all);
1487
1488 }
1489
1490 }
1491
1492 193 return ($errors->all);
1493  
1494 }
1495  
1496  
1497 sub get_fields {
1498  
1499 0 ▶ my ($self, @fields) = @_;
Subroutine get_fields: not called CC=4 SCAR=30.0
Pod documented
1500
1501 0 my $fields = {};
1502
1503 0 ▶ $self->fields->each(sub{ $fields->{$_[0]} = $_[1] || {} });
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[1] || {}
l!l
00
MC/DC0%0/1
$_[1] || {}
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
1504
1505 0 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) { } 0 0 0
1506
1507 0 ▶ return @fields ? (map { $fields->{$_} || undef } @fields) : ();
Branch: 2 branches
BranchTrueFalseTotal
@fields ? : 0 0 0
$fields->{$_} or undef 0 0 0
1508
1509 }
1510
1511 else {
1512
1513 0 return $fields;
1514
1515 }
1516  
1517 }
1518  
1519  
1520 sub get_params {
1521  
1522 232 ▶ my ($self, @params) = @_;
Subroutine get_params: called CC=5 SCAR=21.7
Pod documented
1523
1524 232 ▶ my $params = $self->params->hash || {};
Condition50%1/2
$self->params->hash || {}
l!l
2320
MC/DC0%0/1
$self->params->hash || {}
$self->params->hash
Truth table50%1/2
A
A= $self->params->hash
Aresult
11
00
1525
1526 232 $params = $self->unflatten_params($params);
1527
1528 232 ▶ if (@params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@params) { } 0 232 232
1529
1530 0 ▶ return @params ? (map { $params->{$_} || undef } @params) : ();
Branch: 2 branches
BranchTrueFalseTotal
@params ? : 0 0 0
$params->{$_} or undef 0 0 0
1531
1532 }
1533
1534 else {
1535
1536 232 return $params;
1537
1538 }
1539
1540 }
1541  
1542  
1543 sub get_value {
1544
1545 215 ▶ my ($self, $name) = @_;
Subroutine get_value: called CC=7 SCAR=19.5
Pod documented
1546
1547 215 ▶ return undef unless $self->fields->has($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->fields->has($name) 0 215 215
1548
1549 215 my $field = $self->fields->{$name};
1550
1551 215 my $value = undef;
1552
1553 215 ▶ if (exists $self->params->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->params->{$name}) { } 188 27 215
1554
1555 188 $value = $self->params->{$name};
1556
1557 }
1558
1559 else {
1560
1561 27 my $params = $self->get_params; # unflattened params
1562
1563 27 ▶ if (exists $params->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params->{$name}) 6 21 27
1564
1565 6 $value = $params->{$name};
1566
1567 }
1568
1569 }
1570
1571 215 ▶ unless (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 21 194 215
1572
1573 21 ▶ if (exists $field->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $field->{"default"}) 1 20 21
1574
1575 $value = "CODE" eq ref $field->{default} ?
1576 $field->{default}->($self) :
1577 1 ▶ $field->{default};
Branch: 1 branch
BranchTrueFalseTotal
'CODE' eq ref $field->{'default'} ? : 0 1 1
1578
1579 }
1580
1581 }
1582
1583 215 return $value;
1584
1585 }
1586  
1587 sub merge_field {
1588  
1589 30 ▶ my ($self, $field, $mixin_field) = @_;
Subroutine merge_field: called CC=11 SCAR=24.0
Pod undocumented
1590
1591 30 ▶ while (my($key,$value) = each(%{$mixin_field})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$mixin_field;}) 284 30 314
1592
1593 # skip unless the directive is mixin compatible
1594
1595 284 ▶ next unless $self->types->{mixin}->{$key}->{mixin};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->types->{"mixin"}{$key}{"mixin"} 150 134 284
1596
1597 # do not override existing keys but multi values append
1598
1599 134 ▶ if (grep { $key eq $_ } keys %{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$key eq $_;} keys %{$field;}) 90 44 134
1600
1601 90 ▶ next unless $self->types->{field}->{$key}->{multi};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->types->{"field"}{$key}{"multi"} 48 42 90
1602
1603 }
1604
1605 86 ▶ if (defined $self->types->{field}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->types->{"field"}{$key}) 86 0 86
1606
1607 # can the directive have multiple values, merge array
1608
1609 86 ▶ if ($self->types->{field}->{$key}->{multi}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->types->{'field'}{$key}{'multi'}) { } 60 26 86
1610
1611 # if field has existing array value, merge unique
1612
1613 60 ▶ if ("ARRAY" eq ref $field->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ('ARRAY' eq ref $field->{$key}) { } 36 24 60
1614
1615 36 ▶ my @values = "ARRAY" eq ref $value ? @{$value} : ($value);
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $value ? : 21 15 36
1616
1617 36 push @values, @{$field->{$key}};
1618
1619 36 @values = do {
1620
1621 36 my %uniq = ();
1622
1623 36 $uniq{$_} = $_ for @values;
1624
1625 36 values %uniq
1626
1627
1628 };
1629
1630 36 $field->{$key} = [@values];
1631
1632 }
1633
1634 # simple copy
1635
1636 else {
1637
1638 24 ▶ $field->{$key} = "ARRAY" eq ref $value ? $value : [$value];
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $value ? : 9 15 24
1639
1640 }
1641
1642 }
1643
1644 # simple copy
1645
1646 else {
1647
1648 26 $field->{$key} = $value;
1649
1650 }
1651
1652 }
1653
1654 }
1655
1656 30 return $field;
1657  
1658 }
1659  
1660 sub merge_mixin {
1661  
1662 148 ▶ my ($self, $field, $mixin) = @_;
Subroutine merge_mixin: called CC=12 SCAR=24.9
Pod undocumented
1663
1664 148 ▶ while (my($key,$value) = each(%{$mixin})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$mixin;}) 442 148 590
1665
1666 # do not override existing keys but multi values append
1667
1668 442 ▶ if (grep { $key eq $_ } keys %{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$key eq $_;} keys %{$field;}) 331 111 442
1669
1670 331 ▶ next unless $self->types->{field}->{$key}->{multi};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->types->{"field"}{$key}{"multi"} 259 72 331
1671
1672 }
1673
1674 183 ▶ if (defined $self->types->{field}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->types->{"field"}{$key}) 183 0 183
1675
1676 # can the directive have multiple values, merge array
1677
1678 183 ▶ if ($self->types->{field}->{$key}->{multi}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->types->{'field'}{$key}{'multi'}) { } 72 111 183
1679
1680 # if field has existing array value, merge unique
1681
1682 72 ▶ if ("ARRAY" eq ref $field->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ('ARRAY' eq ref $field->{$key}) { } 71 1 72
1683
1684 71 ▶ my @values = "ARRAY" eq ref $value ? @{$value} : ($value);
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $value ? : 62 9 71
1685
1686 71 push @values, @{$field->{$key}};
1687
1688 71 @values = do {
1689
1690 71 my %uniq = ();
1691
1692 71 $uniq{$_} = $_ for @values;
1693
1694 71 values %uniq
1695
1696 };
1697
1698 71 $field->{$key} = [@values];
1699
1700 }
1701
1702 # merge copy
1703 else {
1704
1705 1 ▶ my @values = "ARRAY" eq ref $value ? @{$value} : ($value);
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $value ? : 1 0 1
1706
1707 1 ▶ push @values, $field->{$key} if $field->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $field->{$key} 1 0 1
1708
1709 1 @values = do {
1710
1711 1 my %uniq = ();
1712
1713 1 $uniq{$_} = $_ for @values;
1714
1715 1 values %uniq
1716
1717 };
1718
1719 1 $field->{$key} = [@values];
1720
1721 }
1722
1723 }
1724
1725 # simple copy
1726 else {
1727
1728 111 $field->{$key} = $value;
1729
1730 }
1731
1732 }
1733
1734 }
1735
1736 148 return $field;
1737  
1738 }
1739  
1740  
1741 sub normalize {
1742
1743 234 ▶ my $self = shift;
Subroutine normalize: called CC=2 SCAR=7.0
Pod documented
1744
1745 # reset fields
1746 # NOTICE: (called twice in this routine, not sure why, must
1747 # investigate further although it doesn't currently break anything)
1748
1749 234 $self->reset_fields;
1750  
1751 # validate mixin directives
1752
1753 234 ▶ while (my($name, $mixin) = each(%{ $self->mixins })) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->mixins;}) 74 234 308
1754
1755 74 $self->check_mixin($name, $mixin);
1756
1757 }
1758  
1759 # validate field directives and create default directives if needed
1760
1761 my $validate_field = sub {
1762
1763 464 ▶ my($name, $field) = @_;
Subroutine __ANON__: called CC=8 SCAR=21.1
1764
1765 464 $self->check_field($name, $field);
1766
1767 # by default fields should have a filters directive
1768
1769 464 ▶ if (!defined $field->{filters}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field->{"filters"}) 125 339 464
1770
1771 125 $field->{filters} = [];
1772
1773 }
1774
1775 # by default fields should have a filtering directive
1776
1777 464 ▶ if (!defined $field->{filtering}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field->{"filtering"}) 154 310 464
1778
1779 154 ▶ $field->{filtering} = $self->filtering if $self->filtering;
Branch: 1 branch
BranchTrueFalseTotal
if $self->filtering 152 2 154
1780
1781 }
1782
1783 # static labels and error messages may contain multiline
1784 # strings for the sake of aesthetics, flatten them here
1785
1786 464 foreach my $string ('error', 'label') {
1787
1788 928 ▶ if (defined $field->{$string}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->{$string}) 451 477 928
1789
1790 451 $field->{$string} =~ s/^[\n\s\t\r]+//g;
1791 451 $field->{$string} =~ s/[\n\s\t\r]+$//g;
1792 451 $field->{$string} =~ s/[\n\s\t\r]+/ /g;
1793
1794 }
1795
1796 }
1797
1798 # respect readonly fields
1799
1800 464 ▶ if (defined $field->{readonly}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->{"readonly"}) 0 464 464
1801
1802 0 ▶ delete $self->params->{$name} if exists $self->params->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->params->{$name} 0 0 0
1803
1804 }
1805
1806 234 };
1807
1808 234 $self->fields->each($validate_field);
1809  
1810 # check for and process a mixin directive
1811
1812 my $process_mixins = sub {
1813
1814 464 ▶ my($name, $field) = @_;
Subroutine __ANON__: called CC=2 SCAR=6.9
1815
1816 464 ▶ $self->apply_mixin($name, $field->{mixin}) if $field->{mixin};
Branch: 1 branch
BranchTrueFalseTotal
if $field->{"mixin"} 164 300 464
1817
1818 234 };
1819
1820 234 $self->fields->each($process_mixins);
1821  
1822 # check for and process a mixin_field directive
1823
1824 my $process_mixin_fields = sub {
1825
1826 464 ▶ my($name, $field) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.1
1827
1828 464 ▶ if ($field->{mixin_field}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field->{"mixin_field"}) 21 443 464
1829
1830 $self->apply_mixin_field($field->{mixin_field}, $name)
1831 21 ▶ if $self->fields->{$field->{mixin_field}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->fields->{$field->{"mixin_field"}} 21 0 21
1832
1833 }
1834
1835 234 };
1836
1837 234 $self->fields->each($process_mixin_fields);
1838
1839 # alias checking, ... for duplicate aliases, etc
1840
1841 234 my $fieldtree = {};
1842 234 my $aliastree = {};
1843
1844 my $find_duplicates = sub {
1845
1846 464 ▶ my($name, $field) = @_;
Subroutine __ANON__: called CC=6 SCAR=20.2
1847
1848 464 $fieldtree->{$name} = $name; # just a counter
1849
1850 464 ▶ if (defined $field->{alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->{"alias"}) 2 462 464
1851
1852 my $aliases = "ARRAY" eq ref $field->{alias}
1853 2 ▶ ? $field->{alias} : [$field->{alias}];
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $field->{'alias'} ? : 2 0 2
1854
1855 2 foreach my $alias (@{$aliases}) {
1856
1857 2 ▶ if ($aliastree->{$alias}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($aliastree->{$alias}) { } 0 2 2
elsif ($fieldtree->{$alias}) { } 0 2 2
1858
1859 0 my $error = qq(
1860 The field $field contains the alias $alias which is
1861 also defined in the field $aliastree->{$alias}
1862 );
1863
1864 0 $self->throw_error($error);
1865
1866 }
1867 elsif ($fieldtree->{$alias}) {
1868
1869 0 my $error = qq(
1870 The field $field contains the alias $alias which is
1871 the name of an existing field
1872 );
1873
1874 0 $self->throw_error($error);
1875
1876 }
1877 else {
1878
1879 2 $aliastree->{$alias} = $field;
1880
1881 }
1882
1883 }
1884
1885 }
1886
1887 234 };
1888
1889 234 $self->fields->each($find_duplicates);
1890
1891 # restore order to the land
1892
1893 234 $self->reset_fields;
1894
1895 234 return $self;
1896  
1897 }
1898  
1899  
1900 sub param {
1901
1902 18 ▶ my ($self, $name, $value) = @_;
Subroutine param: called CC=5 SCAR=16.4
Pod documented
1903
1904 18 ▶ if ($name && !ref($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name and not ref $name) 18 0 18
Condition33%1/3
$name and not ref $name
!ll&&!rl&&r
0018
MC/DC0%0/2
$name and not ref $name
$name not ref $name
Truth table33%1/3
A and not(B)
A= $nameB= not ref $name
ABresult
0X0
100
111
1905
1906 18 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 8 10 18
1907
1908 8 $self->set_params($name => $value);
1909
1910 8 return $value;
1911
1912 }
1913
1914 10 ▶ if ($self->params->has($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->params->has($name)) 8 2 10
1915
1916 8 return $self->params->{$name};
1917
1918 }
1919
1920 }
1921
1922 2 return undef;
1923  
1924 }
1925  
1926 sub pitch_error {
1927  
1928 2 ▶ my ($self, $error_message) = @_;
Subroutine pitch_error: called CC=3 SCAR=12.3
Pod undocumented
1929
1930 2 $error_message =~ s/\n/ /g;
1931 2 $error_message =~ s/\s+/ /g;
1932
1933 2 ▶ if ($self->ignore_unknown) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ignore_unknown) { } 2 0 2
1934
1935 2 ▶ if ($self->report_unknown) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->report_unknown) 0 2 2
1936
1937 0 $self->errors->add($error_message);
1938
1939 }
1940
1941 }
1942
1943 else {
1944
1945 0 $self->throw_error($error_message);
1946
1947 }
1948
1949 }
1950  
1951 sub proxy_methods {
1952
1953 86 ▶ return qw{
Subroutine proxy_methods: called CC=1 SCAR=0.0
Pod undocumented
1954
1955 class
1956 clear_queue
1957 copy_errors
1958 error
1959 error_count
1960 error_fields
1961 errors
1962 errors_to_string
1963 get_errors
1964 fields
1965 filtering
1966 hash_inflator
1967 ignore_failure
1968 ignore_unknown
1969 param
1970 params
1971 queue
1972 report_failure
1973 report_unknown
1974 reset_errors
1975 set_errors
1976 stash
1977
1978 }
1979
1980 }
1981  
1982 sub proxy_methods_wrapped {
1983
1984 86 ▶ return qw{
Subroutine proxy_methods_wrapped: called CC=1 SCAR=0.0
Pod undocumented
1985
1986 set_method
1987 validate
1988 validate_profile
1989
1990 }
1991
1992 }
1993  
1994  
1995 sub queue {
1996
1997 4 ▶ my $self = shift;
Subroutine queue: called CC=1 SCAR=0.0
Pod documented
1998
1999 4 push @{$self->queued}, @_;
2000
2001 4 return $self;
2002  
2003 }
2004  
2005  
2006 sub reset {
2007  
2008 1 ▶ my $self = shift;
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
2009
2010 1 $self->queued([]);
2011
2012 1 $self->reset_fields;
2013
2014 1 return $self;
2015  
2016 }
2017  
2018  
2019 sub reset_errors {
2020  
2021 469 ▶ my $self = shift;
Subroutine reset_errors: called CC=2 SCAR=6.9
Pod documented
2022
2023 469 $self->errors->clear;
2024
2025 469 foreach my $field ($self->fields->values) {
2026
2027 932 $field->{errors}->clear;
2028
2029 }
2030
2031 469 return $self;
2032  
2033 }
2034  
2035  
2036 sub reset_fields {
2037  
2038 469 ▶ my $self = shift;
Subroutine reset_fields: called CC=2 SCAR=6.9
Pod documented
2039
2040 469 foreach my $field ( $self->fields->keys ) {
2041
2042 # set default, special directives, etc
2043 932 $self->fields->{$field}->{name} = $field;
2044 932 $self->fields->{$field}->{':toggle'} = undef;
2045
2046 932 delete $self->fields->{$field}->{value};
2047
2048 }
2049
2050 469 $self->reset_errors();
2051
2052 469 return $self;
2053  
2054 }
2055  
2056  
2057 sub reset_params {
2058  
2059 0 ▶ my $self = shift;
Subroutine reset_params: not called CC=2 SCAR=17.9
Pod documented
2060
2061 0 ▶ my $params = @_ % 2 ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 0 0 0
2062
2063 0 $self->params->clear;
2064
2065 0 $self->params->add($params);
2066
2067 0 return $self;
2068  
2069 }
2070  
2071  
2072 sub set_errors {
2073  
2074 3 ▶ my ($self, @errors) = @_;
Subroutine set_errors: called CC=2 SCAR=7.1
Pod documented
2075
2076 3 ▶ $self->errors->add(@errors) if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 3 0 3
2077
2078 3 return $self->errors->count;
2079  
2080 }
2081  
2082  
2083 sub set_fields {
2084  
2085 45 ▶ my $self = shift;
Subroutine set_fields: called CC=2 SCAR=7.0
Pod documented
2086
2087 45 ▶ my $fields = @_ % 2 ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 45 0 45
2088
2089 45 $self->fields->add($fields);
2090
2091 45 return $self;
2092  
2093 }
2094  
2095  
2096 sub set_method {
2097
2098 4 ▶ my ($self, $context, $name, $code) = @_;
Subroutine set_method: called CC=3 SCAR=11.0
Pod documented
2099
2100 confess
2101 "Context object ($self->{package} class instance) required ".
2102 4 ▶ "to perform validation" unless $self->{package} eq ref $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"package"} eq ref $context 0 4 4
2103
2104 4 my $class = ref $context;
2105
2106 4 my $shortname = $name;
2107 4 $shortname =~ s/::/\_/g;
2108 4 $shortname =~ s/[^a-zA-Z0-9\_]/\_/g;
2109 4 $shortname =~ s/([a-z])([A-Z])/$1\_$2/g;
2110 4 $shortname = lc $shortname;
2111
2112 4 ▶ $self->throw_error("Error creating method $shortname, method already exists")
Branch: 1 branch
BranchTrueFalseTotal
if $class->can($shortname) 0 4 4
2113 if $class->can($shortname);
2114
2115 # place code on the calling class
2116
2117 56 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
2118
2119 4 *{"${class}::$shortname"} = $code;
2120
2121 }
2122  
2123  
2124 sub set_params {
2125  
2126 193 ▶ my $self = shift;
Subroutine set_params: called CC=3 SCAR=11.0
Pod documented
2127
2128 193 ▶ my $args = @_ % 2 ? $_[0] : { @_ };
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 73 120 193
2129
2130 193 my $params = $self->get_params; # unflattened params
2131
2132 193 ▶ while (my($k,$v) = each(%{$args})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$args;}) 221 193 414
2133
2134 221 $params->{$k} = $v; # add new/overwrite existing
2135
2136 }
2137
2138 193 $params = $self->flatten_params($params);
2139
2140 193 $self->params->clear;
2141
2142 193 $self->params->add($params);
2143
2144 193 return $self;
2145  
2146 }
2147  
2148  
2149 sub set_value {
2150  
2151 77 ▶ my ($self, $name, $value) = @_;
Subroutine set_value: called CC=5 SCAR=17.6
Pod documented
2152
2153 77 ▶ if (! $self->fields->has($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->fields->has($name)) { } 0 77 77
2154
2155 0 $self->pitch_error("Field $name does not exist");
2156
2157 }
2158
2159 else {
2160
2161 77 my $error = qq{
2162 "Field value for $name must be a scalar value or arrayref"
2163 };
2164
2165 77 ▶ $self->throw_error($error)
Branch: 1 branch
BranchTrueFalseTotal
if ref $value and "ARRAY" ne ref $value 0 77 77
Condition33%1/3
ref $value and "ARRAY" ne ref $value
!ll&&!rl&&r
7700
MC/DC0%0/2
ref $value and "ARRAY" ne ref $value
ref $value "ARRAY" ne ref $value
Truth table33%1/3
A and B
A= ref $valueB= "ARRAY" ne ref $value
ABresult
0X0
100
111
2166 if ref($value) && "ARRAY" ne ref $value;
2167
2168 77 ▶ unless (defined $self->fields->{$name}->{readonly}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->fields->{$name}{"readonly"}) 77 0 77
2169
2170 77 my $params = $self->unflatten_params($self->params->hash);
2171
2172 77 $params->{$name} = $value;
2173
2174 77 $self->set_params($name => $value);
2175
2176 }
2177
2178 }
2179
2180 77 return $self;
2181  
2182 }
2183  
2184  
2185 sub stash {
2186  
2187 51 ▶ my ($self, @requests) = @_;
Subroutine stash: called CC=6 SCAR=17.9
Pod documented
2188
2189 51 ▶ if (@requests) {
Branch: 1 branch
BranchTrueFalseTotal
if (@requests) 24 27 51
2190
2191 24 ▶ if (@requests == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@requests == 1) 15 9 24
2192
2193 15 my $request = $requests[0];
2194
2195 15 ▶ if ("HASH" eq ref $request) {
Branch: 1 branch
BranchTrueFalseTotal
if ('HASH' eq ref $request) { } 13 2 15
2196
2197 13 @requests = %{$request};
2198
2199 }
2200 else {
2201
2202 2 return $self->stashed->{$request};
2203
2204 }
2205
2206 }
2207
2208 22 ▶ if (@requests > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@requests > 1) 12 10 22
2209
2210 12 my %data = @requests;
2211
2212 12 ▶ while (my($key, $value) = each %data) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %data) 14 12 26
2213
2214 14 $self->stashed->{$key} = $value;
2215
2216 }
2217
2218 }
2219
2220 }
2221
2222 49 return $self->stashed;
2223  
2224 }
2225  
2226 sub throw_error {
2227
2228 0 ▶ my $error_message = pop;
Subroutine throw_error: not called CC=1 SCAR=6.9
Pod undocumented
2229
2230 0 $error_message =~ s/\n/ /g;
2231 0 $error_message =~ s/\s+/ /g;
2232
2233 0 confess $error_message ;
2234
2235 }
2236  
2237  
2238 sub unflatten_params {
2239
2240 311 ▶ my ($self, $params) = @_;
Subroutine unflatten_params: called CC=3 SCAR=11.4
Pod documented
2241
2242 311 ▶ return undef unless "HASH" eq ref $params;
Branch: 1 branch
BranchTrueFalseTotal
unless "HASH" eq ref $params 0 311 311
2243
2244 311 my $serializer = Hash::Flatten->new($self->hash_inflator);
2245
2246 311 ▶ return $serializer->unflatten($params) || {};
Condition50%1/2
$serializer->unflatten($params) || {}
l!l
3110
MC/DC0%0/1
$serializer->unflatten($params) || {}
$serializer->unflatten($params)
Truth table50%1/2
A
A= $serializer->unflatten($params)
Aresult
11
00
2247
2248 }
2249  
2250  
2251 sub validate {
2252  
2253 132 ▶ my ( $self, $context, @fields ) = @_;
Subroutine validate: called CC=18 SCAR=29.1
Pod documented
2254
2255 confess
2256 "Context object ($self->{package} class instance) required ".
2257 132 ▶ "to perform validation" unless $self->{package} eq ref $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"package"} eq ref $context 0 132 132
2258
2259 # first-things-first, normalize our environment
2260
2261 132 $self->normalize();
2262
2263 # include fields queued by the queue method
2264
2265 132 ▶ if (@{$self->queued}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->queued;}) 5 127 132
2266
2267 5 push @fields, @{$self->queued};
2268
2269 }
2270
2271 # process field patterns
2272
2273 132 my @new_fields = ();
2274
2275 132 foreach my $field (@fields) {
2276
2277 122 ▶ if ("Regexp" eq ref $field) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Regexp' eq ref $field) { } 2 120 122
2278
2279 8 push @new_fields, grep { $_ =~ $field }
2280 2 sort keys %{$self->fields};
2281
2282 }
2283 else {
2284
2285 120 push @new_fields, $field;
2286
2287 }
2288
2289 }
2290
2291 132 @fields = @new_fields;
2292
2293 # process toggled fields
2294
2295 132 foreach my $field (@fields) {
2296
2297 128 my ($switch) = $field =~ /^([\-\+]{1})./;
2298
2299 128 ▶ if ($switch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($switch) 22 106 128
2300
2301 # set fields toggle directive
2302 22 $field =~ s/^[\-\+]{1}//;
2303 22 $self->fields->{$field}->{':toggle'} = $switch;
2304 }
2305
2306 }
2307
2308 # create alias map manually if requested
2309 # extremely-depreciated but it remains for back-compat and nostalgia !!!
2310
2311 132 my $alias_map ;
2312
2313 132 ▶ if ( "HASH" eq ref $fields[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("HASH" eq ref $fields[0]) 1 131 132
2314
2315 1 $alias_map = $fields[0]; @fields = (); # blank
2316
2317 1 ▶ while (my($name, $alias) = each(%{$alias_map})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$alias_map;}) 1 1 2
2318
2319 1 $self->set_params($alias => delete $self->params->{$name});
2320 1 push @fields, $alias;
2321
2322 }
2323
2324 }
2325
2326 # create a map from aliases if applicable
2327
2328 my $alias_mapping = sub {
2329
2330 279 ▶ my($name, $field) = @_;
Subroutine __ANON__: called CC=5 SCAR=16.2
2331
2332 279 ▶ if (defined $field->{alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->{"alias"}) 2 277 279
2333
2334 my $aliases = "ARRAY" eq ref $field->{alias} ?
2335 2 ▶ $field->{alias} : [$field->{alias}];
Branch: 1 branch
BranchTrueFalseTotal
'ARRAY' eq ref $field->{'alias'} ? : 2 0 2
2336
2337 2 foreach my $alias (@{$aliases}) {
2338
2339 2 ▶ if (defined $self->params->{$alias}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->params->{$alias}) 2 0 2
2340
2341 2 $self->set_params($name => delete $self->params->{$alias});
2342 2 push @fields, $name;
2343
2344 }
2345
2346 }
2347
2348 }
2349
2350 132 };
2351
2352 132 $self->fields->each($alias_mapping);
2353
2354 132 my @clones = ();
2355
2356 # begin validation !!!
2357
2358 132 ▶ if ($self->params->count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->params->count) { } 121 11 132
2359
2360 # check if any parameters are arrayrefs
2361 # and handle them appropriately (clone, validate, then reap)
2362
2363 121 my ($ad, $hd) = @{$self->hash_inflator}{
2364 # ^^ pun here
2365 121 'ArrayDelimiter', 'HashDelimiter'
2366 };
2367
2368 121 my %seen = ();
2369
2370 my $create_clones_routine = sub {
2371
2372 199 ▶ my($key, $value) = @_;
Subroutine __ANON__: called CC=11 SCAR=24.8
2373
2374 199 ▶ my $name = $1 if $key =~ /(.*)$ad\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $key =~ /(.*)$ad\d+$/ 6 193 199
2375
2376 199 ▶ if (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 6 193 199
2377
2378 6 ▶ unless ($seen{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seen{$name}) 3 3 6
2379
2380 3 my $field = $self->fields->{$name};
2381
2382 3 ▶ if ($field) {
Branch: 1 branch
BranchTrueFalseTotal
if ($field) 3 0 3
2383
2384 3 $seen{$name}++;
2385
2386 3 my $varcount = scalar grep { /$name$ad\d+$/ }
2387 $self->params->keys;
2388
2389 3 ▶ for (my $i = 0; $i < $varcount; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $varcount) 6 3 9
2390
2391 6 ▶ unless (defined $self->fields->{"$name:$i"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->fields->{"${name}:$i"}) 6 0 6
2392
2393 6 ▶ my $label = ($field->{label} || $field->{name});
Condition33%1/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
060
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table33%1/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
2394
2395 6 $self->clone($name, "$name:$i", {
2396 label => $label . " #" . ($i+1)
2397 });
2398
2399 6 push @clones, "$name:$i"; # to be reaped later
2400 push @fields, "$name:$i" # black hackery
2401 6 ▶ if @fields && grep { $_ eq $name } @fields;
Branch: 1 branch
BranchTrueFalseTotal
if @fields and grep {$_ eq $name;} @fields 6 0 6
Condition33%1/3
@fields and grep {$_ eq $name;} @fields
!ll&&!rl&&r
006
MC/DC0%0/2
@fields and grep {$_ eq $name;} @fields
@fields grep {$_ eq $name;} @fields
Truth table33%1/3
A and B
A= @fieldsB= grep {$_ eq $name;} @fields
ABresult
0X0
100
111
2402
2403 }
2404
2405 }
2406
2407 # like it never existed ...
2408
2409 3 ▶ @fields = grep { $_ ne $name } @fields if @fields; # ...
Branch: 1 branch
BranchTrueFalseTotal
if @fields 3 0 3
2410
2411 }
2412
2413 }
2414
2415 }
2416
2417 121 };
2418
2419 121 $self->params->each($create_clones_routine);
2420
2421 # run pre-filtering if filtering is enabled
2422
2423 121 ▶ $self->apply_filters('pre') if $self->filtering;
Branch: 1 branch
BranchTrueFalseTotal
if $self->filtering 120 1 121
2424
2425 121 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) { } 85 36 121
2426
2427 # validate all parameters against only the fields explicitly
2428 # requested to be validated
2429
2430 85 $self->validate_params_specified($context, @fields);
2431
2432 }
2433
2434 else {
2435
2436 # validate all parameters against all defined fields because no fields
2437 # were explicitly requested to be validated - i.e. not explicitly
2438 # defining fields to be validated effectively allows the parameters
2439 # submitted to dictate what gets validated (may not be ideal)
2440
2441 36 $self->validate_params_discovered($context);
2442
2443 }
2444
2445 }
2446
2447 else {
2448
2449 11 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) { } 11 0 11
2450
2451 # validate fields specified although no parameters were submitted
2452 # will likely pass validation unless fields exist with
2453 # a *required* directive or other validation logic
2454 # expecting a value
2455
2456 11 $self->validate_fields_specified($context, @fields);
2457
2458 }
2459
2460 else {
2461
2462 # validate all defined fields although no parameters were submitted
2463 # will likely pass validation unless fields exist with
2464 # a *required* directive or other validation logic
2465 # expecting a value
2466
2467 0 $self->validate_fields_discovered($context);
2468
2469 }
2470
2471 }
2472
2473 132 ▶ my $valid = $self->error_count ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->error_count ? : 58 74 132
2474
2475 # restore parameters from depreciated alias map functionality
2476
2477 132 ▶ if ( defined $alias_map ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alias_map) 1 131 132
2478
2479 # reversal
2480 1 ▶ while (my($name, $alias) = each(%{$alias_map})) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$alias_map;}) 1 1 2
2481
2482 1 $self->set_params($name => delete $self->params->{$alias});
2483
2484 }
2485
2486 }
2487
2488 # reap cloned fields
2489 # we are currently NOT reaping clones, because we need them to stick around
2490 # so we can reference any errors they may have generated ...
2491 # $self->fields->remove($_) for @clones;
2492
2493 # run post-validation filtering
2494
2495 132 ▶ $self->apply_filters('post') if $self->filtering && $valid;
Branch: 1 branch
BranchTrueFalseTotal
if $self->filtering and $valid 73 59 132
Condition100%3/3
$self->filtering and $valid
!ll&&!rl&&r
15873
MC/DC100%2/2
$self->filtering and $valid
$self->filtering $valid
Truth table100%3/3
A and B
A= $self->filteringB= $valid
ABresult
0X0
100
111
2496  
2497 132 return $valid; # returns true if no errors
2498  
2499 }
2500  
2501 sub validate_fields_discovered {
2502
2503 0 ▶ my ($self, $context) = @_;
Subroutine validate_fields_discovered: not called CC=4 SCAR=30.0
Pod undocumented
2504
2505 0 my @fields = sort $self->fields->keys;
2506
2507 0 ▶ if (@fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (@fields) { } 0 0 0
2508
2509 0 $self->validate_fields_specified($context, @fields);
2510
2511 }
2512
2513 else {
2514
2515 # if no parameters (or) fields are found ... you're screwed :)
2516 # instead of dying, warn and continue, depending on configuration
2517
2518 0 my $error = qq{
2519 No parameters were submitted and no fields are
2520 registered. Fields and parameters are required
2521 for validation
2522 };
2523
2524 0 ▶ if ($self->ignore_unknown) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ignore_unknown) { } 0 0 0
2525
2526 0 ▶ if ($self->report_unknown) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->report_unknown) 0 0 0
2527
2528 0 $self->errors->add($error);
2529
2530 }
2531
2532 }
2533
2534 else {
2535
2536 0 $self->throw_error($error);
2537
2538 }
2539
2540 }
2541  
2542 }
2543  
2544 sub validate_fields_specified {
2545
2546 11 ▶ my ($self, $context, @fields) = @_;
Subroutine validate_fields_specified: called CC=9 SCAR=33.4
Pod undocumented
2547
2548 11 foreach my $field_name (@fields) {
2549
2550 13 ▶ if ( !defined $self->fields->{$field_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->fields->{$field_name}) 0 13 13
2551
2552 0 $self->pitch_error(
2553 "Data validation field $field_name does not exist"
2554 );
2555 0 next;
2556
2557 }
2558
2559 13 my $field = $self->fields->{$field_name};
2560
2561 13 $field->{name} = $field_name;
2562 13 $field->{value} = $self->get_value($field_name);
2563
2564 13 my @args = ($context, $field, $self->params);
2565  
2566 # execute simple validation
2567  
2568 13 $self->apply_validator($field_name, $field);
2569  
2570 # custom validation
2571  
2572 13 ▶ if (defined $field->{validation} && $field->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->{"validation"} and $field->{"value"}) 0 13 13
Condition66%2/3
defined $field->{"validation"} and $field->{"value"}
!ll&&!rl&&r
940
MC/DC0%0/2
defined $field->{"validation"} and $field->{"value"}
defined $field->{"validation"} $field->{"value"}
Truth table66%2/3
A and B
A= defined $field->{"validation"}B= $field->{"value"}
ABresult
0X0
100
111
2573
2574 0 my $count = $self->error_count;
2575
2576 0 ▶ unless ($field->{validation}->(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field->{"validation"}->(@args)) 0 0 0
2577
2578 # assuming the validation routine didnt issue an error
2579 0 ▶ if ($count == $self->error_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == $self->error_count) 0 0 0
2580
2581 0 ▶ if (defined $field->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->{'error'}) { } 0 0 0
2582  
2583 0 $field->{errors}->add($field->{error});
2584  
2585 }
2586  
2587 else {
2588
2589 my $error_msg = join " ",
2590
2591 0 ▶ ($field->{label} || $field->{name}),
Condition0%0/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table0%0/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
2592 "could not be validated"
2593 ;
2594
2595 0 $field->{errors}->add($error_msg);
2596
2597 }
2598
2599 }
2600
2601 }
2602
2603 }
2604
2605 }
2606  
2607 }
2608  
2609 sub validate_params_discovered {
2610
2611 36 ▶ my ($self, $context) = @_;
Subroutine validate_params_discovered: called CC=1 SCAR=2.0
Pod undocumented
2612
2613 # process all params
2614  
2615 my $validate_param = sub {
2616
2617 51 ▶ my($name, $param) = @_;
Subroutine __ANON__: called CC=8 SCAR=32.6
2618
2619 51 ▶ if ( ! $self->fields->has($name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->fields->has($name)) { } 0 51 51
2620
2621 0 $self->pitch_error(
2622 "Data validation field $name does not exist"
2623 );
2624
2625 }
2626
2627 else {
2628
2629 51 my $field = $self->fields->{$name};
2630
2631 51 $field->{name} = $name;
2632 51 $field->{value} = $self->get_value($name);
2633
2634 # create arguments to be passed to the validation directive
2635
2636 51 my @args = ($context, $field, $self->params);
2637
2638 # execute validator directives
2639
2640 51 $self->apply_validator($name, $field);
2641
2642 # execute custom/validation directive
2643
2644 51 ▶ if (defined $field->{validation} && $field->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->{"validation"} and $field->{"value"}) 0 51 51
Condition33%1/3
defined $field->{"validation"} and $field->{"value"}
!ll&&!rl&&r
5100
MC/DC0%0/2
defined $field->{"validation"} and $field->{"value"}
defined $field->{"validation"} $field->{"value"}
Truth table33%1/3
A and B
A= defined $field->{"validation"}B= $field->{"value"}
ABresult
0X0
100
111
2645
2646 0 my $count = $self->error_count;
2647
2648 0 ▶ unless ($field->{validation}->(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field->{"validation"}->(@args)) 0 0 0
2649
2650 # assuming the validation routine didnt issue an error
2651
2652 0 ▶ if ($count == $self->error_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == $self->error_count) 0 0 0
2653
2654 0 ▶ if (defined $field->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->{'error'}) { } 0 0 0
2655
2656 0 $field->{errors}->add($field->{error});
2657
2658 }
2659
2660 else {
2661
2662 my $error_msg = join " ",
2663
2664 0 ▶ ($field->{label} || $field->{name}),
Condition0%0/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table0%0/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
2665 "could not be validated"
2666 ;
2667
2668 0 $field->{errors}->add($error_msg);
2669
2670 }
2671
2672 }
2673
2674 }
2675
2676 }
2677
2678 }
2679
2680 36 };
2681
2682 36 $self->params->each($validate_param);
2683
2684 }
2685  
2686 sub validate_params_specified {
2687
2688 85 ▶ my ($self, $context, @fields) = @_;
Subroutine validate_params_specified: called CC=9 SCAR=22.1
Pod undocumented
2689
2690 85 foreach my $field_name (@fields) {
2691
2692 120 ▶ if (!defined $self->fields->{$field_name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->fields->{$field_name}) 2 118 120
2693
2694 2 $self->pitch_error(
2695 "Data validation field $field_name does not exist"
2696 );
2697 2 next;
2698
2699 }
2700
2701 118 my $field = $self->fields->{$field_name};
2702
2703 118 $field->{name} = $field_name;
2704 118 $field->{value} = $self->get_value($field_name);
2705
2706 118 my @args = ($context, $field, $self->params);
2707  
2708 # execute simple validation
2709  
2710 118 $self->apply_validator($field_name, $field);
2711  
2712 # custom validation
2713  
2714 118 ▶ if (defined $field->{validation} && $field->{value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->{"validation"} and $field->{"value"}) 12 106 118
Condition66%2/3
defined $field->{"validation"} and $field->{"value"}
!ll&&!rl&&r
106012
MC/DC50%1/2
defined $field->{"validation"} and $field->{"value"}
defined $field->{"validation"} $field->{"value"}
Truth table66%2/3
A and B
A= defined $field->{"validation"}B= $field->{"value"}
ABresult
0X0
100
111
2715
2716 12 my $count = $self->error_count;
2717
2718 12 ▶ unless ($field->{validation}->(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field->{"validation"}->(@args)) 2 10 12
2719
2720 # assuming the validation routine didnt issue an error
2721  
2722 2 ▶ if ($count == $self->error_count) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == $self->error_count) 2 0 2
2723
2724 2 ▶ if ( defined $field->{error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field->{'error'}) { } 1 1 2
2725  
2726 1 $field->{errors}->add($field->{error});
2727  
2728 }
2729  
2730 else {
2731
2732 my $error_msg = join " ",
2733
2734 1 ▶ ($field->{label} || $field->{name}),
Condition33%1/3
$field->{'label'} || $field->{'name'}
l!l&&r!l&&!r
010
MC/DC0%0/2
$field->{'label'} || $field->{'name'}
$field->{'label'} $field->{'name'}
Truth table33%1/3
A || B
A= $field->{'label'}B= $field->{'name'}
ABresult
1X1
011
000
2735 "could not be validated"
2736 ;
2737
2738 1 $field->{errors}->add($error_msg);
2739
2740 }
2741
2742 }
2743
2744 }
2745
2746 }
2747
2748 }
2749
2750 }
2751  
2752  
2753 sub validate_profile {
2754  
2755 9 ▶ my ($self, $context, $name, @args) = @_;
Subroutine validate_profile: called CC=5 SCAR=17.5
Pod documented
2756
2757 confess
2758 "Context object ($self->{package} class instance) required ".
2759 9 ▶ "to perform validation" unless $self->{package} eq ref $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"package"} eq ref $context 0 9 9
2760
2761 9 ▶ return 0 unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 9 9
2762
2763 9 $self->normalize();
2764 9 ▶ $self->apply_filters('pre') if $self->filtering;
Branch: 1 branch
BranchTrueFalseTotal
if $self->filtering 9 0 9
2765
2766 9 ▶ if ("CODE" eq ref $self->profiles->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ("CODE" eq ref $self->profiles->{$name}) 9 0 9
2767
2768 9 return $self->profiles->{$name}->($context, @args)
2769
2770 }
2771
2772 0 return 0;
2773  
2774 }
2775  
2776 56 1;
2777 __END__
« Class.pm ↑ Summary Class/Base.pm »