lib/HTML/FormFu/Inflator/CompoundDateTime.pm

filter: Statementstmt 98.4% 62 / 63 Branchbran 57.1% 8 / 14 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 1 totaltotal 85.7% 84 / 98 SCAR 25.1
CC
12
Cov
88%
inflator
13.3
__ANON__
1.0
__ANON__
1.0
CRAP 12.3
« lib/HTML/FormFu/FakeQuery.pm ↑ Summary lib/HTML/FormFu/Exception/Input.pm »
1 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package HTML::FormFu::Inflator::CompoundDateTime;
4 3 $HTML::FormFu::Inflator::CompoundDateTime::VERSION = '2.06';
5 # ABSTRACT: CompoundDateTime inflator
6  
7 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use MooseX::Attribute::Chained;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 extends 'HTML::FormFu::Inflator';
10  
11 3 ▶use HTML::FormFu::Constants qw( $EMPTY_STR );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use DateTime::Format::Strptime;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use List::Util 1.33 qw( none );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 3 ▶use Scalar::Util qw( reftype );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 3 has strptime => ( is => 'rw', traits => ['Chained'] );
19 3 has field_order => ( is => 'rw', traits => ['Chained'] );
20  
21 3 my @known_fields = qw( year month day hour minute second nanosecond time_zone );
22  
23 sub inflator {
24 2 ▶ my ( $self, $value ) = @_;
Subroutine inflator: called CC=12 SCAR=25.9
Pod undocumented
25  
26 2 ▶ return if !defined $value || $value eq $EMPTY_STR;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $value eq $HTML::FormFu::Inflator::CompoundDateTime::EMPTY_STR 0 2 2
Condition33%1/3
not defined $value or $value eq $HTML::FormFu::Inflator::CompoundDateTime::EMPTY_STR
l!l&&r!l&&!r
002
MC/DC0%0/2
not defined $value or $value eq $HTML::FormFu::Inflator::CompoundDateTime::EMPTY_STR
not defined $value $value eq $HTML::FormFu::Inflator::CompoundDateTime::EMPTY_STR
Truth table33%1/3
not(A) or B
A= not defined $valueB= $value eq $HTML::FormFu::Inflator::CompoundDateTime::EMPTY_STR
ABresult
1X1
011
000
27  
28 2 my ( $multi, @fields ) = @{ $self->parent->get_fields };
29 2 my %input;
30  
31 2 ▶ if ( defined( my $order = $self->field_order ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $order = $self->field_order)) { } 1 1 2
32 1 for my $order (@$order) {
33 croak "unknown DateTime field_order name"
34 3 ▶ if none { $order eq $_ } @known_fields;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $order eq $_; } , @known_fields) 0 3 3
35  
36 3 my $field = shift @fields;
37 3 my $name = $field->name;
38  
39 3 $input{$order} = $value->{$name};
40 }
41 }
42 else {
43 1 for my $name ( keys %$value ) {
44 croak "unknown DateTime field name"
45 3 ▶ if none { $name eq $_ } @known_fields;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
if &none(sub { $name eq $_; } , @known_fields) 0 3 3
46 }
47  
48 1 %input = %$value;
49 }
50  
51 2 my $dt;
52  
53 2 eval { $dt = DateTime->new(%input) };
54  
55 2 ▶ return $value if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 2 2
56  
57 2 ▶ if ( defined $self->strptime ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->strptime) 2 0 2
58 2 my $strptime = $self->strptime;
59 2 my %args;
60  
61 2 ▶ if ( ( reftype($strptime) || '' ) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ((builtin::reftype($strptime) || '') eq 'HASH') { } 0 2 2
Condition50%1/2
builtin::reftype($strptime) || ''
l!l
02
MC/DC0%0/1
builtin::reftype($strptime) || ''
builtin::reftype($strptime)
Truth table50%1/2
A
A= builtin::reftype($strptime)
Aresult
11
00
62 0 %args = %$strptime;
63 }
64 else {
65 2 %args = ( pattern => $strptime );
66 }
67  
68 2 my $formatter = DateTime::Format::Strptime->new(%args);
69  
70 2 $dt->set_formatter($formatter);
71 }
72  
73 2 return $dt;
74 }
75  
76 3 __PACKAGE__->meta->make_immutable;
77  
78 3 1;
79  
80 __END__
81  
82 =pod
83  
84 =encoding UTF-8
85  
86 =head1 NAME
87  
88 HTML::FormFu::Inflator::CompoundDateTime - CompoundDateTime inflator
89  
90 =head1 VERSION
91  
92 version 2.06
93  
94 =head1 SYNOPSIS
95  
96 ---
97 element:
98 - type: Multi
99 name: date
100  
101 elements:
102 - name: day
103 - name: month
104 - name: year
105  
106 inflator:
107 - type: CompoundDateTime
108  
109 # get the submitted value as a DateTime object
110  
111 my $date = $form->param_value('date');
112  
113 =head1 DESCRIPTION
114  
115 For use with a L<HTML::FormFu::Element::Multi> group of fields.
116  
117 Changes the input from several fields into a single L<DateTime> value.
118  
119 By default, expects the field names to be any of the following:
120  
121 =over
122  
123 =item year
124  
125 =item month
126  
127 =item day
128  
129 =item hour
130  
131 =item minute
132  
133 =item second
134  
135 =item nanosecond
136  
137 =item time_zone
138  
139 =back
140  
141 =head1 METHODS
142  
143 =head2 field_order
144  
145 Arguments: \@order
146  
147 If your field names doesn't follow the convention listed above, you must
148 provide an arrayref containing the above names, in the order they correspond
149 with your own fields.
150  
151 ---
152 element:
153 - type: Multi
154 name: date
155  
156 elements:
157 - name: m
158 - name: d
159 - name: y
160  
161 inflator:
162 - type: CompoundDateTime
163 field_order:
164 - month
165 - day
166 - year
167  
168 =head2 strptime
169  
170 Arguments: \%args
171  
172 Arguments: $string
173  
174 Optional. Define the format that should be used if the L<DateTime> object is
175 stringified.
176  
177 Accepts a hashref of arguments to be passed to
178 L<DateTime::Format::Strptime/new>. Alternatively, accepts a single string
179 argument, suitable for passing to
180 C<< DateTime::Format::Strptime->new( pattern => $string ) >>.
181  
182 ---
183 inflator:
184 - type: CompoundDateTime
185 strptime:
186 pattern: '%d-%b-%Y'
187 locale: de
188  
189 ---
190 inflator:
191 - type: CompoundDateTime
192 strptime: '%d-%m-%Y'
193  
194 =head1 AUTHOR
195  
196 Carl Franks
197  
198 =head1 LICENSE
199  
200 This library is free software, you can redistribute it and/or modify it under
201 the same terms as Perl itself.
202  
203 =head1 AUTHOR
204  
205 Carl Franks <cpan@fireartist.com>
206  
207 =head1 COPYRIGHT AND LICENSE
208  
209 This software is copyright (c) 2018 by Carl Franks.
210  
211 This is free software; you can redistribute it and/or modify it under
212 the same terms as the Perl 5 programming language system itself.
213  
214 =cut
« lib/HTML/FormFu/FakeQuery.pm ↑ Summary lib/HTML/FormFu/Exception/Input.pm »