File Coverage

blib/lib/Workflow/Validator/MatchesDateFormat.pm
Criterion Covered Total %
statement 36 36 100.0
branch 10 10 100.0
condition 3 3 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 60 60 100.0


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