Initial commit
[number-phone-ro.git] / lib / Number / Phone / RO.pm
CommitLineData
5f6a17ef
MG
1package Number::Phone::RO;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Number::Phone/;
7use utf8;
8use re '/s';
9
10sub AREA_NAMES (); ## no critic (ProhibitSubroutinePrototypes)
11
12our $VERSION = '0.001';
13
14our %cache;
15
16sub _normalized {
17 my ($nr) = @_;
18 $nr =~ y/0-9+//cd;
19 $nr =~ s/^[+]40//;
20 $nr =~ s/^0//;
21 $nr
22}
23
24sub _analyze_number {
25 my ($nr) = @_;
26 my %info;
27
28 return { valid => 0 } unless length $nr == 9;
29 $info{valid} = 1;
30
31 $info{geographic} = $nr =~ /^[23][3-6]/ ? 1 : 0;
32 @info{qw/fixed_line mobile/} = (1, 0) if $nr =~ /^[23]/;
33 @info{qw/fixed_line mobile/} = (0, 1) if $nr =~ /^7/;
34 $info{tollfree} = $nr =~ /^800/ ? 1 : 0;
35 $info{specialrate} = $nr =~ /^90/ ? 1 : 0;
36 $info{adult} = 1 if $nr =~ /^906/;
37
38 my $arealen = $nr =~ /^[23]1/ ? 2 : 3;
39 $info{areacode} = substr $nr, 0, $arealen;
40 $info{subscriber} = substr $nr, $arealen;
41
42 \%info
43}
44
45sub _info { $cache{${$_[0]}} }
46
47sub new {
48 my ($class, $nr) = @_;
49 $nr = _normalized $nr;
50 $cache{$nr} = _analyze_number $nr;
51 my $self = bless \$nr, $class;
52 $self->is_valid ? $self : undef
53}
54
55sub is_valid { shift->_info->{valid} }
56sub is_geographic { shift->_info->{geographic} }
57sub is_fixed_line { shift->_info->{fixed_line} }
58sub is_mobile { shift->_info->{mobile} }
59sub is_tollfree { shift->_info->{tollfree} }
60sub is_specialrate { shift->_info->{specialrate} }
61sub is_adult { shift->_info->{adult} }
62
63sub country_code { 40 }
64sub regulator { 'ANCOM, http://ancom.org.ro'}
65
66sub areacode { shift->_info->{areacode} }
67sub areaname { $_[0]->is_geographic ? AREA_NAMES->{substr $_[0]->areacode, 1} : undef }
68sub subscriber { shift->_info->{subscriber} }
69
70sub format { ## no critic (ProhibitBuiltinHomonyms)
71 my ($self) = @_;
72 join ' ',
73 '+40',
74 $self->areacode,
75 (substr $self->subscriber, 0, 3),
76 (substr $self->subscriber, 3);
77}
78
79sub intra_country_dial_to { "0${$_[0]}" }
80
81use constant AREA_NAMES => {
82 1 => 'București',
83 30 => 'Suceava',
84 31 => 'Botoșani',
85 32 => 'Iași',
86 33 => 'Neamț',
87 34 => 'Bacău',
88 35 => 'Vaslui',
89 36 => 'Galați',
90 37 => 'Vrancea',
91 38 => 'Buzău',
92 39 => 'Brăila',
93 40 => 'Tulcea',
94 41 => 'Constanța',
95 42 => 'Călărași',
96 43 => 'Ialomița',
97 44 => 'Prahova',
98 45 => 'Dâmbovița',
99 46 => 'Giurgiu',
100 47 => 'Teleorman',
101 48 => 'Argeș',
102 49 => 'Olt',
103 50 => 'Vâlcea',
104 51 => 'Dolj',
105 52 => 'Mehedinți',
106 53 => 'Gorj',
107 54 => 'Hunedoara',
108 55 => 'Caraș-Severin',
109 56 => 'Timiș',
110 57 => 'Arad',
111 58 => 'Alba',
112 59 => 'Bihor',
113 60 => 'Sălaj',
114 61 => 'Satu Mare',
115 62 => 'Maramureș',
116 63 => 'Bistrița-Năsăud',
117 64 => 'Cluj',
118 65 => 'Mureș',
119 66 => 'Harghita',
120 67 => 'Covasna',
121 68 => 'Brașov',
122 69 => 'Sibiu',
123};
124
1251;
126__END__
127
128=encoding utf-8
129
130=head1 NAME
131
132Number::Phone::RO - Phone number information for Romania (+40)
133
134=head1 SYNOPSIS
135
136 use Number::Phone::RO;
137 my $nr = Number::Phone::RO->new('+40250123456');
138 say $nr->is_geographic; # 1
139 say $nr->is_fixed_line; # 1
140 say $nr->is_mobile; # 0
141 say $nr->is_tollfree; # 0
142 say $nr->is_specialrate; # 0
143 say $nr->areacode; # 250
144 say $nr->areaname; # Vâlcea
145 say $nr->subscriber; # 123456
146 say $nr->format; # +40 250 123 456
147
148=head1 DESCRIPTION
149
150This module is a work in progress. See the L<TODO> section below.
151
152See the L<Number::Phone> documentation for usage information. The
153following methods from L<Number::Phone> are overridden:
154
155=over
156
157=item B<is_geographic>
158
159=item B<is_fixed_line>
160
161=item B<is_mobile>
162
163=item B<is_tollfree>
164
165=item B<is_specialrate>
166
167=item B<country_code>
168
169Always returns 40.
170
171=item B<regulator>
172
173Returns the name and URL of the regulator, ANCOM.
174
175=item B<areacode>
176
177=item B<areaname>
178
179=item B<subscriber>
180
181=item B<format>
182
183=back
184
185=head1 TODO
186
187Only long (10 digits) numbers are supported.
188
189Should query L<http://portabilitate.ro/> to implement B<operator> and B<operator_ported>.
190
191=head1 AUTHOR
192
193Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
194
195=head1 COPYRIGHT AND LICENSE
196
197Copyright (C) 2015 by Marius Gavrilescu
198
199This library is free software; you can redistribute it and/or modify
200it under the same terms as Perl itself, either Perl version 5.20.2 or,
201at your option, any later version of Perl 5 you may have available.
202
203
204=cut
This page took 0.020127 seconds and 4 git commands to generate.