lib/HTML/FormFu/Inflator/DateTime.pm

filter: Statementstmt 98.2% 55 / 56 Branchbran 92.8% 13 / 14 Conditioncond 81.8% 9 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 11 / 11 Podpod 33.3% 1 / 3 totaltotal 92.1% 94 / 102 SCAR 24.9
CC
12
Cov
95%
inflator
10.1
parser
3.0
BEGIN
1.0
CRAP 12.0
« lib/HTML/FormFu/Exception/Input.pm ↑ Summary lib/HTML/FormFu/Role/Element/ProcessOptionsFromModel.pm »
1 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Inflator::DateTime;
4 25 $HTML::FormFu::Inflator::DateTime::VERSION = '2.06';
5 # ABSTRACT: DateTime inflator
6  
7 25 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 25 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 25 extends 'HTML::FormFu::Inflator';
10  
11 25 ▶use HTML::FormFu::Constants qw( $EMPTY_STR );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 25 ▶use DateTime::Format::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 25 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 25 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 25 has strptime => ( is => 'rw', traits => ['Chained'] );
17 25 has time_zone => ( is => 'rw', traits => ['Chained'] );
18  
19 has _builder => (
20 is => 'rw',
21 26 ▶ default => sub { DateTime::Format::Builder->new },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 25 lazy => 1,
23 );
24  
25 sub parser {
26 26 ▶ my ( $self, $arg ) = @_;
Subroutine parser: called CC=3 SCAR=11.0
Pod documented
27  
28 26 ▶ if ( exists $arg->{regex} && !ref $arg->{regex} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $arg->{"regex"} and not ref $arg->{"regex"}) 1 25 26
Condition100%3/3
exists $arg->{"regex"} and not ref $arg->{"regex"}
!ll&&!rl&&r
2411
MC/DC100%2/2
exists $arg->{"regex"} and not ref $arg->{"regex"}
exists $arg->{"regex"} not ref $arg->{"regex"}
Truth table100%3/3
A and not(B)
A= exists $arg->{"regex"}B= not ref $arg->{"regex"}
ABresult
0X0
100
111
29 1 $arg->{regex} = qr/$arg->{regex}/;
30 }
31  
32 26 $self->_builder->parser($arg);
33  
34 26 return $self;
35 }
36  
37 sub inflator {
38 27 ▶ my ( $self, $value ) = @_;
Subroutine inflator: called CC=10 SCAR=23.1
Pod undocumented
39  
40 27 ▶ return if !defined $value || $value eq $EMPTY_STR;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq $HTML::FormFu::Inflator::DateTime::EMPTY_STR 2 25 27
Condition100%3/3
not defined $value or $value eq $HTML::FormFu::Inflator::DateTime::EMPTY_STR
l!l&&r!l&&!r
1125
MC/DC100%2/2
not defined $value or $value eq $HTML::FormFu::Inflator::DateTime::EMPTY_STR
not defined $value $value eq $HTML::FormFu::Inflator::DateTime::EMPTY_STR
Truth table100%3/3
not(A) or B
A= not defined $valueB= $value eq $HTML::FormFu::Inflator::DateTime::EMPTY_STR
ABresult
1X1
011
000
41  
42 25 my $dt = $self->_builder->parse_datetime($value);
43  
44 22 ▶ if ( defined $self->time_zone ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->time_zone) 1 21 22
45 1 $dt->set_time_zone( $self->time_zone );
46 }
47  
48 22 ▶ if ( defined $self->strptime ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->strptime) 11 11 22
49 11 my $strptime = $self->strptime;
50 11 my %args;
51  
52 11 ▶ if ( ( reftype($strptime) || '' ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($strptime) || '') eq 'HASH') { } 2 9 11
Condition100%2/2
builtin::reftype($strptime) || ''
l!l
29
MC/DC100%1/1
builtin::reftype($strptime) || ''
builtin::reftype($strptime)
Truth table100%2/2
A
A= builtin::reftype($strptime)
Aresult
11
00
53 2 %args = %$strptime;
54 }
55 else {
56 9 %args = ( pattern => $strptime );
57 }
58  
59 # Make strptime format the date with the specified time_zone,
60 # this is most likely what the user wants
61 11 ▶ if ( defined $self->time_zone ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->time_zone) 1 10 11
62 1 $args{time_zone} = $self->time_zone;
63 }
64  
65 11 ▶ if ( !exists $args{locale}
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $args{"locale"} and defined(my $locale = $self->locale)) 0 11 11
Condition33%1/3
not exists $args{"locale"} and defined(my $locale = $self->locale)
!ll&&!rl&&r
0110
MC/DC0%0/2
not exists $args{"locale"} and defined(my $locale = $self->locale)
not exists $args{"locale"} defined(my $locale = $self->locale)
Truth table33%1/3
not(A) and B
A= not exists $args{"locale"}B= defined(my $locale = $self->locale)
ABresult
0X0
100
111
66 && defined( my $locale = $self->locale ) )
67 {
68 0 $args{locale} = $locale;
69 }
70  
71 11 my $formatter = DateTime::Format::Strptime->new(%args);
72  
73 11 $dt->set_formatter($formatter);
74 }
75  
76 22 return $dt;
77 }
78  
79 sub clone {
80 2 ▶ my $self = shift;
Subroutine clone: called CC=1 SCAR=0.0
Pod undocumented
81  
82 2 my $clone = $self->SUPER::clone(@_);
83  
84 2 $clone->_builder( $self->_builder->clone );
85  
86 2 return $clone;
87 }
88  
89 25 __PACKAGE__->meta->make_immutable;
90  
91 25 1;
92  
93 __END__
94  
95 =pod
96  
97 =encoding UTF-8
98  
99 =head1 NAME
100  
101 HTML::FormFu::Inflator::DateTime - DateTime inflator
102  
103 =head1 VERSION
104  
105 version 2.06
106  
107 =head1 SYNOPSIS
108  
109 ---
110 elements:
111 - type: Text
112 name: start_date
113 inflators:
114 - type: DateTime
115 parser:
116 strptime: '%d-%m-%Y'
117 strptime:
118 pattern: '%d-%b-%Y'
119 locale: de
120  
121 - type: Text
122 name: end_time
123 inflators:
124 - type: DateTime
125 time_zone: Europe/Rome
126 parser:
127 regex: '^ (\d{2}) - (\d{2}) - (\d{4}) $'
128 params: [day, month, year]
129 strptime: '%d-%m-%Y'
130  
131 An example of using the same parser declaration for both a DateTime
132 constraint and a DateTime inflator, using YAML references:
133  
134 ---
135 elements:
136 - type: Text
137 name: date
138 constraints:
139 - type: DateTime
140 parser: &PARSER
141 strptime: '%d-%m-%Y'
142 inflators:
143 - type: DateTime
144 parser: *PARSER
145  
146 =head1 DESCRIPTION
147  
148 Inflate dates into L<DateTime> objects.
149  
150 For a corresponding deflator, see L<HTML::FormFu::Deflator::Strftime>.
151  
152 =head1 METHODS
153  
154 =head2 parser
155  
156 Arguments: \%args
157  
158 Required. Define the expected input string, so L<DateTime::Format::Builder>
159 knows how to inflate it into a L<DateTime> object.
160  
161 Accepts arguments to be passed to L<DateTime::Format::Builder/parser>.
162  
163 =head2 strptime
164  
165 Arguments: \%args
166  
167 Arguments: $string
168  
169 Optional. Define the format that should be used if the L<DateTime> object is
170 stringified.
171  
172 =head2 time_zone
173  
174 Arguments: $string
175  
176 Optional. You can pass along a time_zone in which the DateTime will be
177 created. This is useful if the string to parse does not contain time zone
178 information and you want the DateTime to be in a specific zone instead
179 of the floating one (which is likely).
180  
181 Accepts a hashref of arguments to be passed to
182 L<DateTime::Format::Strptime/new>. Alternatively, accepts a single string
183 argument, suitable for passing to
184 C<< DateTime::Format::Strptime->new( pattern => $string ) >>.
185  
186 =head1 AUTHOR
187  
188 Carl Franks, C<cfranks@cpan.org>
189  
190 =head1 LICENSE
191  
192 This library is free software, you can redistribute it and/or modify it under
193 the same terms as Perl itself.
194  
195 =head1 AUTHOR
196  
197 Carl Franks <cpan@fireartist.com>
198  
199 =head1 COPYRIGHT AND LICENSE
200  
201 This software is copyright (c) 2018 by Carl Franks.
202  
203 This is free software; you can redistribute it and/or modify it under
204 the same terms as the Perl 5 programming language system itself.
205  
206 =cut
« lib/HTML/FormFu/Exception/Input.pm ↑ Summary lib/HTML/FormFu/Role/Element/ProcessOptionsFromModel.pm »