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 72     72   3186085 use Mojo::Base -strict;
  72         8149  
  72         499  
3 72     72   6938 use Mojolicious::Plugin::Vparam::Common;
  72         149  
  72         38619  
4              
5             sub check_phone($) {
6 16 100   16 0 42 return 'Value not defined' unless defined $_[0];
7 13 50       34 return 'Value is not set' unless length $_[0];
8 13 50       46 return 'The number should be in the format +...'
9             unless $_[0] =~ m{^\+\d};
10 13 50       41 return 'The number must be a minimum of 11 digits'
11             unless $_[0] =~ m{^\+\d{11}};
12 13 100       44 return 'The number should be no more than 16 digits'
13             unless $_[0] =~ m{^\+\d{11,16}(?:\D|$)};
14 12 50       39 return 'Wrong format'
15             unless $_[0] =~ m{^\+\d{11,16}(?:[pw]\d+)?$};
16 12         47 return 0;
17             }
18              
19             sub fix_phone($$$) {
20 13     13 0 28 my ($sign, $phone, $code) = @_;
21              
22 13 100       32 if( $code eq 'ru' ) {
23 3 100       9 unless( $sign ) {
24 1         6 s{^8}{7} for $phone;
25             }
26             }
27              
28 13         35 return $phone;
29             }
30              
31             sub parse_phone($$$$) {
32 16     16 0 37 my ($str, $country, $region, $fix_code) = @_;
33 16 100       36 return undef unless $str;
34              
35             # Clear
36 15         106 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       42 return undef unless $phone;
41              
42             # Add country and region codes if defined
43 14 100 100     46 $phone = $region . $phone if $region and 11 > length $phone;
44 14 100 100     38 $phone = $country . $phone if $country and 11 > length $phone;
45 14 100       47 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       46 $str = $str . lc $pause if defined $pause;
52 13 100       27 $str = $str . $add if defined $add;
53              
54 13         42 return $str;
55             }
56              
57             sub register {
58 74     74 0 263 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   47 },
68 16     16   34 valid => sub { check_phone $_[1] },
69 74         676 );
70              
71 74         336 return;
72             }
73              
74             1;