File Coverage

lib/Mojolicious/Plugin/Vparam/Phone.pm
Criterion Covered Total %
statement 36 36 100.0
branch 26 30 86.6
condition 6 6 100.0
subroutine 8 8 100.0
pod 0 4 0.0
total 76 84 90.4


line stmt bran cond sub pod time code
1             package Mojolicious::Plugin::Vparam::Phone;
2 73     73   1509238 use Mojo::Base -strict;
  73         109686  
  73         432  
3 73     73   7775 use Mojolicious::Plugin::Vparam::Common;
  73         129  
  73         41235  
4              
5             sub check_phone($) {
6 16 100   16 0 36 return 'Value not defined' unless defined $_[0];
7 13 50       28 return 'Value is not set' unless length $_[0];
8 13 50       41 return 'The number should be in the format +...'
9             unless $_[0] =~ m{^\+\d};
10 13 50       33 return 'The number must be a minimum of 11 digits'
11             unless $_[0] =~ m{^\+\d{11}};
12 13 100       38 return 'The number should be no more than 16 digits'
13             unless $_[0] =~ m{^\+\d{11,16}(?:\D|$)};
14 12 50       36 return 'Wrong format'
15             unless $_[0] =~ m{^\+\d{11,16}(?:[pw]\d+)?$};
16 12         31 return 0;
17             }
18              
19             sub fix_phone($$$) {
20 13     13 0 29 my ($sign, $phone, $code) = @_;
21              
22 13 100       29 if( $code eq 'ru' ) {
23 3 100       5 unless( $sign ) {
24 1         7 s{^8}{7} for $phone;
25             }
26             }
27              
28 13         34 return $phone;
29             }
30              
31             sub parse_phone($$$$) {
32 16     16 0 38 my ($str, $country, $region, $fix_code) = @_;
33 16 100       52 return undef unless $str;
34              
35             # Clear
36 15         99 s{[.,]}{w}g, s{[^0-9pw+]}{}ig, s{w{2,}}{w}ig, s{p{2,}}{p}ig for $str;
37              
38             # Split
39 15         85 my ($sign, $phone, $pause, $add) = $str =~ m{^(\+)?(\d+)([wp])?(\d+)?$}i;
40 15 100       33 return undef unless $phone;
41              
42             # Add country and region codes if defined
43 14 100 100     35 $phone = $region . $phone if $region and 11 > length $phone;
44 14 100 100     32 $phone = $country . $phone if $country and 11 > length $phone;
45 14 100       40 return undef unless 10 <= length $phone;
46              
47             # Some country deprication fix
48 13         29 $phone = fix_phone $sign, $phone, $fix_code;
49              
50 13         30 $str = '+' . $phone;
51 13 100       32 $str = $str . lc $pause if defined $pause;
52 13 100       29 $str = $str . $add if defined $add;
53              
54 13         36 return $str;
55             }
56              
57             sub register {
58 75     75 0 216 my ($class, $self, $app, $conf) = @_;
59              
60             $app->vtype(
61             phone =>
62             pre => sub { parse_phone
63             trim( $_[1] ),
64             $conf->{phone_country},
65             $conf->{phone_region},
66             $conf->{phone_fix}
67 16     16   39 },
68 16     16   28 valid => sub { check_phone $_[1] },
69 75         697 );
70              
71 75         321 return;
72             }
73              
74             1;