blib/lib/Data/FormValidator/ConstraintsFactory.pm

filter: Statementstmt 21.5% 17 / 79 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 17.2% 5 / 29 Podpod 92.3% 12 / 13 totaltotal 22.2% 34 / 153 CC 19 SCAR 20.1
CC
19
Cov
16%
__ANON__
20.0
__ANON__
12.0
__ANON__
12.0
CRAP 7.5
« blib/lib/Data/FormValidator/Constraints.pm ↑ Summary blib/lib/Data/FormValidator/Constraints/Dates.pm »
1 #
2 # ConstraintsFactory.pm - Module to create constraints for Data::FormValidator.
3 #
4 # This file is part of Data::FormValidator.
5 #
6 # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM>
7 # Maintainer: Mark Stosberg <mark@summersault.com>
8 #
9 # Copyright (C) 2000 iNsu Innovations Inc.
10 #
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms as perl itself.
13 #
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 package Data::FormValidator::ConstraintsFactory;
17 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =pod
20  
21 =head1 NAME
22  
23 Data::FormValidator::ConstraintsFactory - Module to create constraints for HTML::FormValidator.
24  
25 =head1 DESCRIPTION
26  
27 This module contains functions to help generate complex constraints.
28  
29 If you are writing new code, take a look at L<Data::FormValidator::Constraints::MethodsFactory>
30 instead. It's a modern alternative to what's here, offering improved names and syntax.
31  
32 =head1 SYNOPSIS
33  
34 use Data::FormValidator::ConstraintsFactory qw( :set :bool );
35  
36 constraints => {
37 param1 => make_or_constraint(
38 make_num_set_constraint( -1, ( 1 .. 10 ) ),
39 make_set_constraint( 1, ( 20 .. 30 ) ),
40 ),
41 province => make_word_set_constraint( 1, "AB QC ON TN NU" ),
42 bid => make_range_constraint( 1, 1, 10 ),
43 }
44  
45 =cut
46  
47 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
48 1 our $VERSION = 4.88;
49 1 our @EXPORT = ();
50 1 our @EXPORT_OK = (qw/make_length_constraint/);
51  
52 1 our %EXPORT_TAGS =
53 (
54 bool => [ qw( make_not_constraint make_or_constraint
55 make_and_constraint ) ],
56 set => [ qw( make_set_constraint make_num_set_constraint
57 make_word_set_constraint make_cmp_set_constraint ) ],
58 num => [ qw( make_clamp_constraint make_lt_constraint
59 make_le_constraint make_gt_constraint
60 make_ge_constraint ) ],
61 );
62  
63 1 Exporter::export_ok_tags( 'bool' );
64 1 Exporter::export_ok_tags( 'set' );
65 1 Exporter::export_ok_tags( 'num' );
66  
67 }
68  
69 =pod
70  
71 =head1 BOOLEAN CONSTRAINTS
72  
73 Those constraints are available by using the C<:bool> tag.
74  
75 =head2 make_not_constraint( $c1 )
76  
77 This will create a constraint that will return the negation of the
78 result of constraint $c1.
79  
80 =cut
81  
82 0 ▶sub make_not_constraint {
Subroutine make_not_constraint: not called CC=1 SCAR=6.9
Pod documented
83 0 my $c1 = $_[0];
84 # Closure
85 0 ▶ return sub { ! $c1->( @_ ) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
86 }
87  
88 =head2 make_or_constraint( @constraints )
89  
90 This will create a constraint that will return the result of the first
91 constraint that return an non false result.
92  
93 =cut
94  
95 0 ▶sub make_or_constraint {
Subroutine make_or_constraint: not called CC=1 SCAR=6.9
Pod documented
96 0 my @c = @_;
97 # Closure
98 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
99 0 my $res;
100 0 for my $c ( @c ) {
101 0 $res = $c->( @_ );
102 0 ▶ return $res if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 0 0 0
103 }
104 0 return $res;
105 0 };
106 }
107  
108 =head2 make_and_constraint( @constraints )
109  
110 This will create a constraint that will return the result of the first
111 constraint that return an non false result only if all constraints
112 returns a non-false results.
113  
114 =cut
115  
116 0 ▶sub make_and_constraint {
Subroutine make_and_constraint: not called CC=1 SCAR=6.9
Pod documented
117 0 my @c = @_;
118  
119 # Closure
120 0 ▶ return sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
121 0 my $res;
122 0 for my $c ( @c ) {
123 0 $res = $c->( @_ );
124 0 ▶ return $res if ! $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 0 0
125  
126 0 ▶ $res ||= $res;
Condition0%0/3
$res ||= $res
l!l&&r!l&&!r
000
MC/DC0%0/2
$res ||= $res
$res $res
Truth table0%0/3
A ||= B
A= $resB= $res
ABresult
1X1
011
000
127 }
128 0 return $res;
129 0 };
130 }
131  
132 =pod
133  
134 =head1 SET CONSTRAINTS
135  
136 Those constraints are available by using the C<:set> tag.
137  
138 =head2 make_set_constraint( $res, @elements )
139  
140 This will create a constraint that will return $res if the value
141 is one of the @elements set, or the negation of $res otherwise.
142  
143 The C<eq> operator is used for comparison.
144  
145 =cut
146  
147 0 ▶sub make_set_constraint {
Subroutine make_set_constraint: not called CC=1 SCAR=6.9
Pod documented
148 0 my $res = shift;
149 0 my @values = @_;
150  
151 # Closure
152 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
153 0 my $v = $_[0];
154 0 for my $t ( @values ) {
155 0 ▶ return $res if $t eq $v;
Branch: 1 branch
BranchTrueFalseTotal
if $t eq $v 0 0 0
156 }
157 0 return ! $res;
158 }
159 0 }
160  
161 =head2 make_num_set_constraint( $res, @elements )
162  
163 This will create a constraint that will return $res if the value
164 is one of the @elements set, or the negation of $res otherwise.
165  
166 The C<==> operator is used for comparison.
167  
168 =cut
169  
170 0 ▶sub make_num_set_constraint {
Subroutine make_num_set_constraint: not called CC=1 SCAR=6.9
Pod documented
171 0 my $res = shift;
172 0 my @values = @_;
173  
174 # Closure
175 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
176 0 my $v = $_[0];
177 0 for my $t ( @values ) {
178 0 ▶ return $res if $t == $v;
Branch: 1 branch
BranchTrueFalseTotal
if $t == $v 0 0 0
179 }
180 0 return ! $res;
181 }
182 0 }
183  
184 =head2 make_word_set_constraint( $res, $set )
185  
186 This will create a constraint that will return $res if the value is
187 a word in $set, or the negation of $res otherwise.
188  
189 =cut
190  
191 0 ▶sub make_word_set_constraint {
Subroutine make_word_set_constraint: not called CC=1 SCAR=6.9
Pod documented
192 0 my ($res,$set) = @_;
193  
194 # Closure
195 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
196 0 my $v = $_[0];
197 0 ▶ if ( $set =~ /\b$v\b/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($set =~ /\b$v\b/i) { } 0 0 0
198 0 return $res;
199 } else {
200 0 return ! $res;
201 }
202 }
203 0 }
204  
205 =head2 make_cmp_set_constraint( $res, $cmp, @elements )
206  
207 This will create a constraint that will return $res if the value
208 is one of the @elements set, or the negation of $res otherwise.
209  
210 $cmp is a function which takes two argument and should return true or false depending if the two elements are equal.
211  
212 =cut
213  
214 0 ▶sub make_match_set_constraint {
Subroutine make_match_set_constraint: not called CC=1 SCAR=6.9
Pod undocumented
215 0 my $res = shift;
216 0 my $cmp = shift;
217 0 my @values = @_;
218  
219 # Closure
220 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
221 0 my $v = $_[0];
222 0 for my $t ( @values ) {
223 0 ▶ return $res if $cmp->($v, $t );
Branch: 1 branch
BranchTrueFalseTotal
if &$cmp($v, $t) 0 0 0
224 }
225 0 return ! $res;
226 }
227 0 }
228  
229 =pod
230  
231 =head1 NUMERICAL LOGICAL CONSTRAINTS
232  
233 Those constraints are available by using the C<:num> tag.
234  
235 =head2 make_clamp_constraint( $res, $low, $high )
236  
237 This will create a constraint that will return $res if the value
238 is between $low and $high bounds included or its negation otherwise.
239  
240 =cut
241  
242 0 ▶sub make_clamp_constraint {
Subroutine make_clamp_constraint: not called CC=1 SCAR=6.9
Pod documented
243 0 my ( $res, $low, $high ) = @_;
244  
245 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
246 0 my $v = $_[0];
247 0 ▶ $v < $low || $v > $high ? ! $res : $res;
Branch: 1 branch
BranchTrueFalseTotal
$v < $low || $v > $high ? : 0 0 0
Condition0%0/3
$v < $low or $v > $high
l!l&&r!l&&!r
000
MC/DC0%0/2
$v < $low or $v > $high
$v < $low $v > $high
Truth table0%0/3
A or B
A= $v < $lowB= $v > $high
ABresult
1X1
011
000
248 }
249 0 }
250  
251 =head2 make_lt_constraint( $res, $bound )
252  
253 This will create a constraint that will return $res if the value
254 is lower than $bound, or the negation of $res otherwise.
255  
256 =cut
257  
258 0 ▶sub make_lt_constraint {
Subroutine make_lt_constraint: not called CC=1 SCAR=6.9
Pod documented
259 0 my ( $res, $bound ) = @_;
260  
261 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
262 0 ▶ $_[0] < $bound ? $res : ! $res;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < $bound ? : 0 0 0
263 }
264 0 }
265  
266 =head2 make_le_constraint( $res, $bound )
267  
268 This will create a constraint that will return $res if the value
269 is lower or equal than $bound, or the negation of $res otherwise.
270  
271 =cut
272  
273 0 ▶sub make_le_constraint {
Subroutine make_le_constraint: not called CC=1 SCAR=6.9
Pod documented
274 0 my ( $res, $bound ) = @_;
275  
276 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
277 0 ▶ $_[0] <= $bound ? $res : ! $res;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] <= $bound ? : 0 0 0
278 }
279 0 }
280  
281 =head2 make_gt_constraint( $res, $bound )
282  
283 This will create a constraint that will return $res if the value
284 is greater than $bound, or the negation of $res otherwise.
285  
286 =cut
287  
288 0 ▶sub make_gt_constraint {
Subroutine make_gt_constraint: not called CC=1 SCAR=6.9
Pod documented
289 0 my ( $res, $bound ) = @_;
290  
291 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
292 0 ▶ $_[0] >= $bound ? $res : ! $res;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] >= $bound ? : 0 0 0
293 }
294 0 }
295  
296 =head2 make_ge_constraint( $res, $bound )
297  
298 This will create a constraint that will return $res if the value
299 is greater or equal than $bound, or the negation of $res otherwise.
300  
301 =cut
302  
303 0 ▶sub make_ge_constraint {
Subroutine make_ge_constraint: not called CC=1 SCAR=6.9
Pod documented
304 0 my ( $res, $bound ) = @_;
305  
306 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
307 0 ▶ $_[0] >= $bound ? $res : ! $res;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] >= $bound ? : 0 0 0
308 }
309 0 }
310  
311 =head1 OTHER CONSTRAINTS
312  
313 =head2 make_length_constraint($max_length)
314  
315 This will create a constraint that will return true if the value
316 has a length of less than or equal to $max_length
317  
318 =cut
319  
320 2 ▶sub make_length_constraint {
Subroutine make_length_constraint: called CC=1 SCAR=0.0
Pod documented
321 2 my $max_length = shift;
322 2 ▶ return sub { length(shift) <= $max_length };
Subroutine __ANON__: called CC=1 SCAR=0.0
323 }
324  
325 1 1;
326  
327  
328 __END__
« blib/lib/Data/FormValidator/Constraints.pm ↑ Summary blib/lib/Data/FormValidator/Constraints/Dates.pm »