Brannigan/Tree.pm

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