File Coverage

blib/lib/HTML/CheckArgs/cc_expiration.pm
Criterion Covered Total %
statement 28 30 93.3
branch 6 8 75.0
condition 3 6 50.0
subroutine 4 4 100.0
pod 0 1 0.0
total 41 49 83.6


line stmt bran cond sub pod time code
1             package HTML::CheckArgs::cc_expiration;
2              
3             # to-do: better date format checking with DateTime
4             # add future date check, perhaps 20 years
5              
6 1     1   6 use strict;
  1         1  
  1         33  
7 1     1   6 use warnings;
  1         1  
  1         26  
8              
9 1     1   4 use base 'HTML::CheckArgs::Object';
  1         2  
  1         846  
10              
11             sub is_valid {
12 5     5 0 8 my $self = shift;
13            
14 5         22 my $value = $self->value;
15 5         23 my $config = $self->config;
16              
17 5         28 $self->check_params( required => [], optional => [], cleanable => 0 );
18              
19             # no value passed in
20 4 100 66     37 if ( $config->{required} && !$value ) {
    50 33        
21 1         11 $self->error_code( 'cc_expiration_00' ); # required
22 1         8 $self->error_message( 'Not given.' );
23 1         7 return;
24             } elsif ( !$config->{required} && !$value ) {
25 0         0 return 1;
26             }
27              
28             # quick dumb check for date format
29             # value must be passed in as YYYYMM
30 3 100       11 if ( length( $value ) != 6 ) {
31 1         5 $self->error_code( 'cc_expiration_01' ); # not valid
32 1         4 $self->error_message( 'Not a valid date.' );
33 1         4 return;
34             }
35              
36             # check if is past
37 2         921 my ( $month, $year ) = ( localtime )[4,5];
38 2         13 $month = sprintf( '%02d', $month ); $year += 1900;
  2         5  
39              
40 2 50       12 if ( $value < "$year$month" ) {
41 2         9 $self->error_code( 'cc_expiration_02' ); # already passed
42 2         6 $self->error_message( 'Date has already passed.' );
43 2         7 return;
44             }
45              
46 0           return 1;
47             }
48              
49             1;