| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package HTML::CheckArgs::state; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 5 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 4 | 1 |  |  | 1 |  | 5 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 27 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 1 |  |  | 1 |  | 5 | use base 'HTML::CheckArgs::Object'; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 1087 |  | 
| 7 | 1 |  |  | 1 |  | 711 | use HTML::FormatData; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | sub is_valid { | 
| 10 |  |  |  |  |  |  | my $self = shift; | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | my $value = $self->value; | 
| 13 |  |  |  |  |  |  | my $config = $self->config; | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | $self->check_params( | 
| 16 |  |  |  |  |  |  | required => [], | 
| 17 |  |  |  |  |  |  | optional => [ qw( country include_dc include_territories include_military ) ], | 
| 18 |  |  |  |  |  |  | cleanable => 1 | 
| 19 |  |  |  |  |  |  | ); | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | # no value passed in | 
| 22 |  |  |  |  |  |  | if ( $config->{required} && !$value ) { | 
| 23 |  |  |  |  |  |  | $self->error_code( 'state_00' ); # required | 
| 24 |  |  |  |  |  |  | $self->error_message( 'Not given.' ); | 
| 25 |  |  |  |  |  |  | return; | 
| 26 |  |  |  |  |  |  | } elsif ( !$config->{required} && !$value ) { | 
| 27 |  |  |  |  |  |  | $self->value( $value ); | 
| 28 |  |  |  |  |  |  | return 1; | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | # clean it up for validation | 
| 32 |  |  |  |  |  |  | $value =~ s/\.//g; # for S.C. case | 
| 33 |  |  |  |  |  |  | $value = HTML::FormatData->new->format_text( | 
| 34 |  |  |  |  |  |  | $value, clean_whitespace => 1, strip_html => 1, | 
| 35 |  |  |  |  |  |  | ); | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | # which country? must be a two-character country abbr | 
| 38 |  |  |  |  |  |  | # only does careful validation check for US for now | 
| 39 |  |  |  |  |  |  | my $country = uc $config->{params}{country} || 'US'; | 
| 40 |  |  |  |  |  |  |  | 
| 41 |  |  |  |  |  |  | if ( $country eq 'US' ) { | 
| 42 |  |  |  |  |  |  | my %states = get_states_hash( | 
| 43 |  |  |  |  |  |  | include_dc          => $config->{params}{include_dc}, | 
| 44 |  |  |  |  |  |  | include_territories => $config->{params}{include_territories}, | 
| 45 |  |  |  |  |  |  | include_military    => $config->{params}{include_military} | 
| 46 |  |  |  |  |  |  | ); | 
| 47 |  |  |  |  |  |  |  | 
| 48 |  |  |  |  |  |  | my $match = 0; | 
| 49 |  |  |  |  |  |  | while ( my ( $key, $val ) = each( %states ) ) { | 
| 50 |  |  |  |  |  |  | # match two-letter abbrieviation | 
| 51 |  |  |  |  |  |  | if ( lc( $value ) eq lc( $key ) ) { | 
| 52 |  |  |  |  |  |  | $value = uc( $value ); | 
| 53 |  |  |  |  |  |  | $match = 1; | 
| 54 |  |  |  |  |  |  | last; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | # match full state name | 
| 58 |  |  |  |  |  |  | if ( lc( $value ) eq lc( $val ) ) { | 
| 59 |  |  |  |  |  |  | $value = uc( $key ); | 
| 60 |  |  |  |  |  |  | $match = 1; | 
| 61 |  |  |  |  |  |  | last; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | unless ( $match ) { | 
| 66 |  |  |  |  |  |  | $self->error_code( 'state_01' ); # not valid | 
| 67 |  |  |  |  |  |  | $self->error_message( 'Not valid US state; please enter full state name or two-letter abbrieviation.' ); | 
| 68 |  |  |  |  |  |  | return; | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | # if not US, just do a sanity check on length | 
| 72 |  |  |  |  |  |  | } elsif ( length( $value ) > 100 ) { | 
| 73 |  |  |  |  |  |  | $self->error_code( 'state_02' ); # over max length | 
| 74 |  |  |  |  |  |  | $self->error_message( 'Exceeds the maximum allowable length (100 characters).' ); | 
| 75 |  |  |  |  |  |  | return; | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | # return cleaned value (the 2-letter abbr)? | 
| 79 |  |  |  |  |  |  | unless ( $config->{noclean} ) { | 
| 80 |  |  |  |  |  |  | $self->value( $value ); | 
| 81 |  |  |  |  |  |  | } | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | return 1; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | # get_states_hash( include_dc=>1, include_territories=>1, include_military=>1 ) | 
| 88 |  |  |  |  |  |  | # Returns a hash of all 50 states. Optionally, can include DC, the territories, and | 
| 89 |  |  |  |  |  |  | # military APOs. | 
| 90 |  |  |  |  |  |  |  | 
| 91 |  |  |  |  |  |  | sub get_states_hash { | 
| 92 |  |  |  |  |  |  | my %args = @_; | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | my %states = ( | 
| 95 |  |  |  |  |  |  | AL=>"Alabama", | 
| 96 |  |  |  |  |  |  | AK=>"Alaska", | 
| 97 |  |  |  |  |  |  | AZ=>"Arizona", | 
| 98 |  |  |  |  |  |  | AR=>"Arkansas", | 
| 99 |  |  |  |  |  |  | CA=>"California", | 
| 100 |  |  |  |  |  |  | CO=>"Colorado", | 
| 101 |  |  |  |  |  |  | CT=>"Connecticut", | 
| 102 |  |  |  |  |  |  | DE=>"Delaware", | 
| 103 |  |  |  |  |  |  | FL=>"Florida", | 
| 104 |  |  |  |  |  |  | GA=>"Georgia", | 
| 105 |  |  |  |  |  |  | HI=>"Hawaii", | 
| 106 |  |  |  |  |  |  | ID=>"Idaho", | 
| 107 |  |  |  |  |  |  | IL=>"Illinois", | 
| 108 |  |  |  |  |  |  | IN=>"Indiana", | 
| 109 |  |  |  |  |  |  | IA=>"Iowa", | 
| 110 |  |  |  |  |  |  | KS=>"Kansas", | 
| 111 |  |  |  |  |  |  | KY=>"Kentucky", | 
| 112 |  |  |  |  |  |  | LA=>"Louisiana", | 
| 113 |  |  |  |  |  |  | ME=>"Maine", | 
| 114 |  |  |  |  |  |  | MD=>"Maryland", | 
| 115 |  |  |  |  |  |  | MA=>"Massachusetts", | 
| 116 |  |  |  |  |  |  | MI=>"Michigan", | 
| 117 |  |  |  |  |  |  | MN=>"Minnesota", | 
| 118 |  |  |  |  |  |  | MS=>"Mississippi", | 
| 119 |  |  |  |  |  |  | MO=>"Missouri", | 
| 120 |  |  |  |  |  |  | MT=>"Montana", | 
| 121 |  |  |  |  |  |  | 'NE'=>"Nebraska", | 
| 122 |  |  |  |  |  |  | NV=>"Nevada", | 
| 123 |  |  |  |  |  |  | NH=>"New Hampshire", | 
| 124 |  |  |  |  |  |  | NJ=>"New Jersey", | 
| 125 |  |  |  |  |  |  | NM=>"New Mexico", | 
| 126 |  |  |  |  |  |  | NY=>"New York", | 
| 127 |  |  |  |  |  |  | NC=>"North Carolina", | 
| 128 |  |  |  |  |  |  | ND=>"North Dakota", | 
| 129 |  |  |  |  |  |  | OH=>"Ohio", | 
| 130 |  |  |  |  |  |  | OK=>"Oklahoma", | 
| 131 |  |  |  |  |  |  | 'OR'=>"Oregon", | 
| 132 |  |  |  |  |  |  | PA=>"Pennsylvania", | 
| 133 |  |  |  |  |  |  | RI=>"Rhode Island", | 
| 134 |  |  |  |  |  |  | SC=>"South Carolina", | 
| 135 |  |  |  |  |  |  | SD=>"South Dakota", | 
| 136 |  |  |  |  |  |  | TN=>"Tennessee", | 
| 137 |  |  |  |  |  |  | TX=>"Texas", | 
| 138 |  |  |  |  |  |  | UT=>"Utah", | 
| 139 |  |  |  |  |  |  | VT=>"Vermont", | 
| 140 |  |  |  |  |  |  | VA=>"Virginia", | 
| 141 |  |  |  |  |  |  | WA=>"Washington", | 
| 142 |  |  |  |  |  |  | WV=>"West Virginia", | 
| 143 |  |  |  |  |  |  | WI=>"Wisconsin", | 
| 144 |  |  |  |  |  |  | WY=>"Wyoming" | 
| 145 |  |  |  |  |  |  | ); | 
| 146 |  |  |  |  |  |  |  | 
| 147 |  |  |  |  |  |  | if ( $args{include_dc} ) { | 
| 148 |  |  |  |  |  |  | $states{DC} = "District of Columbia"; | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 |  |  |  |  |  |  | if ( $args{include_territories} ) { | 
| 152 |  |  |  |  |  |  | $states{AS} = "American Samoa"; | 
| 153 |  |  |  |  |  |  | $states{GU} = "Guam"; | 
| 154 |  |  |  |  |  |  | $states{MP} = "Northern Mariana Islands"; | 
| 155 |  |  |  |  |  |  | $states{PR} = "Puerto Rico"; | 
| 156 |  |  |  |  |  |  | $states{VI} = "Virgin Islands"; | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | if ( $args{include_military} ) { | 
| 160 |  |  |  |  |  |  | $states{AA} = 'Armed Forces Americas'; | 
| 161 |  |  |  |  |  |  | $states{AE} = 'Armed Forces Europe'; | 
| 162 |  |  |  |  |  |  | $states{AP} = 'Armed Forces Pacific'; | 
| 163 |  |  |  |  |  |  | } | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | return %states; | 
| 166 |  |  |  |  |  |  | } | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | 1; |