| 1 | package HTML::FormHandler::Field::Date; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: a date field with formats | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | $HTML::FormHandler::Field::Date::VERSION = '0.410002'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | extends 'HTML::FormHandler::Field::Text'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use DateTime; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use DateTime::Format::Strptime; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | has '+html5_type_attr' => ( default => 'date' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | has 'format' => ( is => 'rw', isa => 'Str', default => "%Y-%m-%d" ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | has 'locale' => ( is => 'rw', isa => 'Str' ); # TODO | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 3 | has 'time_zone' => ( is => 'rw', isa => 'Str' ); # TODO | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 3 | has 'date_start' => ( is => 'rw', isa => 'Str|CodeRef', clearer => 'clear_date_start' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 3 | has 'date_end' => ( is => 'rw', isa => 'Str|CodeRef', clearer => 'clear_date_end' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 3 | has '+size' => ( default => '10' ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 14 | ▶ | has '+deflate_method' => ( default => sub { \&date_deflate } ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | # translator for Datepicker formats to DateTime strftime formats | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | my $dp_to_dt = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | "d" => "\%e", # day of month (no leading zero) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | "dd" => "\%1", # day of month (2 digits) "%d" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | "o" => "\%4", # day of year (no leading zero) "%{day_of_year}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | "oo" => "\%j", # day of year (3 digits) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | "D" => "\%a", # day name long | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | "DD" => "\%A", # day name short | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | "m" => "\%5", # month of year (no leading zero) "%{day_of_month}" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | "mm" => "\%3", # month of year (two digits) "%m" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | "M" => "\%b", # Month name short | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | "MM" => "\%B", # Month name long | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | "y" => "\%2", # year (2 digits) "%y" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | "yy" => "\%Y", # year (4 digits) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | "@" => "\%s", # epoch | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | our $class_messages = { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 'date_early' => 'Date is too early', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 'date_late' => 'Date is too late', | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 3 | ▶ | sub get_class_messages { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_class_messages: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | return { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | %{ $self->next::method }, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | %$class_messages, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 5 | ▶ | sub date_deflate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
date_deflate: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | my ( $self, $value ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # if not a DateTime, assume correctly formatted string and return | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 5 | ▶ | return $value unless blessed $value && $value->isa('DateTime'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
builtin::blessed($value) and $value->isa("DateTime")
MC/DC50%1/2
builtin::blessed($value) and $value->isa("DateTime")
builtin::blessed($value)
$value->isa("DateTime")
Truth table66%2/3
A and B
A= builtin::blessed($value)B= $value->isa("DateTime")
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | my $format = $self->get_strf_format; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | my $string = $value->strftime($format); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 3 | return $string; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 14 | ▶ | sub validate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
validate: called CC=13 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 14 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 14 | my $format = $self->get_strf_format; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 14 | my @options; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 14 | ▶ | push @options, ( time_zone => $self->time_zone ) if $self->time_zone; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 14 | ▶ | push @options, ( locale => $self->locale ) if $self->locale; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 14 | my $strp = DateTime::Format::Strptime->new( pattern => $format, @options ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 14 | my $dt = eval { $strp->parse_datetime( $self->value ) }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 14 | ▶ | unless ($dt) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 5 | ▶ | $self->add_error( $strp->errmsg || $@ ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$strp->errmsg || $@
MC/DC0%0/2
$strp->errmsg || $@
$strp->errmsg
$@
Truth table33%1/3
A || B
A= $strp->errmsgB= $@
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 9 | $self->_set_value($dt); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 9 | my $val_strp = DateTime::Format::Strptime->new( pattern => "%Y-%m-%d", @options ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 9 | ▶ | if ( my $date_start = $self->date_start ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | ▶ | $date_start = $date_start->() if ref $date_start eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | $date_start = $val_strp->parse_datetime( $date_start ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | die "date_start: " . $val_strp->errmsg unless $date_start; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | my $cmp = DateTime->compare( $date_start, $dt ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | ▶ | $self->add_error($self->get_message('date_early')) if $cmp eq 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 9 | ▶ | if ( my $date_end = $self->date_end ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 3 | ▶ | $date_end = $date_end->() if ref $date_end eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 3 | $date_end = $val_strp->parse_datetime( $date_end ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | ▶ | die "date_end: " . $val_strp->errmsg unless $date_end; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 3 | my $cmp = DateTime->compare( $date_end, $dt ); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | ▶ | $self->add_error($self->get_message('date_late')) if $cmp eq -1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 17 | ▶ | sub get_strf_format { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get_strf_format: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 17 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | # if contains %, then it's a strftime format | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 17 | ▶ | return $self->format if $self->format =~ /\%/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 6 | my $format = $self->format; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 6 | foreach my $dpf ( reverse sort keys %{$dp_to_dt} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 78 | my $strf = $dp_to_dt->{$dpf}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | 78 | $format =~ s/$dpf/$strf/g; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 6 | $format =~ s/\%1/\%d/g, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | $format =~ s/\%2/\%y/g, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | $format =~ s/\%3/\%m/g, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | $format =~ s/\%4/\%{day_of_year}/g, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | $format =~ s/\%5/\%{day_of_month}/g, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | return $format; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 19 | ▶ | before 'get_tag' => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 19 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 19 | ▶ | if ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition77%7/9
$self->form and $self->form->is_html5 and $self->html5_type_attr eq "date" and not $self->format =~ /^(yy|%Y)-(mm|%m)-(dd|%d)$/
$self->form and $self->form->is_html5 and $self->html5_type_attr eq "date"
$self->form and $self->form->is_html5
MC/DC50%2/4
$self->form and $self->form->is_html5 and $self->html5_type_attr eq "date" and not $self->format =~ /^(yy|%Y)-(mm|%m)-(dd|%d)$/
$self->form
$self->form->is_html5
$self->html5_type_attr eq "date"
not $self->format =~ /^(yy|%Y)-(mm|%m)-(dd|%d)$/
Truth table60%3/5
A and B and C and not(D)
A= $self->formB= $self->form->is_html5C= $self->html5_type_attr eq "date"D= not $self->format =~ /^(yy|%Y)-(mm|%m)-(dd|%d)$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | $self->form | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | && $self->form->is_html5 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | && $self->html5_type_attr eq 'date' # subclass may be using different input type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | && not( $self->format =~ /^(yy|%Y)-(mm|%m)-(dd|%d)$/ ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 2 | warn "Form is HTML5, but date field '" . $self->full_name | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | . "' has a format other than %Y-%m-%d, which HTML5 requires for date " | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | . "fields. Either correct the date format or set the is_html5 flag to false."; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 3 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 3 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||