| 1 | package Number::Phone::BR; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Brazilian phone numbers | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use Carp qw/confess/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use Number::Phone::BR::Areas qw/code2name mobile_phone_digits_by_area/; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 4 | extends 'Moo::Object', 'Number::Phone'; | |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 4 | our $VERSION = '0.001'; # VERSION | |||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 8 | ▶ | sub country { 'BR' } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
country: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 8 | ▶ | sub country_code { 55 } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
country_code: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | 4 | has subscriber => ( is => 'ro' ); | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 4 | has areacode => ( is => 'ro' ); | |||||||||||||||||||||||||||||||||||||||||
| 16 | 4 | has areaname => ( is => 'ro' ); | |||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 4 | has is_mobile => ( is => 'ro' ); | |||||||||||||||||||||||||||||||||||||||||
| 19 | 4 | has is_valid => ( is => 'ro' ); | |||||||||||||||||||||||||||||||||||||||||
| 20 | 4 | has is_fixed_line => ( is => 'ro' ); | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 4 | has _original_number => ( is => 'ro' ); | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | 13 | ▶ | sub BUILDARGS { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILDARGS: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 13 | my ($class, $number) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 13 | my ($areacode, $subscriber); | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | 13 | my %args = ( _original_number => $number ); | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 13 | ▶ | my $number_sane = _sanitize_number($number) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | or return \%args; | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | 12 | $number_sane =~ s{ \( ([0-9]+) \) }{}x; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | 12 | ▶ | if ( $areacode = $1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 36 | 3 | $areacode =~ s/^0//; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | $subscriber = $number_sane; | |||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||
| 39 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 9 | $number_sane =~ s{^0}{}; | |||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | 9 | $areacode = substr $number_sane, 0, 2; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 9 | $subscriber = substr $number_sane, 2; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 12 | ▶ | my $areaname = code2name($areacode) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | or return \%args; | ||||||||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 10 | my $is_mobile = _validate_mobile( $areacode, $subscriber ); | |||||||||||||||||||||||||||||||||||||||||
| 50 | 10 | ▶ | my $is_fixed_line = $is_mobile ? 0 : _validate_fixed_line( $subscriber ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 10 | ▶ | my $is_valid = $is_mobile || $is_fixed_line; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$is_mobile || $is_fixed_line
MC/DC0%0/2
$is_mobile || $is_fixed_line
$is_mobile
$is_fixed_line
Truth table66%2/3
A || B
A= $is_mobileB= $is_fixed_line
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||||
| 53 | 10 | ▶ | %args = (%args, | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | areacode => $areacode, | ||||||||||||||||||||||||||||||||||||||||||
| 55 | areaname => $areaname, | ||||||||||||||||||||||||||||||||||||||||||
| 56 | subscriber => $subscriber, | ||||||||||||||||||||||||||||||||||||||||||
| 57 | is_mobile => $is_mobile, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | is_fixed_line => $is_fixed_line, | ||||||||||||||||||||||||||||||||||||||||||
| 59 | is_valid => $is_valid, | ||||||||||||||||||||||||||||||||||||||||||
| 60 | ) if $is_valid; | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 10 | return \%args; | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | 13 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 13 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | # Breaks compat with Number::Phone | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 13 | ▶ | $self->is_valid | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | or confess "Not a valid Brazilian phone number: " . $self->_original_number; | ||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | 13 | ▶ | sub _sanitize_number { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_sanitize_number: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 13 | my $number = shift; | |||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | 13 | ▶ | return '' unless $number; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 13 | my $number_sane = $number; | |||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | # remove stuff we don't need | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 13 | $number_sane =~ s{[\- \s]}{}gx; | |||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | # strip country code | ||||||||||||||||||||||||||||||||||||||||||
| 84 | 13 | $number_sane =~ s{^\+55}{}gx; | |||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 13 | ▶ | return '' if $number_sane =~ m|\+|; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | 12 | return $number_sane; | |||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 10 | ▶ | sub _validate_mobile { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_mobile: called CC=7 SCAR=22.0
| |||||||||||||||||||||||||||||||||||||||||||
| 92 | 10 | my ($code, $number) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 10 | my $digits = mobile_phone_digits_by_area($code); | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 10 | my $f = substr $number, 0, 1; | |||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 10 | ▶ | if ($digits == 9 && $f ne '9') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$digits == 9 and $f ne 9
MC/DC50%1/2
$digits == 9 and $f ne 9
$digits == 9
$f ne 9
Truth table66%2/3
A and B
A= $digits == 9B= $f ne 9
| |||||||||||||||||||||||||||||||||||||||||||
| 99 | 9 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | ▶ | if ($f ne '6' && $f ne '8' && $f ne '9') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
$f ne 6 and $f ne 8 and $f ne 9
$f ne 6 and $f ne 8
MC/DC0%0/3
$f ne 6 and $f ne 8 and $f ne 9
$f ne 6
$f ne 8
$f ne 9
Truth table25%1/4
A and B and C
A= $f ne 6B= $f ne 8C= $f ne 9
| |||||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | return $number =~ m|^[0-9]{$digits}$| ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | 9 | ▶ | sub _validate_fixed_line { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_validate_fixed_line: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 9 | my ($number) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 9 | ▶ | return $number =~ m|^[2-5][0-9]{7}$| ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | # TODO: 0800, 0300 ? | ||||||||||||||||||||||||||||||||||||||||||
| 116 | 0 | ▶ | sub is_tollfree { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_tollfree: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | # TODO: 190, etc | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 0 | ▶ | sub is_network_service { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_network_service: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | # XXX: all of these return undef, because I have no idea how to implement them, | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # or even if it is possible at all in Brazil. | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 0 | ▶ | sub is_allocated { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_allocated: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub is_in_use { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_in_use: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | sub is_geographic { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_geographic: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub is_pager { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_pager: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub is_ipphone { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_ipphone: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub is_isdn { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_isdn: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub is_specialrate { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_specialrate: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | sub is_adult { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_adult: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub is_international { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_international: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub is_personal { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_personal: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub is_corporate { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_corporate: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub is_government { } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_government: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||
| 136 | 4 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | __END__ | ||||||||||||||||||||||||||||||||||||||||||