| 1 | package Validator::Lazy::Role::Check::CountryCode; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 4 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | Validator::Lazy::Role::Check::CountryCode | ||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||
| 10 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||
| 12 | Version 0.03 | ||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||
| 17 | use Validator::Lazy; | ||||||||||||||||||||||||||||||||||||
| 18 | my $v = Validator::Lazy->new( { cc => { CountryCode => 'US' } } ); # empty, or list, or scalar with 2letters countrycode | ||||||||||||||||||||||||||||||||||||
| 19 | my $v = Validator::Lazy->new( { cc => { CountryCode => [ 'US' ] } } ); # empty, or list, or scalar with 2letters countrycode | ||||||||||||||||||||||||||||||||||||
| 20 | my $v = Validator::Lazy->new( { cc => { CountryCode => [ 'DE', 'US' ] } } ); # empty, or list, or scalar with 2letters countrycode | ||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||
| 22 | my $ok = $v->check( cc => 'ES' ); # ok is false | ||||||||||||||||||||||||||||||||||||
| 23 | say Dumper $v->errors; # [ { code => 'COUNTRYCODE_ERROR', field => 'cc', data => { required => [ 'DE', 'US' ] } } ] | ||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||
| 26 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||
| 28 | An internal Role for Validator::Lazy, part of Validator::Lazy package. | ||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||
| 30 | Provides "CountryCode" type for Validator::Lazy config. | ||||||||||||||||||||||||||||||||||||
| 31 | Allows to check 2letters country code. | ||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||
| 33 | When called without param - performs simple check for any country code | ||||||||||||||||||||||||||||||||||||
| 34 | When param is passed - performs additional check as value-in-list | ||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||
| 36 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||
| 38 | =head2 C<check> | ||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||
| 40 | Called from inside if Validator::Lazy->check process | ||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||
| 42 | Temporary overrides internal Validator::Lazy::check method like this: | ||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||
| 44 | $validator->check( $value, $param ); | ||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||
| 46 | $param - scalar or list. can contain any 2-letters country code(s) | ||||||||||||||||||||||||||||||||||||
| 47 | $value - your value to check | ||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||
| 50 | =head1 SUPPORT AND DOCUMENTATION | ||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||
| 52 | After installing, you can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||
| 54 | perldoc Validator::Lazy | ||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||
| 56 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||
| 58 | RT, CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||||||||||
| 59 | http://rt.cpan.org/NoAuth/Bugs.html?Dist=Validator-Lazy | ||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||
| 61 | AnnoCPAN, Annotated CPAN documentation | ||||||||||||||||||||||||||||||||||||
| 62 | http://annocpan.org/dist/Validator-Lazy | ||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||
| 64 | CPAN Ratings | ||||||||||||||||||||||||||||||||||||
| 65 | http://cpanratings.perl.org/d/Validator-Lazy | ||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||
| 67 | Search CPAN | ||||||||||||||||||||||||||||||||||||
| 68 | http://search.cpan.org/dist/Validator-Lazy/ | ||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||
| 71 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||
| 73 | ANTONC <antonc@cpan.org> | ||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||
| 75 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||
| 77 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||
| 78 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||||||||||||||||||||||||
| 79 | copy of the full license at: | ||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||
| 81 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||
| 83 | =cut | ||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||
| 85 | 4 | ▶ | use v5.14.0; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 86 | 4 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 87 | 4 | ▶ | use Modern::Perl; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 88 | 4 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 89 | 4 | ▶ | use Locale::Codes; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||
| 92 | 35 | ▶ | sub check { | ||||||||||||||||||||||||||||||||||
|
Subroutine
check: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 93 | 35 | my ( $self, $value, $param ) = @_; | |||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||
| 95 | 35 | ▶ | return $value unless $value; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||
| 97 | 31 | ▶ | my @allowed_cc = | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
ref $param and ref $param eq "ARRAY"
MC/DC100%2/2
ref $param and ref $param eq "ARRAY"
ref $param
ref $param eq "ARRAY"
Truth table100%3/3
A and B
A= ref $paramB= ref $param eq "ARRAY"
| |||||||||||||||||||||||||||||||||||||
| 98 | $param | ||||||||||||||||||||||||||||||||||||
| 99 | ? ref $param && ref $param eq 'ARRAY' | ||||||||||||||||||||||||||||||||||||
| 100 | ? @$param | ||||||||||||||||||||||||||||||||||||
| 101 | : ref $param | ||||||||||||||||||||||||||||||||||||
| 102 | ? () | ||||||||||||||||||||||||||||||||||||
| 103 | : $param | ||||||||||||||||||||||||||||||||||||
| 104 | : (); | ||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||
| 106 | 31 | chomp $value; | |||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||
| 108 | 31 | my $l = Locale::Codes->new( "country" ); | |||||||||||||||||||||||||||||||||||
| 109 | 31 | $l->show_errors(0); | |||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||
| 111 | eval{ | ||||||||||||||||||||||||||||||||||||
| 112 | 31 | ▶ | $value =~ /^[A-Z]{2}$/ | ||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$value =~ /^[A-Z]{2}$/u && $l->code2name($value)
MC/DC50%1/2
$value =~ /^[A-Z]{2}$/u && $l->code2name($value)
$value =~ /^[A-Z]{2}$/u
$l->code2name($value)
Truth table66%2/3
A && B
A= $value =~ /^[A-Z]{2}$/uB= $l->code2name($value)
| |||||||||||||||||||||||||||||||||||||
| 113 | && | ||||||||||||||||||||||||||||||||||||
| 114 | $l->code2name( $value ) | ||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||
| 116 | 31 | ▶ | or do { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | $self->add_error( scalar @allowed_cc ? { required => \@allowed_cc } : () ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 118 | 1 | return $value; | |||||||||||||||||||||||||||||||||||
| 119 | }; | ||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||
| 121 | 30 | ▶ | if ( @allowed_cc ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 122 | 3 | ▶ | unless ( scalar grep /^$value$/, @allowed_cc ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 123 | 2 | $self->add_error( { required => \@allowed_cc } ); | |||||||||||||||||||||||||||||||||||
| 124 | }; | ||||||||||||||||||||||||||||||||||||
| 125 | }; | ||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||
| 127 | 30 | return $value; | |||||||||||||||||||||||||||||||||||
| 128 | }; | ||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||
| 130 | 4 | 1; | |||||||||||||||||||||||||||||||||||