blib/lib/Data/FormValidator/Constraints/DateTime.pm

filter: Statementstmt 100% 231 / 231 Branchbran 84.2% 96 / 114 Conditioncond 68.5% 74 / 108 MC/DCmcdc 46.6% 28 / 60 Subroutinesub 100% 47 / 47 Podpod 50.0% 13 / 26 totaltotal 83.4% 489 / 586 CC 95 SCAR 15.4
CC
95
Cov
89%
match_ymd_to_datetime
11.0
match_between_datetimes
9.0
match_before_datetime
6.0
CRAP 4.7
↑ Summary
1 package Data::FormValidator::Constraints::DateTime;
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 our @ISA = qw(Exporter);
9 6 our @EXPORT_OK = qw(
10 to_datetime
11 ymd_to_datetime
12 before_today
13 after_today
14 ymd_before_today
15 ymd_after_today
16 before_datetime
17 after_datetime
18 between_datetimes
19 to_mysql_datetime
20 to_mysql_date
21 to_mysql_timestamp
22 to_pg_datetime
23 );
24  
25 6 our %EXPORT_TAGS = (
26 all => \@EXPORT_OK,
27 mysql => [qw(to_mysql_datetime to_mysql_date to_mysql_timestamp)],
28 pg => [qw(to_pg_datetime)],
29 );
30 6 our $VERSION = '1.11';
31  
32 =head1 NAME
33  
34 Data::FormValidator::Constraints::DateTime - D::FV constraints for dates and times
35  
36 =head1 DESCRIPTION
37  
38 This package provides constraint routines for L<Data::FormValidator> for
39 dealing with dates and times. It provides an easy mechanism for validating
40 dates of any format (using strptime(3)) and transforming those dates (as long
41 as you 'untaint' the fields) into valid L<DateTime> objects, or into strings
42 that would be properly formatted for various database engines.
43  
44 =head1 ABSTRACT
45  
46 use Data::FormValidator;
47 use Data::FormValidator::Constraints::DateTime qw(:all);
48
49 # create our profile
50 my $profile = {
51 required => [qw(my_date)],
52 constraint_methods => {
53 my_date => to_datetime('%D'), # in the format MM/DD/YYYY
54 },
55 untaint_all_constraints => 1,
56 };
57  
58 # validate 'my_date'
59 my $results = Data::FormValidator->check($my_input, $profile);
60  
61 if( $results->success ) {
62 # if we got here then $results->valid('my_date')
63 # is a valid DateTime object
64 my $datetime = $results->valid('my_date');
65 .
66 .
67 }
68  
69 =head1 STRPTIME FORMATS
70  
71 Most of the validation routines provided by this module use
72 strptime(3) format strings to know what format your date string
73 is in before we can process it. You specify this format for each
74 date you want to validate using by passing it to constraint
75 generation routine (see the example above).
76  
77 We use L<DateTime::Format::Strptime> for this transformation.
78 If you need a list of these formats (if you haven't yet committed
79 them to memory) you can see the strptime(3) man page (if you are
80 on a *nix system) or you can see the L<DateTime::Format::Strptime>
81 documentation.
82  
83 There are however some routines that can live without the format
84 param. These include routines which try and validate according
85 to rules for a particular database (C<< to_mysql_* >> and
86 C<< to_pg_* >>). If no format is provided, then we will attempt to
87 validate according to the rules for that datatype in that database
88 (using L<DateTime::Format::MySQL> and L<DateTime::Format::Pg>).
89 Here are some examples:
90  
91 without a format param
92  
93 my $profile = {
94 required => [qw(my_date)],
95 constraint_methods => {
96 my_date => to_mysql_datetime(),
97 },
98 };
99  
100 with a format param
101  
102 my $profile = {
103 required => [qw(my_date)],
104 constraint_methods => {
105 my_date => to_mysql_datetime('%m/%d/%Y'),
106 },
107 };
108  
109 =head2 DateTime::Format Objects
110  
111 Using strptime(3) format strings gives a lot of flexibility, but sometimes
112 not enough. Suppose you have a web form that allows the user to input a date
113 in the format '11/21/2006' or simply '11/21/06'. A simple format string is
114 not enough. To take full advantage of the DateTime project, any place that
115 you can pass in a strptime(3) format string, you can also pass in a
116 L<DateTime::Format> object. To solve the above problem you might have code
117 that looks like this:
118  
119 # your formatter code
120 package MyProject::DateTime::FlexYear;
121 use DateTime::Format::Strptime;
122  
123 use DateTime::Format::Builder (
124 parsers => {
125 parse_datetime => [
126 sub { eval { DateTime::Format::Strptime->new(pattern => '%m/%d/%Y')->parse_datetime($_[1]) } },
127 sub { eval { DateTime::Format::Strptime->new(pattern => '%m/%d/%y')->parse_datetime($_[1]) } },
128 ]
129 }
130 );
131  
132 1;
133  
134 # in your web validation code
135 my $profile = {
136 required => [qw(my_date)],
137 constraint_methods => {
138 my_date => to_mysql_datetime(MyProject::DateTime::FlexYear->new()),
139 },
140 };
141  
142  
143 =head1 VALIDATION ROUTINES
144  
145 Following is the list of validation routines that are provided
146 by this module.
147  
148 =head2 to_datetime
149  
150 The routine will validate the date aginst a strptime(3) format and
151 change the date string into a DateTime object. This routine B<must>
152 have an accompanying L<strptime|DateTime::Format::Strptime> format param.
153  
154 If the value is untainted (using C<untaint_all_constraints> or
155 C<untaint_constraint_fields>, it will change the date string into a DateTime
156 object.
157  
158 =cut
159  
160 6 ▶sub to_datetime {
Subroutine to_datetime: called CC=1 SCAR=0.3
Pod documented
161 6 my $format = shift;
162 # dereference stuff if we need to
163  
164 8 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
165 8 my $dfv = shift;
166 8 ▶ croak("Must be called using 'constraint_methods'!")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results") 0 8 8
Condition33%1/3
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
!ll&&!rl&&r
008
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
builtin::blessed($dfv) $dfv->isa("Data::FormValidator::Results")
Truth table33%1/3
A and B
A= builtin::blessed($dfv)B= $dfv->isa("Data::FormValidator::Results")
ABresult
0X0
100
111
167 unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
168 8 return match_to_datetime($dfv, $format);
169 }
170 6 }
171  
172 16 ▶sub match_to_datetime {
Subroutine match_to_datetime: called CC=2 SCAR=6.9
Pod undocumented
173 16 my ($dfv, $format) = @_;
174 # if $dfv is a ref then we are called as 'constraint_method'
175 # else as 'constaint'
176 16 ▶ my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
Branch: 1 branch
BranchTrueFalseTotal
ref $dfv ? : 12 4 16
177 # get the DateTime
178 16 my $dt = _get_datetime_from_strp($value, $format);
179 16 return $dt;
180 }
181  
182 318 ▶sub _get_datetime_from_strp {
Subroutine _get_datetime_from_strp: called CC=5 SCAR=16.1
183 318 my ($value, $format) = @_;
184 318 ▶ $format = $$format if( ref $format eq 'SCALAR' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $format eq "SCALAR" 240 78 318
185 318 my $formatter;
186 # if we have a simple scalar for the format
187 318 ▶ if( ! ref $format ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $format) { } 314 4 318
188 # create the formatter
189 314 $formatter = DateTime::Format::Strptime->new(
190 pattern => $format
191 );
192 # else we assume it's a DateTime::Format based object
193 } else {
194 4 $formatter = $format;
195 }
196  
197 # create the DateTime object
198 318 my $dt;
199 318 eval { $dt = $formatter->parse_datetime($value); };
200 # set the formatter (if we can) so that the object
201 # stringifies to the same format as we parsed
202 318 ▶ $dt->set_formatter($formatter)
Branch: 1 branch
BranchTrueFalseTotal
if $dt and $formatter->can("format_datetime") 166 152 318
Condition100%3/3
$dt and $formatter->can("format_datetime")
!ll&&!rl&&r
1502166
MC/DC100%2/2
$dt and $formatter->can("format_datetime")
$dt $formatter->can("format_datetime")
Truth table100%3/3
A and B
A= $dtB= $formatter->can("format_datetime")
ABresult
0X0
100
111
203 if( $dt && $formatter->can('format_datetime') );
204 318 return $dt;
205 }
206  
207 =head2 ymd_to_datetime
208  
209 This routine is used to take multiple inputs (one each for the
210 year, month, and day) and combine them into a L<DateTime> object,
211 validate the resulting date, and give you the resulting DateTime
212 object in your C<< valid() >> results. It must recieve as C<< params >>
213 the year, month, and day inputs in that order. You may also specify
214 additional C<< params >> that will be interpretted as 'hour', 'minute'
215 and 'second' values to use. If none are provided, then the time '00:00:00'
216 will be used.
217  
218 my $profile = {
219 required => [qw(my_year)],
220 constraint_methods => {
221 my_year => ymd_to_datetime(qw(my_year my_month my_day my_hour my_min my_sec)),
222 },
223 };
224  
225 If the value is untainted (using C<untaint_all_constraints> or
226 C<untaint_constraint_fields>, it will change the date string into a DateTime
227 object.
228  
229 =cut
230  
231 3 ▶sub ymd_to_datetime {
Subroutine ymd_to_datetime: called CC=1 SCAR=0.2
Pod documented
232 3 my ($year, $month, $day, $hour, $min, $sec) = @_;
233
234 8 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
235 8 my $dfv = shift;
236 8 ▶ croak("Must be called using 'constraint_methods'!")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results") 0 8 8
Condition33%1/3
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
!ll&&!rl&&r
008
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
builtin::blessed($dfv) $dfv->isa("Data::FormValidator::Results")
Truth table33%1/3
A and B
A= builtin::blessed($dfv)B= $dfv->isa("Data::FormValidator::Results")
ABresult
0X0
100
111
237 unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
238 8 my $data = $dfv->get_input_data(as_hashref => 1);
239 8 return match_ymd_to_datetime(
240 $dfv,
241 _get_value($year, $data),
242 _get_value($month, $data),
243 _get_value($day, $data),
244 _get_value($hour, $data),
245 _get_value($min, $data),
246 _get_value($sec, $data),
247 );
248 3 };
249 }
250  
251 106 ▶sub _get_value {
Subroutine _get_value: called CC=3 SCAR=11.0
252 106 my ($value, $data) = @_;
253 106 ▶ if( $value && exists $data->{$value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value and exists $data->{$value}) { } 63 43 106
Condition100%3/3
$value and exists $data->{$value}
!ll&&!rl&&r
212263
MC/DC100%2/2
$value and exists $data->{$value}
$value exists $data->{$value}
Truth table100%3/3
A and B
A= $valueB= exists $data->{$value}
ABresult
0X0
100
111
254 63 return $data->{$value};
255 } else {
256 43 return $value;
257 }
258 }
259  
260 36 ▶sub match_ymd_to_datetime {
Subroutine match_ymd_to_datetime: called CC=11 SCAR=24.0
Pod undocumented
261 36 my ($dfv, $year, $month, $day, $hour, $min, $sec);
262  
263 # if we were called as a 'constraint_method'
264 36 ▶ if( ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 28 8 36
265 28 ($dfv, $year, $month, $day, $hour, $min, $sec) = @_;
266 # else we were called as a 'constraint'
267 } else {
268 8 ($year, $month, $day, $hour, $min, $sec) = @_;
269 }
270
271 # make sure year, month and day are positive numbers
272 36 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if (defined $year and $year ne '' and defined $month and $month ne '' and defined $day and $day ne '') { } 18 18 36
Condition100%15/15
defined $year and $year ne "" and defined $month and $month ne "" and defined $day and $day ne ""
!ll&&!rl&&r
15318
defined $year and $year ne "" and defined $month and $month ne "" and defined $day
!ll&&!rl&&r
12321
defined $year and $year ne "" and defined $month and $month ne ""
!ll&&!rl&&r
9324
defined $year and $year ne "" and defined $month
!ll&&!rl&&r
6327
defined $year and $year ne ""
!ll&&!rl&&r
3330
MC/DC100%6/6
defined $year and $year ne "" and defined $month and $month ne "" and defined $day and $day ne ""
defined $year $year ne "" defined $month $month ne "" defined $day $day ne ""
Truth table100%7/7
A and B and C and D and E and F
A= defined $yearB= $year ne ""C= defined $monthD= $month ne ""E= defined $dayF= $day ne ""
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
273 defined $year && $year ne ""
274 && defined $month && $month ne ""
275 && defined $day && $day ne ""
276 ) {
277 # set the defaults for time if we don't have any
278 18 ▶ $hour ||= 0;
Condition100%2/2
$hour ||= 0
l!l
315
MC/DC100%1/1
$hour ||= 0
$hour
Truth table100%2/2
A
A= $hour
Aresult
11
00
279 18 ▶ $min ||= 0;
Condition100%2/2
$min ||= 0
l!l
315
MC/DC100%1/1
$min ||= 0
$min
Truth table100%2/2
A
A= $min
Aresult
11
00
280 18 ▶ $sec ||= 0;
Condition100%2/2
$sec ||= 0
l!l
315
MC/DC100%1/1
$sec ||= 0
$sec
Truth table100%2/2
A
A= $sec
Aresult
11
00
281
282 18 my $dt;
283 18 eval {
284 18 $dt = DateTime->new(
285 year => $year,
286 month => $month,
287 day => $day,
288 hour => $hour,
289 minute => $min,
290 second => $sec,
291 );
292 };
293
294 18 return $dt;
295 } else {
296 18 return;
297 }
298 }
299  
300 =head2 before_today
301  
302 This routine will validate the date and make sure it less than or
303 equal to today (using C<< DateTime->today >>). It takes one param
304 which is the <strptime|DateTime::Format::Strptime> format string for the date.
305  
306 If it validates and you tell D::FV to untaint this parameter it will be
307 converted into a DateTime object.
308  
309 # make sure they weren't born in the future
310 my $profile = {
311 required => [qw(birth_date)],
312 constraint_methods => {
313 birth_date => before_today('%m/%d/%Y'),
314 },
315 };
316  
317 If the value is untainted (using C<untaint_all_constraints> or
318 C<untaint_constraint_fields>, it will change the date string into a DateTime
319 object.
320  
321 =cut
322  
323 3 ▶sub before_today {
Subroutine before_today: called CC=1 SCAR=0.3
Pod documented
324 3 my $format = shift;
325  
326 3 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
327 3 my $dfv = shift;
328 3 ▶ croak("Must be called using 'constraint_methods'!")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results") 0 3 3
Condition33%1/3
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
!ll&&!rl&&r
003
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
builtin::blessed($dfv) $dfv->isa("Data::FormValidator::Results")
Truth table33%1/3
A and B
A= builtin::blessed($dfv)B= $dfv->isa("Data::FormValidator::Results")
ABresult
0X0
100
111
329 unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
330 3 return match_before_today($dfv, $format);
331 3 };
332 }
333  
334 9 ▶sub match_before_today {
Subroutine match_before_today: called CC=5 SCAR=16.4
Pod undocumented
335 9 my ($dfv, $format) = @_;
336 # if $dfv is a ref then we are called as 'constraint_method'
337 # else as 'constaint'
338 9 ▶ my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
Branch: 1 branch
BranchTrueFalseTotal
ref $dfv ? : 6 3 9
339 # get the DateTime
340 9 my $dt = _get_datetime_from_strp($value, $format);
341 9 my $dt_target = DateTime->today();
342 # if we have valid DateTime objects and they have the correct
343 # temporaral relationship
344 9 ▶ if( $dt && $dt_target && $dt <= $dt_target ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt and $dt_target and $dt <= $dt_target) { } 6 3 9
Condition50%3/6
$dt and $dt_target and $dt <= $dt_target
!ll&&!rl&&r
036
$dt and $dt_target
!ll&&!rl&&r
009
MC/DC33%1/3
$dt and $dt_target and $dt <= $dt_target
$dt $dt_target $dt <= $dt_target
Truth table50%2/4
A and B and C
A= $dtB= $dt_targetC= $dt <= $dt_target
ABCresult
0XX0
10X0
1100
1111
345 6 return $dt;
346 } else {
347 3 return;
348 }
349 }
350  
351 =head2 after_today
352  
353 This routine will validate the date and make sure it is greater
354 than or equal to today (using C<< DateTime->today() >>). It takes
355 only one param, which is the L<strptime|DateTime::Format::Strptime> format for the date being
356 validated.
357  
358 If it validates and you tell D::FV to untaint this parameter it will be
359 converted into a DateTime object.
360  
361 # make sure the project isn't already due
362 my $profile = {
363 required => [qw(death_date)],
364 constraint_methods => {
365 death_date => after_today('%m/%d/%Y'),
366 },
367 untaint_all_constraints => 1,
368 };
369  
370 If the value is untainted (using C<untaint_all_constraints> or
371 C<untaint_constraint_fields>, it will change the date string into a DateTime
372 object.
373  
374 =cut
375  
376 3 ▶sub after_today {
Subroutine after_today: called CC=1 SCAR=0.3
Pod documented
377 3 my $format = shift;
378  
379 3 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
380 3 my $dfv = shift;
381 3 ▶ croak("Must be called using 'constraint_methods'!")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results") 0 3 3
Condition33%1/3
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
!ll&&!rl&&r
003
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
builtin::blessed($dfv) $dfv->isa("Data::FormValidator::Results")
Truth table33%1/3
A and B
A= builtin::blessed($dfv)B= $dfv->isa("Data::FormValidator::Results")
ABresult
0X0
100
111
382 unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
383 3 return match_after_today($dfv, $format);
384 3 };
385 }
386  
387 9 ▶sub match_after_today {
Subroutine match_after_today: called CC=5 SCAR=16.4
Pod undocumented
388 9 my ($dfv, $format) = @_;
389 # if $dfv is a ref then we are called as 'constraint_method'
390 # else as 'constaint'
391 9 ▶ my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
Branch: 1 branch
BranchTrueFalseTotal
ref $dfv ? : 6 3 9
392 # get the DateTime
393 9 my $dt = _get_datetime_from_strp($value, $format);
394 9 my $dt_target = DateTime->today();
395 # if we have valid DateTime objects and they have the correct
396 # temporaral relationship
397 9 ▶ if( $dt && $dt_target && $dt >= $dt_target ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt and $dt_target and $dt >= $dt_target) { } 6 3 9
Condition50%3/6
$dt and $dt_target and $dt >= $dt_target
!ll&&!rl&&r
036
$dt and $dt_target
!ll&&!rl&&r
009
MC/DC33%1/3
$dt and $dt_target and $dt >= $dt_target
$dt $dt_target $dt >= $dt_target
Truth table50%2/4
A and B and C
A= $dtB= $dt_targetC= $dt >= $dt_target
ABCresult
0XX0
10X0
1100
1111
398 6 return $dt;
399 } else {
400 3 return;
401 }
402 }
403  
404  
405 =head2 ymd_before_today
406  
407 This routine will validate the date and make sure it less than or
408 equal to today (using C<< DateTime->today >>). It works just like
409 L<ymd_to_datetime> in the parameters it takes.
410  
411 If it validates and you tell D::FV to untaint this parameter it will be
412 converted into a DateTime object.
413  
414 # make sure they weren't born in the future
415 my $profile = {
416 required => [qw(birth_date)],
417 constraint_methods => {
418 birth_date => ymd_before_today(qw(dob_year dob_month dob_day)),
419 },
420 untaint_all_constraints => 1,
421 };
422  
423 If the value is untainted (using C<untaint_all_constraints> or
424 C<untaint_constraint_fields>, it will change the date string into a DateTime
425 object.
426  
427 =cut
428  
429 3 ▶sub ymd_before_today {
Subroutine ymd_before_today: called CC=1 SCAR=0.2
Pod documented
430 3 my ($year, $month, $day, $hour, $min, $sec) = @_;
431 3 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
432 3 my $dfv = shift;
433 3 ▶ croak("Must be called using 'constraint_methods'!")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results") 0 3 3
Condition33%1/3
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
!ll&&!rl&&r
003
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
builtin::blessed($dfv) $dfv->isa("Data::FormValidator::Results")
Truth table33%1/3
A and B
A= builtin::blessed($dfv)B= $dfv->isa("Data::FormValidator::Results")
ABresult
0X0
100
111
434 unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
435  
436 3 my $data = $dfv->get_input_data(as_hashref => 1);
437 3 return match_ymd_before_today(
438 $dfv,
439 _get_value($year, $data),
440 _get_value($month, $data),
441 _get_value($day, $data),
442 _get_value($hour, $data),
443 _get_value($min, $data),
444 _get_value($sec, $data),
445 );
446 3 };
447 }
448  
449 6 ▶sub match_ymd_before_today {
Subroutine match_ymd_before_today: called CC=3 SCAR=11.0
Pod undocumented
450 6 my $dt = match_ymd_to_datetime(@_);
451 6 ▶ if( $dt && ( $dt <= DateTime->today ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt and $dt <= "DateTime"->today) 4 2 6
Condition66%2/3
$dt and $dt <= "DateTime"->today
!ll&&!rl&&r
024
MC/DC50%1/2
$dt and $dt <= "DateTime"->today
$dt $dt <= "DateTime"->today
Truth table66%2/3
A and B
A= $dtB= $dt <= "DateTime"->today
ABresult
0X0
100
111
452 4 return $dt;
453 }
454 2 return; # if we get here then it's false
455 }
456  
457 =head2 ymd_after_today
458  
459 This routine will validate the date and make sure it greater than or
460 equal to today (using C<< DateTime->today >>). It works just like
461 L<ymd_to_datetime> in the parameters it takes.
462  
463 If it validates and you tell D::FV to untaint this parameter it will be
464 converted into a DateTime object.
465  
466 # make sure the project isn't already due
467 my $profile = {
468 required => [qw(due_date)],
469 constraint_methods => {
470 birth_date => ymd_after_today(qw(dob_year dob_month dob_day)),
471 },
472 untaint_all_constraints => 1,
473 };
474  
475 If the value is untainted (using C<untaint_all_constraints> or
476 C<untaint_constraint_fields>, it will change the date string into a DateTime
477 object.
478  
479 =cut
480  
481 3 ▶sub ymd_after_today {
Subroutine ymd_after_today: called CC=1 SCAR=0.2
Pod documented
482 3 my ($year, $month, $day, $hour, $min, $sec) = @_;
483 3 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
484 3 my $dfv = shift;
485 3 ▶ croak("Must be called using 'constraint_methods'!")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results") 0 3 3
Condition33%1/3
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
!ll&&!rl&&r
003
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
builtin::blessed($dfv) $dfv->isa("Data::FormValidator::Results")
Truth table33%1/3
A and B
A= builtin::blessed($dfv)B= $dfv->isa("Data::FormValidator::Results")
ABresult
0X0
100
111
486 unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
487  
488 3 my $data = $dfv->get_input_data(as_hashref => 1);
489 3 return match_ymd_after_today(
490 $dfv,
491 _get_value($year, $data),
492 _get_value($month, $data),
493 _get_value($day, $data),
494 _get_value($hour, $data),
495 _get_value($min, $data),
496 _get_value($sec, $data),
497 );
498 3 };
499 }
500  
501 6 ▶sub match_ymd_after_today {
Subroutine match_ymd_after_today: called CC=3 SCAR=11.0
Pod undocumented
502 6 my $dt = match_ymd_to_datetime(@_);
503 6 ▶ if( $dt && ( $dt >= DateTime->today ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt and $dt >= "DateTime"->today) 4 2 6
Condition66%2/3
$dt and $dt >= "DateTime"->today
!ll&&!rl&&r
024
MC/DC50%1/2
$dt and $dt >= "DateTime"->today
$dt $dt >= "DateTime"->today
Truth table66%2/3
A and B
A= $dtB= $dt >= "DateTime"->today
ABresult
0X0
100
111
504 4 return $dt;
505 }
506 2 return; # if we get here then it's false
507 }
508  
509 =head2 before_datetime
510  
511 This routine will validate the date and make sure it occurs before
512 the specified date. It takes two params:
513  
514 =over
515  
516 =item * first, the L<strptime|DateTime::Format::Strptime> format
517  
518 (for both the date we are validating and also the date we want to
519 compare against)
520  
521 =item * second, the date we are comparing against.
522  
523 This date we are comparing against can either be a specified date (using
524 a scalar ref), or a named parameter from your form (using a scalar name).
525  
526 =back
527  
528 If it validates and you tell D::FV to untaint this parameter it will be
529 converted into a DateTime object.
530  
531 # make sure they were born before 1979
532 my $profile = {
533 required => [qw(birth_date)],
534 constraint_methods => {
535 birth_date => before_datetime('%m/%d/%Y', '01/01/1979'),
536 },
537 untaint_all_constraints => 1,
538 };
539  
540 If the value is untainted (using C<untaint_all_constraints> or
541 C<untaint_constraint_fields>, it will change the date string into a DateTime
542 object.
543  
544 =cut
545  
546 6 ▶sub before_datetime {
Subroutine before_datetime: called CC=2 SCAR=7.4
Pod documented
547 6 my ($format, $date) = @_;
548 # dereference stuff if we need to
549 6 ▶ $date = $$date if( ref $date eq 'SCALAR' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $date eq "SCALAR" 0 6 6
550  
551 6 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=15.2
552 6 my $dfv = shift;
553 6 ▶ croak("Must be called using 'constraint_methods'!")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results") 0 6 6
Condition33%1/3
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
!ll&&!rl&&r
006
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
builtin::blessed($dfv) $dfv->isa("Data::FormValidator::Results")
Truth table33%1/3
A and B
A= builtin::blessed($dfv)B= $dfv->isa("Data::FormValidator::Results")
ABresult
0X0
100
111
554 unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
555  
556 # are we using a real date or the name of a parameter
557 6 my $data = $dfv->get_input_data(as_hashref => 1);
558 6 ▶ $date = $data->{$date} if( $data->{$date} );
Branch: 1 branch
BranchTrueFalseTotal
if $data->{$date} 0 6 6
559 6 return match_before_datetime($dfv, $format, $date);
560 6 };
561 }
562  
563 26 ▶sub match_before_datetime {
Subroutine match_before_datetime: called CC=6 SCAR=17.9
Pod undocumented
564 26 my ($dfv, $format, $target_date) = @_;
565 26 ▶ $target_date = $$target_date if( ref $target_date eq 'SCALAR' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $target_date eq "SCALAR" 20 6 26
566 # if $dfv is a ref then we are called as 'constraint_method'
567 # else as 'constaint'
568 26 ▶ my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
Branch: 1 branch
BranchTrueFalseTotal
ref $dfv ? : 16 10 26
569 # get the DateTime
570 26 my $dt = _get_datetime_from_strp($value, $format);
571 26 my $dt_target = _get_datetime_from_strp($target_date, $format);
572 # if we have valid DateTime objects and they have the correct
573 # temporaral relationship
574 26 ▶ if( $dt && $dt_target && $dt < $dt_target ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt and $dt_target and $dt < $dt_target) { } 3 23 26
Condition100%6/6
$dt and $dt_target and $dt < $dt_target
!ll&&!rl&&r
2033
$dt and $dt_target
!ll&&!rl&&r
1646
MC/DC100%3/3
$dt and $dt_target and $dt < $dt_target
$dt $dt_target $dt < $dt_target
Truth table100%4/4
A and B and C
A= $dtB= $dt_targetC= $dt < $dt_target
ABCresult
0XX0
10X0
1100
1111
575 3 return $dt;
576 } else {
577 23 return;
578 }
579 }
580  
581 =head2 after_datetime
582  
583 This routine will validate the date and make sure it occurs after
584 the specified date. It takes two params:
585  
586 =over
587  
588 =item * first, the L<strptime|DateTime::Format::Strptime> format
589  
590 (for both the date we are validating and also the date we want to
591 compare against)
592  
593 =item * second, the date we are comparing against.
594  
595 This date we are comparing against can either be a specified date (using a
596 scalar ref), or a named parameter from your form (using a scalar name).
597  
598 =back
599  
600 # make sure they died after they were born
601 my $profile = {
602 required => [qw(birth_date death_date)],
603 constraint_methods => {
604 death_date => after_datetime('%m/%d/%Y', 'birth_date'),
605 },
606 untaint_all_constraints => 1,
607 };
608  
609 If the value is untainted (using C<untaint_all_constraints> or
610 C<untaint_constraint_fields>, it will change the date string into a DateTime
611 object.
612  
613 =cut
614  
615 6 ▶sub after_datetime {
Subroutine after_datetime: called CC=2 SCAR=7.3
Pod documented
616 6 my ($format, $date) = @_;
617 # dereference stuff if we need to
618 6 ▶ $date = $$date if( ref $date eq 'SCALAR' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $date eq "SCALAR" 0 6 6
619  
620 6 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.8
621 6 my $dfv = shift;
622 6 ▶ croak("Must be called using 'constraint_methods'!")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results") 0 6 6
Condition33%1/3
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
!ll&&!rl&&r
006
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
builtin::blessed($dfv) $dfv->isa("Data::FormValidator::Results")
Truth table33%1/3
A and B
A= builtin::blessed($dfv)B= $dfv->isa("Data::FormValidator::Results")
ABresult
0X0
100
111
623 unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
624  
625 # are we using a real date or the name of a parameter
626 6 my $data = $dfv->get_input_data(as_hashref => 1);
627 6 $date = _get_value($date, $data);
628 6 return match_after_datetime($dfv, $format, $date);
629 6 };
630 }
631  
632 26 ▶sub match_after_datetime {
Subroutine match_after_datetime: called CC=6 SCAR=17.9
Pod undocumented
633 26 my ($dfv, $format, $target_date) = @_;
634 26 ▶ $target_date = $$target_date if( ref $target_date eq 'SCALAR' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $target_date eq "SCALAR" 20 6 26
635 # if $dfv is a ref then we are called as 'constraint_method'
636 # else as 'constaint'
637 26 ▶ my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
Branch: 1 branch
BranchTrueFalseTotal
ref $dfv ? : 16 10 26
638 # get the DateTime
639 26 my $dt = _get_datetime_from_strp($value, $format);
640 26 my $dt_target = _get_datetime_from_strp($target_date, $format);
641 # if we have valid DateTime objects and they have the correct
642 # temporaral relationship
643 26 ▶ if( $dt && $dt_target && $dt > $dt_target ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt and $dt_target and $dt > $dt_target) { } 3 23 26
Condition100%6/6
$dt and $dt_target and $dt > $dt_target
!ll&&!rl&&r
2033
$dt and $dt_target
!ll&&!rl&&r
1646
MC/DC100%3/3
$dt and $dt_target and $dt > $dt_target
$dt $dt_target $dt > $dt_target
Truth table100%4/4
A and B and C
A= $dtB= $dt_targetC= $dt > $dt_target
ABCresult
0XX0
10X0
1100
1111
644 3 return $dt;
645 } else {
646 23 return;
647 }
648 }
649  
650 =head2 between_datetimes
651  
652 This routine will validate the date and make sure it occurs after
653 the first specified date and before the second specified date. It
654 takes three params:
655  
656 =over
657  
658 =item * first, the L<strptime|DateTime::Format::Strptime> format
659  
660 (for both the date we are validating and also the dates we want to
661 compare against)
662  
663 =item * second, the first date we are comparing against.
664  
665 =item * third, the second date we are comparing against.
666  
667 This date (and the second) we are comparing against can either be a specified date
668 (using a scalar ref), or a named parameter from your form (using a scalar name).
669  
670 =back
671  
672 # make sure they died after they were born
673 my $profile = {
674 required => [qw(birth_date death_date marriage_date)],
675 constraint_methods => {
676 marriage_date => between_datetimes('%m/%d/%Y', 'birth_date', 'death_date'),
677 },
678 untaint_all_constraints => 1,
679 };
680  
681 If the value is untainted (using C<untaint_all_constraints> or
682 C<untaint_constraint_fields>, it will change the date string into a DateTime
683 object.
684  
685 =cut
686  
687 8 ▶sub between_datetimes {
Subroutine between_datetimes: called CC=3 SCAR=11.5
Pod documented
688 8 my ($format, $target1, $target2) = @_;
689 # dereference stuff if we need to
690 8 ▶ $target1 = $$target1 if( ref $target1 eq 'SCALAR' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $target1 eq "SCALAR" 0 8 8
691 8 ▶ $target2 = $$target2 if( ref $target2 eq 'SCALAR' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $target2 eq "SCALAR" 0 8 8
692  
693 8 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.6
694 8 my $dfv = shift;
695 8 ▶ croak("Must be called using 'constraint_methods'!")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results") 0 8 8
Condition33%1/3
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
!ll&&!rl&&r
008
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
builtin::blessed($dfv) $dfv->isa("Data::FormValidator::Results")
Truth table33%1/3
A and B
A= builtin::blessed($dfv)B= $dfv->isa("Data::FormValidator::Results")
ABresult
0X0
100
111
696 unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
697  
698 # are we using a real date or the name of a parameter
699 8 my $data = $dfv->get_input_data(as_hashref => 1);
700 8 $target1 = _get_value($target1, $data);
701 8 $target2 = _get_value($target2, $data);
702 8 return match_between_datetimes($dfv, $format, $target1, $target2);
703 }
704 8 }
705  
706 44 ▶sub match_between_datetimes {
Subroutine match_between_datetimes: called CC=9 SCAR=22.0
Pod undocumented
707 44 my ($dfv, $format, $target1, $target2) = @_;
708 44 ▶ $target1 = $$target1 if( ref $target1 eq 'SCALAR' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $target1 eq "SCALAR" 36 8 44
709 44 ▶ $target2 = $$target2 if( ref $target2 eq 'SCALAR' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $target2 eq "SCALAR" 36 8 44
710  
711 # if $dfv is a ref then we are called as 'constraint_method'
712 # else as 'constaint'
713 44 ▶ my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
Branch: 1 branch
BranchTrueFalseTotal
ref $dfv ? : 26 18 44
714 # get the DateTime
715 44 my $dt = _get_datetime_from_strp($value, $format);
716 44 my $dt_target1 = _get_datetime_from_strp($target1, $format);
717 44 my $dt_target2 = _get_datetime_from_strp($target2, $format);
718 # if we have valid DateTime objects and they have the correct
719 # temporaral relationship
720 44 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if ($dt and $dt_target1 and $dt_target2 and $dt > $dt_target1 and $dt < $dt_target2) { } 3 41 44
Condition100%12/12
$dt and $dt_target1 and $dt_target2 and $dt > $dt_target1 and $dt < $dt_target2
!ll&&!rl&&r
3833
$dt and $dt_target1 and $dt_target2 and $dt > $dt_target1
!ll&&!rl&&r
3536
$dt and $dt_target1 and $dt_target2
!ll&&!rl&&r
2969
$dt and $dt_target1
!ll&&!rl&&r
23615
MC/DC100%5/5
$dt and $dt_target1 and $dt_target2 and $dt > $dt_target1 and $dt < $dt_target2
$dt $dt_target1 $dt_target2 $dt > $dt_target1 $dt < $dt_target2
Truth table100%6/6
A and B and C and D and E
A= $dtB= $dt_target1C= $dt_target2D= $dt > $dt_target1E= $dt < $dt_target2
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
721 $dt
722 && $dt_target1
723 && $dt_target2
724 && $dt > $dt_target1
725 && $dt < $dt_target2
726 ) {
727 3 return $dt;
728 } else {
729 41 return;
730 }
731 }
732  
733 =head1 DATABASE RELATED VALIDATION ROUTINES
734  
735 =head2 to_mysql_datetime
736  
737 The routine will change the date string into a DATETIME datatype
738 suitable for MySQL. If you don't provide a format parameter then
739 this routine will just validate the data as a valid MySQL DATETIME
740 datatype (using L<DateTime::Format::MySQL>).
741  
742 If the value is untainted (using C<untaint_all_constraints> or
743 C<untaint_constraint_fields>, it will change the date string into a DateTime
744 object.
745  
746 =cut
747  
748 9 ▶sub to_mysql_datetime {
Subroutine to_mysql_datetime: called CC=1 SCAR=0.3
Pod documented
749 9 my $format = shift;
750  
751 9 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
752 9 my $dfv = shift;
753 9 ▶ croak("Must be called using 'constraint_methods'!")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results") 0 9 9
Condition33%1/3
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
!ll&&!rl&&r
009
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
builtin::blessed($dfv) $dfv->isa("Data::FormValidator::Results")
Truth table33%1/3
A and B
A= builtin::blessed($dfv)B= $dfv->isa("Data::FormValidator::Results")
ABresult
0X0
100
111
754 unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
755 9 return match_to_mysql_datetime($dfv, $format);
756 }
757 9 }
758  
759 22 ▶sub match_to_mysql_datetime {
Subroutine match_to_mysql_datetime: called CC=5 SCAR=16.1
Pod undocumented
760 22 my ($dfv, $format) = @_;
761 # if $dfv is a ref then we are called as 'constraint_method'
762 # else as 'constaint'
763 22 ▶ my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
Branch: 1 branch
BranchTrueFalseTotal
ref $dfv ? : 13 9 22
764  
765 # make sure they have DateTime::Format::MySQL
766 22 eval { require DateTime::Format::MySQL; };
767 22 ▶ die "DateTime::Format::MySQL is required to use this routine"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 20 22
768 if( $@ );
769 20 my $dt;
770  
771 # if they gave us a format (through params as a scalar ref)
772 # then translate the value
773 20 ▶ if( $format ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format) { } 12 8 20
774 12 $dt = _get_datetime_from_strp($value, $format);
775 # else there is no format, so just use parse_datetime
776 } else {
777 8 eval { $dt = DateTime::Format::MySQL->parse_datetime($value) };
778 }
779 20 ▶ if( $dt ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt) { } 5 15 20
780 5 return DateTime::Format::MySQL->format_datetime($dt);
781 } else {
782 15 return undef;
783 }
784 }
785  
786 =head2 to_mysql_date
787  
788 The routine will change the date string into a DATE datatype
789 suitable for MySQL. If you don't provide a format param then
790 this routine will validate the data as a valid DATE datatype
791 in MySQL (using L<DateTime::Format::MySQL>).
792  
793 If the value is untainted (using C<untaint_all_constraints> or
794 C<untaint_constraint_fields>, it will change the date string into a DateTime
795 object.
796  
797 =cut
798  
799 9 ▶sub to_mysql_date {
Subroutine to_mysql_date: called CC=1 SCAR=0.3
Pod documented
800 9 my $format = shift;
801  
802 9 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
803 9 my $dfv = shift;
804 9 ▶ croak("Must be called using 'constraint_methods'!")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results") 0 9 9
Condition33%1/3
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
!ll&&!rl&&r
009
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
builtin::blessed($dfv) $dfv->isa("Data::FormValidator::Results")
Truth table33%1/3
A and B
A= builtin::blessed($dfv)B= $dfv->isa("Data::FormValidator::Results")
ABresult
0X0
100
111
805 unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
806 9 return match_to_mysql_date($dfv, $format);
807 9 };
808 }
809  
810 22 ▶sub match_to_mysql_date {
Subroutine match_to_mysql_date: called CC=5 SCAR=16.1
Pod undocumented
811 22 my ($dfv, $format) = @_;
812 # if $dfv is a ref then we are called as 'constraint_method'
813 # else as 'constaint'
814 22 ▶ my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
Branch: 1 branch
BranchTrueFalseTotal
ref $dfv ? : 13 9 22
815  
816 # make sure they have DateTime::Format::MySQL
817 22 eval { require DateTime::Format::MySQL; };
818 22 ▶ die "DateTime::Format::MySQL is required to use this routine"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 20 22
819 if( $@ );
820 20 my $dt;
821  
822 # if they gave us a format (through params as a scalar ref)
823 # then translate the value
824 20 ▶ if( $format ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format) { } 12 8 20
825 12 $dt = _get_datetime_from_strp($value, $format);
826 # else there is no format, so just use parse_datetime
827 } else {
828 8 eval { $dt = DateTime::Format::MySQL->parse_date($value) };
829 }
830 20 ▶ if( $dt ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt) { } 5 15 20
831 5 return DateTime::Format::MySQL->format_date($dt);
832 } else {
833 15 return undef;
834 }
835 }
836  
837 =head2 to_mysql_timestamp
838  
839 The routine will change the date string into a TIMESTAMP datatype
840 suitable for MySQL. If you don't provide a format then the data
841 will be validated as a MySQL TIMESTAMP datatype.
842  
843 If the value is untainted (using C<untaint_all_constraints> or
844 C<untaint_constraint_fields>, it will change the date string into a DateTime
845 object.
846  
847 =cut
848  
849 8 ▶sub to_mysql_timestamp {
Subroutine to_mysql_timestamp: called CC=1 SCAR=0.3
Pod documented
850 8 my $format = shift;
851  
852 8 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
853 8 my $dfv = shift;
854 8 ▶ croak("Must be called using 'constraint_methods'!")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results") 0 8 8
Condition33%1/3
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
!ll&&!rl&&r
008
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
builtin::blessed($dfv) $dfv->isa("Data::FormValidator::Results")
Truth table33%1/3
A and B
A= builtin::blessed($dfv)B= $dfv->isa("Data::FormValidator::Results")
ABresult
0X0
100
111
855 unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
856 8 match_to_mysql_timestamp($dfv, $format);
857 8 };
858 }
859  
860 20 ▶sub match_to_mysql_timestamp {
Subroutine match_to_mysql_timestamp: called CC=5 SCAR=16.1
Pod undocumented
861 20 my ($dfv, $format) = @_;
862 # if $dfv is a ref then we are called as 'constraint_method'
863 # else as 'constaint'
864 20 ▶ my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
Branch: 1 branch
BranchTrueFalseTotal
ref $dfv ? : 12 8 20
865 20 my $dt;
866  
867 # if they gave us a format (through params as a scalar ref)
868 # then translate the value
869 20 ▶ if( $format ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format) { } 12 8 20
870 12 $dt = _get_datetime_from_strp($value, $format);
871 # else there is no format, so parse into a timestamp
872 } else {
873 # if it matches a timestamp format YYYYMMDDHHMMSS
874 # but we're actually a little looser than that... we take
875 # YYYY-MM-DD HH:MM:SS with any other potential separators
876 8 ▶ if( $value =~ /(\d{4})\D*(\d{2})\D*(\d{2})\D*(\d{2})\D*(\d{2})\D*(\d{2})/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /(\d{4})\D*(\d{2})\D*(\d{2})\D*(\d{2})\D*(\d{2})\D*(\d{2})/) 2 6 8
877 2 eval {
878 2 $dt = DateTime->new(
879 year => $1,
880 month => $2,
881 day => $3,
882 hour => $4,
883 minute => $5,
884 second => $6,
885 );
886 };
887 }
888 }
889 20 ▶ if( $dt ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt) { } 5 15 20
890 5 return $dt->ymd('') . $dt->hms('');
891 } else {
892 15 return undef;
893 }
894 }
895  
896 =head2 to_pg_datetime
897  
898 The routine will change the date string into a DATETIME datatype
899 suitable for PostgreSQL. If you don't provide a format then the
900 data will validated as a DATETIME datatype in PostgresSQL (using
901 L<DateTime::Format::Pg>).
902  
903 If the value is untainted (using C<untaint_all_constraints> or
904 C<untaint_constraint_fields>, it will change the date string into a DateTime
905 object.
906  
907 =cut
908  
909 9 ▶sub to_pg_datetime {
Subroutine to_pg_datetime: called CC=1 SCAR=0.3
Pod documented
910 9 my $format = shift;
911  
912 9 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
913 9 my $dfv = shift;
914 9 ▶ croak("Must be called using 'constraint_methods'!")
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results") 0 9 9
Condition33%1/3
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
!ll&&!rl&&r
009
MC/DC0%0/2
builtin::blessed($dfv) and $dfv->isa("Data::FormValidator::Results")
builtin::blessed($dfv) $dfv->isa("Data::FormValidator::Results")
Truth table33%1/3
A and B
A= builtin::blessed($dfv)B= $dfv->isa("Data::FormValidator::Results")
ABresult
0X0
100
111
915 unless( blessed $dfv && $dfv->isa('Data::FormValidator::Results') );
916 9 match_to_pg_datetime($dfv, $format);
917 9 };
918 }
919  
920 22 ▶sub match_to_pg_datetime {
Subroutine match_to_pg_datetime: called CC=5 SCAR=16.1
Pod undocumented
921 22 my ($dfv, $format) = @_;
922 # if $dfv is a ref then we are called as 'constraint_method'
923 # else as 'constaint'
924 22 ▶ my $value = ref $dfv ? $dfv->get_current_constraint_value : $dfv;
Branch: 1 branch
BranchTrueFalseTotal
ref $dfv ? : 13 9 22
925  
926 # make sure they have DateTime::Format::MySQL
927 22 eval { require DateTime::Format::Pg; };
928 22 ▶ die "DateTime::Format::Pg is required to use this routine"
Branch: 1 branch
BranchTrueFalseTotal
if $@ 2 20 22
929 if( $@ );
930 20 my $dt;
931  
932 # if they gave us a format (through params as a scalar ref)
933 # then translate the value
934 20 ▶ if( $format ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format) { } 12 8 20
935 12 $dt = _get_datetime_from_strp($value, $format);
936 # else there is no format, so just use parse_datetime
937 } else {
938 8 eval { $dt = DateTime::Format::Pg->parse_datetime($value) };
939 }
940 20 ▶ if( $dt ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt) { } 5 15 20
941 5 return DateTime::Format::Pg->format_datetime($dt);
942 } else {
943 15 return undef;
944 }
945 }
946  
947  
948 =head1 AUTHOR
949  
950 Michael Peters <mpeters@plusthree.com>
951  
952 Thanks to Plus Three, LP (http://www.plusthree.com) for sponsoring my work on this module
953  
954 =head1 CONTRIBUTORS
955  
956 =over
957  
958 =item Mark Stosberg <mark@summersault.com>
959  
960 =item Charles Frank <cfrank@plusthree.com>
961  
962 =item Aaron Ross <aaronelliotross@gmail.com>
963  
964 =back
965  
966 =head1 SUPPORT
967  
968 This module is a part of the larger L<Data::FormValidator> project. If you have
969 questions, comments, bug reports or feature requests, please join the
970 L<Data::FormValidator>'s mailing list.
971  
972 =head1 CAVEAT
973  
974 When passing parameters to typical L<Data::FormValidator> constraints you pass
975 plain scalars to refer to query params and scalar-refs to refer to literals. We get
976 around that in this module by assuming everything could be refering to a query param,
977 and if one is not found, then it's a literal. This works well unless you have query
978 params with names like C<'01/02/2005'> or C<'%m/%d/%Y'>.
979  
980 And if you do, shame on you for having such horrible names.
981  
982 =head1 SEE ALSO
983  
984 L<Data::FormValidator>, L<DateTime>. L<DateTime::Format::Strptime>,
985 L<DateTime::Format::MySQL>, L<DateTime::Format::Pg>
986  
987 =head1 COPYRIGHT & LICENSE
988  
989 Copyright Michael Peters 2010, all rights reserved.
990  
991 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
992  
↑ Summary