File Coverage

lib/Workflow/Validator/MatchesDateFormat.pm
Criterion Covered Total %
statement 39 40 97.5
branch 11 12 91.6
condition 3 3 100.0
subroutine 9 9 100.0
pod 1 1 100.0
total 63 65 96.9


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