File Coverage

lib/Workflow/Validator/MatchesDateFormat.pm
Criterion Covered Total %
statement 38 39 97.4
branch 11 12 91.6
condition 3 3 100.0
subroutine 9 9 100.0
pod 1 1 100.0
total 62 64 96.8


line stmt bran cond sub pod time code
1              
2             use warnings;
3 12     12   9077 use strict;
  12         25  
  12         421  
4 12     12   71 use base qw( Workflow::Validator );
  12         17  
  12         338  
5 12     12   57 use DateTime::Format::Strptime;
  12         24  
  12         1581  
6 12     12   4959 use Workflow::Exception qw( configuration_error validation_error );
  12         1524883  
  12         63  
7 12     12   1280 use English qw( -no_match_vars );
  12         25  
  12         677  
8 12     12   490 use Carp qw(carp);
  12         1464  
  12         89  
9 12     12   4627  
  12         24  
  12         3530  
10             $Workflow::Validator::MatchesDateFormat::VERSION = '1.61';
11              
12             __PACKAGE__->mk_accessors('formatter');
13              
14             my ( $self, $params ) = @_;
15             unless ( $params->{date_format} ) {
16 16     16   29 configuration_error "You must define a value for 'date_format' in ",
17 16 100       57 "declaration of validator ", $self->name;
18 1         2 }
19             if ( ref $params->{date_format} ) {
20             configuration_error
21 15 100       41 "The value for 'date_format' must be a simple scalar in ",
22 1         3 "declaration of validator ", $self->name;
23             }
24             my $formatter = DateTime::Format::Strptime->new(
25             pattern => $params->{date_format},
26             on_error => 'undef'
27             );
28 14         92 $self->formatter($formatter);
29             }
30 14         19272  
31             my ( $self, $wf, $date_string ) = @_;
32             return unless ($date_string);
33              
34 15     15 1 23221 # already converted!
35 15 100       99 if ( ref $date_string and eval { $date_string->isa('DateTime'); } ) {
36             return;
37             }
38 14 100 100     127  
  11         114  
39 10         41 if ($EVAL_ERROR) {
40             carp 'Unable to assert DateTime or similar object';
41             }
42 4 50       9  
43 0         0 my $fmt = $self->formatter;
44             my $date_object = $fmt->parse_datetime($date_string);
45             unless ($date_object) {
46 4         10 validation_error
47 4         42 "Date '$date_string' does not match required pattern '",
48 4 100       1317 $fmt->pattern, "'";
49 2         7 }
50             }
51              
52             1;
53              
54              
55             =pod
56              
57             =head1 NAME
58              
59             Workflow::Validator::MatchesDateFormat - Ensure a stringified date matches a given pattern
60              
61             =head1 VERSION
62              
63             This documentation describes version 1.61 of this package
64              
65             =head1 SYNOPSIS
66              
67             <action name="CreateNews">
68             <validator name="DateFormat">
69             <param name="date_format" value="%Y-%m-%d"/>
70             <arg value="$news_post_date"/>
71             </validator>
72             </action>
73              
74             =head1 DESCRIPTION
75              
76             This validator ensures that a given date string matches a C<strptime>
77             pattern. The parameter 'date_format' is used to declare the pattern
78             against which the date string must be matched, and the single argument
79             is the date to match.
80              
81             The 'date_format' pattern is a typical C<strptime> pattern. See
82             L<DateTime::Format::Strptime> for details.
83              
84             B<NOTE>: If you pass an empty string (or no string) to this validator
85             it will not throw an error. Why? If you want a value to be defined it
86             is more appropriate to use the 'is_required' attribute of the input
87             field to ensure it has a value.
88              
89             Also, if you pass a L<DateTime> object to the validator it will not
90             determine whether the date is correct or within a range. As far as it
91             is concerned its job is done.
92              
93             =head2 METHODS
94              
95             =head3 _init
96              
97             This method initializes the class and the enumerated class.
98              
99             It uses L</add_enumerated_values> to add the set of values for enumeration.
100              
101             The primary parameter is value, which should be used to specify the
102             either a single value or a reference to array of values to be added.
103              
104             =head3 validate
105              
106             The validator method is the public API. It works with L<Workflow>.
107              
108             Based on the initialized L<Workflow::Validator> it validates a provided
109             parameter, which should adhere to a predefined date format.
110              
111             =head1 EXCEPTIONS
112              
113             =over
114              
115             =item * You must define a value for 'date_format' in declaration of validator <name>
116              
117             =item * The value for 'date_format' must be a simple scalar in declaration of validator <name>
118              
119             =item * Date '<date_string>' does not match required pattern <pattern>
120              
121             =back
122              
123             =head1 SEE ALSO
124              
125             =over
126              
127             =item L<Workflow>
128              
129             =item L<Workflow::Validator>
130              
131             =item L<Workflow::Exception>
132              
133             =item L<DateTime>
134              
135             =back
136              
137             =head1 COPYRIGHT
138              
139             Copyright (c) 2003-2022 Chris Winters. All rights reserved.
140              
141             This library is free software; you can redistribute it and/or modify
142             it under the same terms as Perl itself.
143              
144             Please see the F<LICENSE>
145              
146             =head1 AUTHORS
147              
148             Please see L<Workflow>
149              
150             =cut