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   3650696 use Mojo::Base -strict;
  72         8933  
  72         555  
3 72     72   8832 use Mojolicious::Plugin::Vparam::Common;
  72         174  
  72         50906  
4              
5             sub check_phone($) {
6 16 100   16 0 58 return 'Value not defined' unless defined $_[0];
7 13 50       51 return 'Value is not set' unless length $_[0];
8 13 50       71 return 'The number should be in the format +...'
9             unless $_[0] =~ m{^\+\d};
10 13 50       63 return 'The number must be a minimum of 11 digits'
11             unless $_[0] =~ m{^\+\d{11}};
12 13 100       67 return 'The number should be no more than 16 digits'
13             unless $_[0] =~ m{^\+\d{11,16}(?:\D|$)};
14 12 50       68 return 'Wrong format'
15             unless $_[0] =~ m{^\+\d{11,16}(?:[pw]\d+)?$};
16 12         74 return 0;
17             }
18              
19             sub fix_phone($$$) {
20 13     13 0 38 my ($sign, $phone, $code) = @_;
21              
22 13 100       51 if( $code eq 'ru' ) {
23 3 100       7 unless( $sign ) {
24 1         7 s{^8}{7} for $phone;
25             }
26             }
27              
28 13         42 return $phone;
29             }
30              
31             sub parse_phone($$$$) {
32 16     16 0 57 my ($str, $country, $region, $fix_code) = @_;
33 16 100       52 return undef unless $str;
34              
35             # Clear
36 15         172 s{[.,]}{w}g, s{[^0-9pw+]}{}ig, s{w{2,}}{w}ig, s{p{2,}}{p}ig for $str;
37              
38             # Split
39 15         132 my ($sign, $phone, $pause, $add) = $str =~ m{^(\+)?(\d+)([wp])?(\d+)?$}i;
40 15 100       59 return undef unless $phone;
41              
42             # Add country and region codes if defined
43 14 100 100     67 $phone = $region . $phone if $region and 11 > length $phone;
44 14 100 100     55 $phone = $country . $phone if $country and 11 > length $phone;
45 14 100       74 return undef unless 10 <= length $phone;
46              
47             # Some country deprication fix
48 13         51 $phone = fix_phone $sign, $phone, $fix_code;
49              
50 13         50 $str = '+' . $phone;
51 13 100       64 $str = $str . lc $pause if defined $pause;
52 13 100       41 $str = $str . $add if defined $add;
53              
54 13         60 return $str;
55             }
56              
57             sub register {
58 74     74 0 265 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   74 },
68 16     16   58 valid => sub { check_phone $_[1] },
69 74         750 );
70              
71 74         374 return;
72             }
73              
74             1;