Brannigan/Tree.pm

filter: Statementstmt 94.1% 161 / 171 Branchbran 92.9% 119 / 128 Conditioncond 87.1% 68 / 78 MC/DCmcdc 68.1% 30 / 44 Subroutinesub 100% 12 / 12 Podpod 100% 4 / 4 totaltotal 90.1% 394 / 437 CC 112 SCAR 32.9
CC
112
Cov
92%
parse
49.9
_validate_scalar
20.1
validate
14.0
CRAP 26.8
↑ Summary Brannigan/Validations.pm »
1 package Brannigan::Tree;
2  
3 3 our $VERSION = "1.100001";
4 3 $VERSION = eval $VERSION;
5  
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Brannigan::Validations;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Brannigan::Tree - A Brannigan validation/parsing scheme tree, possibly built from a series of inherited schemes.
13  
14 =head1 DESCRIPTION
15  
16 This module is used internally by L<Brannigan>. Basically, a tree is a
17 validation/parsing scheme in its "final", workable structure, taking
18 any inherited schemes into account. The actual validation and parsing
19 of input is done by this module.
20  
21 =head1 CONSTRUCTOR
22  
23 =head2 new( $scheme | @schemes )
24  
25 Creates a new Brannigan::Tree instance from one or more schemes.
26  
27 =cut
28  
29 sub new {
30 14 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
31  
32 14 return bless $class->_merge_trees(@_), $class;
33 }
34  
35 =head1 OBJECT METHODS
36  
37 =head2 process( \%params )
38  
39 Validates and parses the hash-ref of input parameters. Returns a hash-ref
40 of the parsed input, possibly containing a '_rejects' hash-ref with a list
41 of failed validations for each failed parameter.
42  
43 =cut
44  
45 sub process {
46 9 ▶ my ($self, $params) = @_;
Subroutine process: called CC=2 SCAR=6.9
Pod documented
47  
48 9 my $rejects = $self->validate($params, $self->{params});
49 9 my $data = $self->parse($params, $self->{params}, $self->{groups});
50  
51 9 ▶ $data->{_rejects} = $rejects
Branch: 1 branch
BranchTrueFalseTotal
if $rejects 5 4 9
52 if $rejects;
53  
54 9 return $data;
55 }
56  
57 =head2 validate( \%params )
58  
59 Validates the hash-ref of input parameters and returns a hash-ref of rejects
60 (i.e. failed validation methods) for each parameter.
61  
62 =cut
63  
64 sub validate {
65 38 ▶ my ($self, $params, $rules) = @_;
Subroutine validate: called CC=14 SCAR=26.4
Pod documented
66  
67 38 my $rejects;
68  
69 # go over all the parameters and validate them
70 38 foreach (sort keys %$params) {
71 # find references to this parameter, first in regexes, then direct
72 # give preference to the direct references
73 136 my @references;
74 136 ▶ push(@references, $rules->{_all}) if $rules->{_all};
Branch: 1 branch
BranchTrueFalseTotal
if $rules->{"_all"} 24 112 136
75 136 foreach my $param (sort keys %$rules) {
76 854 ▶ next unless $param =~ m!^/([^/]+)/$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $param =~ m[^/([^/]+)/$] 738 116 854
77 116 my $re = qr/$1/;
78 116 ▶ push(@references, $rules->{$param}) if m/$re/;
Branch: 1 branch
BranchTrueFalseTotal
if /$re/ 52 64 116
79 }
80 136 ▶ push(@references, $rules->{$_}) if $rules->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $rules->{$_} 116 20 136
81  
82 136 my $rj = $self->_validate_param($_, $params->{$_}, $self->_merge_trees(@references));
83  
84 136 ▶ $rejects->{$_} = $rj if $rj;
Branch: 1 branch
BranchTrueFalseTotal
if $rj 68 68 136
85 }
86  
87 # find required parameters that aren't there
88 38 foreach (sort keys %$rules) {
89 167 ▶ next if $_ eq '_all';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "_all" 9 158 167
90 158 ▶ next if m!^/[^/]+/$!;
Branch: 1 branch
BranchTrueFalseTotal
if m[^/[^/]+/$] 22 136 158
91 136 ▶ $rejects->{$_} = ['required(1)'] if $rules->{$_}->{required} && (!defined $params->{$_} || $params->{$_} eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $rules->{$_}{"required"} and !defined($params->{$_}) || $params->{$_} eq "" 7 129 136
Condition100%6/6
$rules->{$_}{"required"} and !defined($params->{$_}) || $params->{$_} eq ""
!ll&&!rl&&r
89443
not defined $params->{$_} or $params->{$_} eq ""
l!l&&r!l&&!r
5240
MC/DC66%2/3
$rules->{$_}{"required"} and !defined($params->{$_}) || $params->{$_} eq ""
$rules->{$_}{"required"} not defined $params->{$_} $params->{$_} eq ""
Truth table75%3/4
A and not(B) or C
A= $rules->{$_}{"required"}B= not defined $params->{$_}C= $params->{$_} eq ""
ABCresult
0XX0
1000
11X1
1011
92 }
93  
94 38 return $rejects;
95 }
96  
97 =head2 parse( \%params, \%param_rules, [\%group_rules] )
98  
99 Receives a hash-ref of parameters, a hash-ref of parameter rules (this is
100 the 'params' part of a scheme) and optionally a hash-ref of group rules
101 (this is the 'groups' part of a scheme), parses the parameters according
102 to these rules and returns a hash-ref of all the parameters after parsing.
103  
104 =cut
105  
106 sub parse {
107 56 ▶ my ($self, $params, $param_rules, $group_rules) = @_;
Subroutine parse: called CC=46 SCAR=39.1
Pod documented
108  
109 56 my $data;
110  
111 # fill-in missing parameters with default values, if defined
112 56 foreach (sort keys %$param_rules) {
113 187 ▶ next if m!^/[^/]+/$!;
Branch: 1 branch
BranchTrueFalseTotal
if m[^/[^/]+/$] 22 165 187
114 165 ▶ next unless !defined $params->{$_} || $params->{$_} eq '';
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $params->{$_} or $params->{$_} eq "" 131 34 165
Condition100%3/3
not defined $params->{$_} or $params->{$_} eq ""
l!l&&r!l&&!r
313131
MC/DC100%2/2
not defined $params->{$_} or $params->{$_} eq ""
not defined $params->{$_} $params->{$_} eq ""
Truth table100%3/3
not(A) or B
A= not defined $params->{$_}B= $params->{$_} eq ""
ABresult
1X1
011
000
115  
116 # is there a default value/method?
117 34 ▶ if (exists $param_rules->{$_}->{default} && ref $param_rules->{$_}->{default} eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $param_rules->{$_}{'default'} and ref $param_rules->{$_}{'default'} eq 'CODE') { } 3 31 34
elsif (exists $param_rules->{$_}{'default'}) { } 3 28 31
Condition100%3/3
exists $param_rules->{$_}{"default"} and ref $param_rules->{$_}{"default"} eq "CODE"
!ll&&!rl&&r
2833
MC/DC100%2/2
exists $param_rules->{$_}{"default"} and ref $param_rules->{$_}{"default"} eq "CODE"
exists $param_rules->{$_}{"default"} ref $param_rules->{$_}{"default"} eq "CODE"
Truth table100%3/3
A and B
A= exists $param_rules->{$_}{"default"}B= ref $param_rules->{$_}{"default"} eq "CODE"
ABresult
0X0
100
111
118 3 $data->{$_} = $param_rules->{$_}->{default}->();
119 } elsif (exists $param_rules->{$_}->{default}) {
120 3 $data->{$_} = $param_rules->{$_}->{default};
121 }
122 }
123  
124 # parse the data
125 56 foreach (sort keys %$params) {
126 # ignore undefined or empty values
127 154 ▶ next if !defined $params->{$_} || $params->{$_} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not defined $params->{$_} or $params->{$_} eq "" 5 149 154
Condition100%3/3
not defined $params->{$_} or $params->{$_} eq ""
l!l&&r!l&&!r
23149
MC/DC100%2/2
not defined $params->{$_} or $params->{$_} eq ""
not defined $params->{$_} $params->{$_} eq ""
Truth table100%3/3
not(A) or B
A= not defined $params->{$_}B= $params->{$_} eq ""
ABresult
1X1
011
000
128
129 # is there a reference to this parameter in the scheme?
130 149 my @refs;
131 149 foreach my $p (sort keys %$param_rules) {
132 841 ▶ next unless $p =~ m!^/([^/]+)/$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $p =~ m[^/([^/]+)/$] 731 110 841
133 110 my $re = qr/$1/;
134 110 ▶ next unless m/$re/;
Branch: 1 branch
BranchTrueFalseTotal
unless /$re/ 61 49 110
135 49 push(@refs, $param_rules->{$p});
136 }
137 149 ▶ push(@refs, $param_rules->{$_}) if $param_rules->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if $param_rules->{$_} 131 18 149
138
139 149 ▶ next if scalar @refs == 0 && $self->{ignore_missing};
Branch: 1 branch
BranchTrueFalseTotal
if scalar @refs == 0 and $self->{"ignore_missing"} 2 147 149
Condition66%2/3
scalar @refs == 0 and $self->{"ignore_missing"}
!ll&&!rl&&r
14702
MC/DC50%1/2
scalar @refs == 0 and $self->{"ignore_missing"}
scalar @refs == 0 $self->{"ignore_missing"}
Truth table66%2/3
A and B
A= scalar @refs == 0B= $self->{"ignore_missing"}
ABresult
0X0
100
111
140 147 ▶ unless (scalar @refs && $self->{ignore_missing}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @refs and $self->{"ignore_missing"}) 2 145 147
Condition66%2/3
scalar @refs and $self->{"ignore_missing"}
!ll&&!rl&&r
02145
MC/DC50%1/2
scalar @refs and $self->{"ignore_missing"}
scalar @refs $self->{"ignore_missing"}
Truth table66%2/3
A and B
A= scalar @refsB= $self->{"ignore_missing"}
ABresult
0X0
100
111
141 # pass the parameter as is
142 2 $data->{$_} = $params->{$_};
143 2 next;
144 }
145  
146 # is this a hash-ref or an array-ref or just a scalar?
147 145 ▶ if (ref $params->{$_} eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $params->{$_} eq 'HASH') { } 29 116 145
elsif (ref $params->{$_} eq 'ARRAY') { } 9 107 116
148 29 my $pd = $self->parse($params->{$_}, $self->_merge_trees(@refs)->{keys});
149 29 foreach my $k (sort keys %$pd) {
150 83 $data->{$_}->{$k} = $pd->{$k};
151 }
152 } elsif (ref $params->{$_} eq 'ARRAY') {
153 9 foreach my $val (@{$params->{$_}}) {
154 # we need to parse this value with the rules
155 # in the 'values' key
156 18 my $pd = $self->parse({ param => $val }, { param => $self->_merge_trees(@refs)->{values} });
157 18 push(@{$data->{$_}}, $pd->{param});
158 }
159 } else {
160 # is there a parsing method?
161 # first see if there's one in a regex
162 107 my $parse;
163 107 my @data = ($params->{$_});
164 107 foreach my $r (sort keys %$param_rules) {
165 553 ▶ next unless $r =~ m!^/([^/]+)/$!;
Branch: 1 branch
BranchTrueFalseTotal
unless $r =~ m[^/([^/]+)/$] 484 69 553
166 69 my $re = qr/$1/;
167
168 69 my @matches = (m/$re/);
169 69 ▶ next unless scalar @matches > 0;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @matches > 0 26 43 69
170 43 push(@data, @matches);
171  
172 43 ▶ $parse = $param_rules->{$r}->{parse} if $param_rules->{$r}->{parse};
Branch: 1 branch
BranchTrueFalseTotal
if $param_rules->{$r}{"parse"} 6 37 43
173 }
174 107 ▶ $parse = $param_rules->{$_}->{parse} if $param_rules->{$_}->{parse};
Branch: 1 branch
BranchTrueFalseTotal
if $param_rules->{$_}{"parse"} 11 96 107
175  
176 # make sure if we have a parse method that is indeed a subroutine
177 107 ▶ if ($parse && ref $parse eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parse and ref $parse eq 'CODE') { } 17 90 107
Condition66%2/3
$parse and ref $parse eq "CODE"
!ll&&!rl&&r
90017
MC/DC50%1/2
$parse and ref $parse eq "CODE"
$parse ref $parse eq "CODE"
Truth table66%2/3
A and B
A= $parseB= ref $parse eq "CODE"
ABresult
0X0
100
111
178 17 my $parsed = $parse->(@data);
179 17 foreach my $k (sort keys %$parsed) {
180 17 ▶ if (ref $parsed->{$k} eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $parsed->{$k} eq 'HASH') { } 0 17 17
elsif (ref $parsed->{$k} eq 'ARRAY') { } 0 17 17
181 0 foreach my $sk (sort keys %{$parsed->{$k}}) {
182 0 $data->{$k}->{$sk} = $parsed->{$k}->{$sk};
183 }
184 } elsif (ref $parsed->{$k} eq 'ARRAY') {
185 0 push(@{$data->{$k}}, @{$parsed->{$k}});
186 } else {
187 17 $data->{$k} = $parsed->{$k};
188 }
189 }
190 } else {
191 # just pass as-is
192 90 $data->{$_} = $params->{$_};
193 }
194 }
195 }
196  
197 # parse group data
198 56 ▶ if ($group_rules) {
Branch: 1 branch
BranchTrueFalseTotal
if ($group_rules) 7 49 56
199 7 foreach (sort keys %$group_rules) {
200 11 my @data;
201
202 # do we have a list of parameters, or a regular expression?
203 11 ▶ if (exists $group_rules->{$_}->{params}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $group_rules->{$_}{'params'}) { } 8 3 11
elsif (exists $group_rules->{$_}{'regex'}) { } 3 0 3
204 8 foreach my $p (@{$group_rules->{$_}->{params}}) {
205 20 push(@data, $data->{$p});
206 }
207 } elsif (exists $group_rules->{$_}->{regex}) {
208 3 my ($re) = ($group_rules->{$_}->{regex} =~ m!^/([^/]+)/$!);
209 3 ▶ next unless $re;
Branch: 1 branch
BranchTrueFalseTotal
unless $re 0 3 3
210 3 $re = qr/$re/;
211 3 foreach my $p (sort keys %$data) {
212 39 ▶ next unless $p =~ m/$re/;
Branch: 1 branch
BranchTrueFalseTotal
unless $p =~ /$re/ 27 12 39
213 12 push(@data, $data->{$p});
214 }
215 } else {
216 # we have nothing in this group
217 0 next;
218 }
219
220 # parse the data
221 11 my $parsed = $group_rules->{$_}->{parse}->(@data);
222 11 foreach my $k (sort keys %$parsed) {
223 8 ▶ if (ref $parsed->{$k} eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $parsed->{$k} eq 'ARRAY') { } 3 5 8
elsif (ref $parsed->{$k} eq 'HASH') { } 0 5 5
224 3 push(@{$data->{$k}}, @{$parsed->{$k}});
225 } elsif (ref $parsed->{$k} eq 'HASH') {
226 0 foreach my $sk (sort keys %{$parsed->{$k}}) {
227 0 $data->{$k}->{$sk} = $parsed->{$k}->{$sk};
228 }
229 } else {
230 5 $data->{$k} = $parsed->{$k};
231 }
232 }
233 }
234 }
235  
236 56 return $data;
237 }
238  
239 #############################
240 ##### INTERNAL METHODS ######
241 #############################
242  
243 # _validate_param( $param, $value, \%validations )
244 # ------------------------------------------------
245 # Receives the name of a parameter, its value, and a hash-ref of validations
246 # to assert against. Returns a list of validations that failed for this
247 # parameter. Depending on the type of the parameter (either scalar, hash
248 # or array), this method will call one of the following three methods.
249  
250 sub _validate_param {
251 154 ▶ my ($self, $param, $value, $validations) = @_;
Subroutine _validate_param: called CC=13 SCAR=25.7
252  
253 # is there any reference to this parameter in the scheme?
254 154 ▶ return unless $validations;
Branch: 1 branch
BranchTrueFalseTotal
unless $validations 2 152 154
255  
256 # is this parameter required? if not, and it has no value
257 # (either undef or an empty string), then don't bother checking
258 # any validations. If yes, and it has no value, do the same.
259 152 ▶ return if !$validations->{required} && (!defined $value || $value eq '');
Branch: 1 branch
BranchTrueFalseTotal
if not $validations->{"required"} and !defined($value) || $value eq "" 2 150 152
Condition100%6/6
not $validations->{"required"} and !defined($value) || $value eq ""
!ll&&!rl&&r
84671
not defined $value or $value eq ""
l!l&&r!l&&!r
1166
MC/DC66%2/3
not $validations->{"required"} and !defined($value) || $value eq ""
not $validations->{"required"} not defined $value $value eq ""
Truth table75%3/4
not(A) and not(B) or C
A= not $validations->{"required"}B= not defined $valueC= $value eq ""
ABCresult
0XX0
1000
11X1
1011
260 150 ▶ return ['required(1)'] if $validations->{required} && (!defined $value || $value eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $validations->{"required"} and !defined($value) || $value eq "" 3 147 150
Condition100%6/6
$validations->{"required"} and !defined($value) || $value eq ""
!ll&&!rl&&r
66822
not defined $value or $value eq ""
l!l&&r!l&&!r
1281
MC/DC66%2/3
$validations->{"required"} and !defined($value) || $value eq ""
$validations->{"required"} not defined $value $value eq ""
Truth table75%3/4
A and not(B) or C
A= $validations->{"required"}B= not defined $valueC= $value eq ""
ABCresult
0XX0
1000
11X1
1011
261  
262 # is this parameter forbidden? if yes, and it has a value,
263 # don't bother checking any other validations.
264 147 ▶ return ['forbidden(1)'] if $validations->{forbidden} && defined $value && $value ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $validations->{"forbidden"} and defined $value and $value ne "" 1 146 147
Condition66%4/6
$validations->{"forbidden"} and defined $value and $value ne ""
!ll&&!rl&&r
14601
$validations->{"forbidden"} and defined $value
!ll&&!rl&&r
14601
MC/DC33%1/3
$validations->{"forbidden"} and defined $value and $value ne ""
$validations->{"forbidden"} defined $value $value ne ""
Truth table50%2/4
A and B and C
A= $validations->{"forbidden"}B= defined $valueC= $value ne ""
ABCresult
0XX0
10X0
1100
1111
265  
266 # is this a scalar, array or hash parameter?
267 146 ▶ if ($validations->{hash}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($validations->{'hash'}) { } 29 117 146
elsif ($validations->{'array'}) { } 11 106 117
268 29 return $self->_validate_hash($param, $value, $validations);
269 } elsif ($validations->{array}) {
270 11 return $self->_validate_array($param, $value, $validations);
271 } else {
272 106 return $self->_validate_scalar($param, $value, $validations);
273 }
274 }
275  
276 # _validate_scalar( $param, $value, \%validations, [$type] )
277 # ----------------------------------------------------------
278 # Receives the name of a parameter, its value, and a hash-ref of validations
279 # to assert against. Returns a list of all failed validations for this
280 # parameter. If the parameter is a child of a hash/array parameter, then
281 # C<$type> must be provided with either 'hash' or 'array'.
282  
283 sub _validate_scalar {
284 144 ▶ my ($self, $param, $value, $validations, $type) = @_;
Subroutine _validate_scalar: called CC=20 SCAR=30.0
285  
286 144 my @rejects;
287  
288 # get all validations we need to perform
289 144 foreach my $v (sort keys %$validations) {
290 # skip the parse method and the default value
291 329 ▶ next if $v eq 'parse' || $v eq 'default';
Branch: 1 branch
BranchTrueFalseTotal
if $v eq "parse" or $v eq "default" 23 306 329
Condition66%2/3
$v eq "parse" or $v eq "default"
l!l&&r!l&&!r
230306
MC/DC50%1/2
$v eq "parse" or $v eq "default"
$v eq "parse" $v eq "default"
Truth table66%2/3
A or B
A= $v eq "parse"B= $v eq "default"
ABresult
1X1
011
000
292 306 ▶ next if $type && $type eq 'array' && $v eq 'values';
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type eq "array" and $v eq "values" 9 297 306
Condition100%6/6
$type and $type eq "array" and $v eq "values"
!ll&&!rl&&r
277209
$type and $type eq "array"
!ll&&!rl&&r
2136429
MC/DC100%3/3
$type and $type eq "array" and $v eq "values"
$type $type eq "array" $v eq "values"
Truth table100%4/4
A and B and C
A= $typeB= $type eq "array"C= $v eq "values"
ABCresult
0XX0
10X0
1100
1111
293 297 ▶ next if $type && $type eq 'hash' && $v eq 'keys';
Branch: 1 branch
BranchTrueFalseTotal
if $type and $type eq "hash" and $v eq "keys" 29 268 297
Condition100%6/6
$type and $type eq "hash" and $v eq "keys"
!ll&&!rl&&r
2333529
$type and $type eq "hash"
!ll&&!rl&&r
2132064
MC/DC100%3/3
$type and $type eq "hash" and $v eq "keys"
$type $type eq "hash" $v eq "keys"
Truth table100%4/4
A and B and C
A= $typeB= $type eq "hash"C= $v eq "keys"
ABCresult
0XX0
10X0
1100
1111
294  
295 # get the data we're passing to the validation method
296 268 ▶ my @data = ref $validations->{$v} eq 'ARRAY' ? @{$validations->{$v}} : ($validations->{$v});
Branch: 1 branch
BranchTrueFalseTotal
ref $validations->{$v} eq 'ARRAY' ? : 69 199 268
297
298 # which validation method are we gonna use?
299 # custom ones have preference
300 268 ▶ if ($v eq 'validate' && ref $validations->{$v} eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if ($v eq 'validate' and ref $validations->{$v} eq 'CODE') { } 18 250 268
elsif (exists $self->{'_custom_validations'} and exists $self->{'_custom_validations'}{$v} and ref $self->{'_custom_validations'}{$v} eq 'CODE') { } 9 241 250
Condition66%2/3
$v eq "validate" and ref $validations->{$v} eq "CODE"
!ll&&!rl&&r
250018
MC/DC50%1/2
$v eq "validate" and ref $validations->{$v} eq "CODE"
$v eq "validate" ref $validations->{$v} eq "CODE"
Truth table66%2/3
A and B
A= $v eq "validate"B= ref $validations->{$v} eq "CODE"
ABresult
0X0
100
111
301 # this is an "inline" validation method, invoke it
302 18 ▶ push(@rejects, $v) unless $validations->{$v}->($value, @data);
Branch: 1 branch
BranchTrueFalseTotal
unless $validations->{$v}->($value, @data) 6 12 18
303 ▶ } elsif (exists $self->{_custom_validations} && exists $self->{_custom_validations}->{$v} && ref $self->{_custom_validations}->{$v} eq 'CODE') {
Condition83%5/6
exists $self->{"_custom_validations"} and exists $self->{"_custom_validations"}{$v} and ref $self->{"_custom_validations"}{$v} eq "CODE"
!ll&&!rl&&r
24109
exists $self->{"_custom_validations"} and exists $self->{"_custom_validations"}{$v}
!ll&&!rl&&r
12409
MC/DC66%2/3
exists $self->{"_custom_validations"} and exists $self->{"_custom_validations"}{$v} and ref $self->{"_custom_validations"}{$v} eq "CODE"
exists $self->{"_custom_validations"} exists $self->{"_custom_validations"}{$v} ref $self->{"_custom_validations"}{$v} eq "CODE"
Truth table75%3/4
A and B and C
A= exists $self->{"_custom_validations"}B= exists $self->{"_custom_validations"}{$v}C= ref $self->{"_custom_validations"}{$v} eq "CODE"
ABCresult
0XX0
10X0
1100
1111
304 # this is a cross-scheme custom validation method
305 9 ▶ push(@rejects, $v.'('.join(', ', @data).')') unless $self->{_custom_validations}->{$v}->($value, @data);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_custom_validations"}{$v}->($value, @data) 3 6 9
306 } else {
307 # we're using a built-in validation method
308 241 ▶ push(@rejects, $v.'('.join(', ', @data).')') unless Brannigan::Validations->$v($value, @data);
Branch: 1 branch
BranchTrueFalseTotal
unless "Brannigan::Validations"->$v($value, @data) 43 198 241
309 }
310 }
311  
312 144 ▶ return scalar @rejects ? [@rejects] : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar @rejects ? : 43 101 144
313 }
314  
315 # _validate_array( $param, $value, \%validations )
316 # ------------------------------------------------
317 # Receives the name of an array parameter, its value, and a hash-ref of validations
318 # to assert against. Returns a list of validations that failed for this
319 # parameter.
320  
321 sub _validate_array {
322 11 ▶ my ($self, $param, $value, $validations) = @_;
Subroutine _validate_array: called CC=6 SCAR=17.9
323  
324 # if this isn't an array, don't bother checking any other validation method
325 11 ▶ return { _self => ['array(1)'] } unless ref $value eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "ARRAY" 2 9 11
326  
327 # invoke validations on the parameter itself
328 9 my $rejects = {};
329 9 my $_self = $self->_validate_scalar($param, $value, $validations, 'array');
330 9 ▶ $rejects->{_self} = $_self if $_self;
Branch: 1 branch
BranchTrueFalseTotal
if $_self 0 9 9
331  
332 # invoke validations on the values of the array
333 9 my $i = 0;
334 9 foreach (@$value) {
335 18 my $rj = $self->_validate_param("${param}[$i]", $_, $validations->{values});
336 18 ▶ $rejects->{$i} = $rj if $rj;
Branch: 1 branch
BranchTrueFalseTotal
if $rj 9 9 18
337 18 $i++;
338 }
339  
340 9 ▶ return scalar keys %$rejects ? $rejects : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %$rejects ? : 7 2 9
341 }
342  
343 # _validate_hash( $param, $value, \%validations )
344 # -----------------------------------------------
345 # Receives the name of a hash parameter, its value, and a hash-ref of validations
346 # to assert against. Returns a list of validations that failed for this
347 # parameter.
348  
349 sub _validate_hash {
350 29 ▶ my ($self, $param, $value, $validations) = @_;
Subroutine _validate_hash: called CC=5 SCAR=16.2
351  
352 # if this isn't a hash, don't bother checking any other validation method
353 29 ▶ return { _self => ['hash(1)'] } unless ref $value eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value eq "HASH" 0 29 29
354  
355 # invoke validations on the parameter itself
356 29 my $rejects = {};
357 29 my $_self = $self->_validate_scalar($param, $value, $validations, 'hash');
358 29 ▶ $rejects->{_self} = $_self if $_self;
Branch: 1 branch
BranchTrueFalseTotal
if $_self 0 29 29
359  
360 # invoke validations on the keys of the hash (a.k.a mini-params)
361 29 my $hr = $self->validate($value, $validations->{keys});
362  
363 29 foreach (sort keys %$hr) {
364 38 $rejects->{$_} = $hr->{$_};
365 }
366  
367 29 ▶ return scalar keys %$rejects ? $rejects : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %$rejects ? : 21 8 29
368 }
369  
370 # _merge_trees( @trees )
371 # ----------------------
372 # Merges two or more hash-refs of validation/parsing trees and returns the
373 # resulting tree. The merge is performed in order, so trees later in the
374 # array (i.e. on the right) "tramp" the trees on the left.
375  
376 sub _merge_trees {
377 219 ▶ my $class = shift;
Subroutine _merge_trees: called CC=13 SCAR=25.7
378  
379 219 ▶ return unless scalar @_ && (ref $_[0] eq 'HASH' || ref $_[0] eq 'Brannigan::Tree');
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ and ref $_[0] eq "HASH" || ref $_[0] eq "Brannigan::Tree" 2 217 219
Condition83%5/6
scalar @_ and ref $_[0] eq "HASH" || ref $_[0] eq "Brannigan::Tree"
!ll&&!rl&&r
277140
ref $_[0] eq "HASH" or ref $_[0] eq "Brannigan::Tree"
l!l&&r!l&&!r
21250
MC/DC33%1/3
scalar @_ and ref $_[0] eq "HASH" || ref $_[0] eq "Brannigan::Tree"
scalar @_ ref $_[0] eq "HASH" ref $_[0] eq "Brannigan::Tree"
Truth table50%2/4
A and B or C
A= scalar @_B= ref $_[0] eq "HASH"C= ref $_[0] eq "Brannigan::Tree"
ABCresult
0XX0
1000
11X1
1011
380  
381 # the leftmost tree is the starting tree
382 217 my $tree = shift;
383 217 my %tree = %$tree;
384  
385 # now for the merging business
386 217 foreach (@_) {
387 88 ▶ next unless ref $_ eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "HASH" 0 88 88
388  
389 88 foreach my $k (sort keys %$_) {
390 81 ▶ if (ref $_->{$k} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_->{$k} eq 'HASH') { } 28 53 81
391 28 ▶ unless (exists $tree{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tree{$k}) { } 22 6 28
392 6 $tree{$k} = $_->{$k};
393 } else {
394 22 $tree{$k} = $class->_merge_trees($tree{$k}, $_->{$k});
395 }
396 } else {
397 53 ▶ if ($k eq 'forbidden' && $_->{$k}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($k eq 'forbidden' and $_->{$k}) { } 2 51 53
elsif ($k eq 'required' and $_->{$k}) { } 6 82 88
Condition66%2/3
$k eq "forbidden" and $_->{$k}
!ll&&!rl&&r
5102
MC/DC50%1/2
$k eq "forbidden" and $_->{$k}
$k eq "forbidden" $_->{$k}
Truth table66%2/3
A and B
A= $k eq "forbidden"B= $_->{$k}
ABresult
0X0
100
111
398 # remove required, if there was such a rule
399 2 delete $tree{'required'};
400 ▶ } elsif ($k eq 'required' && $_->{$k}) {
Condition100%3/3
$k eq "required" and $_->{$k}
!ll&&!rl&&r
3786
MC/DC100%2/2
$k eq "required" and $_->{$k}
$k eq "required" $_->{$k}
Truth table100%3/3
A and B
A= $k eq "required"B= $_->{$k}
ABresult
0X0
100
111
401 # remove forbidden, if there was such a rule
402 6 delete $tree{'forbidden'};
403 }
404 53 $tree{$k} = $_->{$k};
405 }
406 }
407 }
408  
409 217 return \%tree;
410 }
411  
412 =head1 SEE ALSO
413  
414 L<Brannigan>, L<Brannigan::Validations>.
415  
416 =head1 AUTHOR
417  
418 Ido Perlmuter, C<< <ido at ido50 dot net> >>
419  
420 =head1 BUGS
421  
422 Please report any bugs or feature requests to C<bug-brannigan at rt.cpan.org>, or through
423 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Brannigan>. I will be notified, and then you'll
424 automatically be notified of progress on your bug as I make changes.
425  
426 =head1 SUPPORT
427  
428 You can find documentation for this module with the perldoc command.
429  
430 perldoc Brannigan::Tree
431  
432 You can also look for information at:
433  
434 =over 4
435  
436 =item * RT: CPAN's request tracker
437  
438 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Brannigan>
439  
440 =item * AnnoCPAN: Annotated CPAN documentation
441  
442 L<http://annocpan.org/dist/Brannigan>
443  
444 =item * CPAN Ratings
445  
446 L<http://cpanratings.perl.org/d/Brannigan>
447  
448 =item * Search CPAN
449  
450 L<http://search.cpan.org/dist/Brannigan/>
451  
452 =back
453  
454 =head1 LICENSE AND COPYRIGHT
455  
456 Copyright 2017 Ido Perlmuter
457  
458 Licensed under the Apache License, Version 2.0 (the "License");
459 you may not use this file except in compliance with the License.
460 You may obtain a copy of the License at
461  
462 http://www.apache.org/licenses/LICENSE-2.0
463  
464 Unless required by applicable law or agreed to in writing, software
465 distributed under the License is distributed on an "AS IS" BASIS,
466 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
467 See the License for the specific language governing permissions and
468 limitations under the License.
469  
470 =cut
471  
472 3 1;
↑ Summary Brannigan/Validations.pm »