Lazy/Role/Check/CountryCode.pm

filter: Statementstmt 100% 30 / 30 Branchbran 87.5% 14 / 16 Conditioncond 83.3% 5 / 6 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 93.6% 59 / 63 CC 11 SCAR 20.7
CC
11
Cov
94%
check
11.1
BEGIN
1.0
BEGIN
1.0
CRAP 7.9
« Lazy/Role/Check/Phone.pm ↑ Summary Lazy/Role/Check/Trim.pm »
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
BranchTrueFalseTotal
unless $value 4 31 35
96  
97 31 ▶ my @allowed_cc =
Branch: 3 branches
BranchTrueFalseTotal
$param ? : 31 0 31
ref $param && ref $param eq 'ARRAY' ? : 2 29 31
ref $param ? : 28 1 29
Condition100%3/3
ref $param and ref $param eq "ARRAY"
!ll&&!rl&&r
1282
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"
ABresult
0X0
100
111
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)
!ll&&!rl&&r
1030
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)
ABresult
0X0
100
111
113 &&
114 $l->code2name( $value )
115 }
116 31 ▶ or do {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $value =~ /^[A-Z]{2}$/u and $l->code2name($value) } } or do { $self->add_error(scalar @allowed_cc ? {"required", \@allowed_cc} : ()); return $value } 30 1 31
117 1 ▶ $self->add_error( scalar @allowed_cc ? { required => \@allowed_cc } : () );
Branch: 1 branch
BranchTrueFalseTotal
scalar @allowed_cc ? : 0 1 1
118 1 return $value;
119 };
120  
121 30 ▶ if ( @allowed_cc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@allowed_cc) 3 27 30
122 3 ▶ unless ( scalar grep /^$value$/, @allowed_cc ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar grep(/^$value$/u, @allowed_cc)) 2 1 3
123 2 $self->add_error( { required => \@allowed_cc } );
124 };
125 };
126  
127 30 return $value;
128 };
129  
130 4 1;
« Lazy/Role/Check/Phone.pm ↑ Summary Lazy/Role/Check/Trim.pm »