| 1 | package Validator::Lazy::Role::Check::Phone; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | Validator::Lazy::Role::Check::Phone | ||||||||||||||||||||||||||||
| 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( { phone => { Phone => { mobile|not_mobile => 1/0, cc|country => [] } } } ); | ||||||||||||||||||||||||||||
| 19 | my $v = Validator::Lazy->new( { phone => { Phone => [ mobile|not_mobile|your_country_code(s) ] } } ); | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | my $ok = $v->check( phone => 'xxxxx' ); # ok is false | ||||||||||||||||||||||||||||
| 22 | say Dumper $v->errors; # [ { code => 'PHONE_BAD_FORMAT', field => 'phone', data => { } } ] | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | my $v = Validator::Lazy->new( { phone => { Phone => { mobile => 1, cc => [ 'US', 'CA' ] } } } ); | ||||||||||||||||||||||||||||
| 25 | my $v = Validator::Lazy->new( { phone => [ 'mobile', 'US', 'CA' ] } ); | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | An internal Role for Validator::Lazy, part of Validator::Lazy package. | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | Provides "Phone" type for Validator::Lazy config. | ||||||||||||||||||||||||||||
| 32 | Allows to check value as Phone number. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head1 METHODS | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head2 C<check> | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Called from inside if Validator::Lazy->check process | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | Temporary overrides internal Validator::Lazy::check method like this: | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | $validator->check( $value, $param ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | $param - is a list = [ mobile|not_mobile|2chars_values_traits_as_allowed_country_codes ] | ||||||||||||||||||||||||||||
| 46 | OR | ||||||||||||||||||||||||||||
| 47 | $param - is a hash = { mobile => 1, not_mobile => 1, cc => ['list or string'] ] | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | all params are optional | ||||||||||||||||||||||||||||
| 50 | in case, whel all params are omitted, then simple phone check will be done. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | $value - your value to check | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head1 SUPPORT AND DOCUMENTATION | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | After installing, you can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | perldoc Validator::Lazy | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | You can also look for information at: | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | RT, CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 64 | http://rt.cpan.org/NoAuth/Bugs.html?Dist=Validator-Lazy | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | AnnoCPAN, Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 67 | http://annocpan.org/dist/Validator-Lazy | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | CPAN Ratings | ||||||||||||||||||||||||||||
| 70 | http://cpanratings.perl.org/d/Validator-Lazy | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | Search CPAN | ||||||||||||||||||||||||||||
| 73 | http://search.cpan.org/dist/Validator-Lazy/ | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | ANTONC <antonc@cpan.org> | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 83 | under the terms of the the Artistic License (2.0). You may obtain a | ||||||||||||||||||||||||||||
| 84 | copy of the full license at: | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | L<http://www.perlfoundation.org/artistic_license_2_0> | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 4 | ▶ | use v5.14.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 4 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | 4 | ▶ | use Modern::Perl; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | 4 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 4 | ▶ | use Number::Phone; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 4 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 99 | 4 | BAD_FORMAT => 'PHONE_BAD_FORMAT', | |||||||||||||||||||||||||||
| 100 | BAD_NUMBER => 'PHONE_BAD_NUMBER', | ||||||||||||||||||||||||||||
| 101 | MOBILE => 'PHONE_IS_MOBILE', | ||||||||||||||||||||||||||||
| 102 | NOT_MOBILE => 'PHONE_IS_NOT_MOBILE', | ||||||||||||||||||||||||||||
| 103 | BAD_COUNTRY => 'PHONE_WRONG_COUNTRY', | ||||||||||||||||||||||||||||
| 104 | 4 | }; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 76 | ▶ | sub before_check { | ||||||||||||||||||||||||||
|
Subroutine
before_check: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 108 | 76 | my ( $self, $value ) = @_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 76 | ▶ | if ( $value ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 45 | $value =~ s/(^\s+|\s+$)//aaig; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 76 | return $value; | |||||||||||||||||||||||||||
| 115 | }; | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 76 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=16 SCAR=27.8
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 76 | my ( $self, $value, $param ) = @_; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # not required | ||||||||||||||||||||||||||||
| 121 | 76 | ▶ | return $value unless $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 45 | ▶ | if( $value !~ /^\+?[1-9][0-9]{0,2}[. ]?\(?[0-9]{2,3}\)?\s*[0-9 -]{3,9}$/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 5 | $self->add_error( BAD_FORMAT ); | |||||||||||||||||||||||||||
| 125 | 5 | return $value; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 40 | my $phone = Number::Phone->new( $value ); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 40 | ▶ | unless ( $phone && $phone->is_valid ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$phone and $phone->is_valid
MC/DC50%1/2
$phone and $phone->is_valid
$phone
$phone->is_valid
Truth table66%2/3
A and B
A= $phoneB= $phone->is_valid
| |||||||||||||||||||||||||||||
| 131 | 1 | $self->add_error( BAD_NUMBER ); | |||||||||||||||||||||||||||
| 132 | 1 | return $value; | |||||||||||||||||||||||||||
| 133 | }; | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 39 | ▶ | my @param_keys = ref $param eq 'ARRAY' ? @$param : keys %$param; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 39 | ▶ | if ( grep /^mobile$/, @param_keys and !$phone->is_mobile ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
grep /^mobile$/u, @param_keys and not $phone->is_mobile
MC/DC0%0/2
grep /^mobile$/u, @param_keys and not $phone->is_mobile
grep /^mobile$/u, @param_keys
not $phone->is_mobile
Truth table66%2/3
A and not(B)
A= grep /^mobile$/u, @param_keysB= not $phone->is_mobile
| |||||||||||||||||||||||||||||
| 139 | 0 | $self->add_error( NOT_MOBILE ); | |||||||||||||||||||||||||||
| 140 | 0 | return $value; | |||||||||||||||||||||||||||
| 141 | }; | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 39 | ▶ | if ( grep /^not_mobile$/, @param_keys and $phone->is_mobile ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
grep /^not_mobile$/u, @param_keys and $phone->is_mobile
MC/DC100%2/2
grep /^not_mobile$/u, @param_keys and $phone->is_mobile
grep /^not_mobile$/u, @param_keys
$phone->is_mobile
Truth table100%3/3
A and B
A= grep /^not_mobile$/u, @param_keysB= $phone->is_mobile
| |||||||||||||||||||||||||||||
| 144 | 2 | $self->add_error( MOBILE ); | |||||||||||||||||||||||||||
| 145 | 2 | return $value; | |||||||||||||||||||||||||||
| 146 | }; | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 37 | my @cc = grep /^.{2}$/, @param_keys; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 37 | ▶ | if ( ref $param eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 34 | for ( qw/ cc country / ) { | |||||||||||||||||||||||||||
| 153 | 68 | ▶ | next unless $param->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 5 | ▶ | $param->{$_} = [ $param->{$_} ] unless ref $param->{$_}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 5 | push @cc, @{ $param->{$_} }; | |||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | } | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 37 | ▶ | return $value unless @cc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 7 | ▶ | unless ( grep { $_ eq $phone->country } @cc ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 3 | $self->add_error( BAD_COUNTRY, { required => \@cc, current => $phone->country } ); | |||||||||||||||||||||||||||
| 164 | 3 | return $value; | |||||||||||||||||||||||||||
| 165 | }; | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 4 | return $value; | |||||||||||||||||||||||||||
| 168 | }; | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 4 | 1; | |||||||||||||||||||||||||||