lib/Locale/Country/Extra.pm

filter: Statementstmt 100% 75 / 75 Branchbran 100% 14 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 19 / 19 Podpod 100% 10 / 10 totaltotal 100% 118 / 118 CC 9 SCAR 5.1
CC
9
Cov
100%
code_from_country
3.0
country_from_code
2.0
idd_from_code
2.0
CRAP 1.7
↑ Summary
1 package Locale::Country::Extra;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 our $VERSION = '1.06';
6  
7 3 ▶use Locale::Country qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Locale::Country::Multilingual {use_io_layer => 1};
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our %COUNTRY_MAP = (
11 "brunei darussalam" => "bn",
12 "cocos islands" => "cc",
13 "congo" => "cg",
14 "côte d'ivoire" => "ci",
15 "côte d’ivoire" => "ci",
16 "curacao" => "cw",
17 "eswatini" => "sz",
18 "heard island and mcdonald islands" => "hm",
19 "hong kong s.a.r." => "hk",
20 "korea" => "kr",
21 "macao s.a.r." => "mo",
22 "myanmar" => "mm",
23 "islamic republic of pakistan" => "pk",
24 "palestinian authority" => "ps",
25 "pitcairn" => "pn",
26 "r\x{e9}union" => "re",
27 "saint vincent and the grenadines" => "vc",
28 "south georgia" => "gs",
29 "south georgia & south sandwich" => "gs",
30 "syrian arab republic" => "sy",
31 "taiwan" => "tw",
32 "trinidad & tobago" => "tt",
33 "u.a.e." => "ae",
34 "vatican city state" => "va",
35 "virgin islands" => "vg"
36 );
37  
38 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
39 1 my $class = shift;
40  
41 1 my $self = {};
42 1 bless $self, $class;
43  
44 1 $self->{_country_codes} = $self->_build_country_codes;
45 1 $self->{_idd_codes} = $self->_build_idd_codes;
46  
47 1 return $self;
48 }
49  
50 7 ▶sub country_from_code {
Subroutine country_from_code: called CC=2 SCAR=6.9
Pod documented
51 7 my ($self, $code) = @_;
52 7 $code = lc $code;
53  
54 # we need gb
55 7 ▶ $code = 'gb' if $code eq 'uk';
Branch: 1 branch
BranchTrueFalseTotal
if $code eq "uk" 1 6 7
56  
57 7 return $self->_country_codes->{$code};
58 }
59  
60 31 ▶sub code_from_country {
Subroutine code_from_country: called CC=3 SCAR=11.0
Pod documented
61 31 my ($self, $country) = @_;
62  
63 31 $country =~ s/^\s+|\s+$//g;
64 31 $country = lc $country;
65  
66 31 ▶ return $COUNTRY_MAP{$country} if $COUNTRY_MAP{$country};
Branch: 1 branch
BranchTrueFalseTotal
if $Locale::Country::Extra::COUNTRY_MAP{$country} 24 7 31
67  
68 7 my $code = Locale::Country::Multilingual->new()->country2code($country);
69  
70 7 ▶ return $code ? lc $code : undef;
Branch: 1 branch
BranchTrueFalseTotal
$code ? : 6 1 7
71  
72 }
73  
74 10 ▶sub idd_from_code {
Subroutine idd_from_code: called CC=2 SCAR=6.9
Pod documented
75 10 my ($self, $code) = @_;
76 10 $code = lc $code;
77  
78 # we need gb
79 10 ▶ $code = 'gb' if $code eq 'uk';
Branch: 1 branch
BranchTrueFalseTotal
if $code eq "uk" 1 9 10
80  
81 10 return $self->_idd_codes->{$code};
82 }
83  
84 7 ▶sub get_valid_phone {
Subroutine get_valid_phone: called CC=2 SCAR=6.9
Pod documented
85 7 my ($self, $number) = @_;
86  
87 7 ▶ return '' if $number =~ /^([0-9])\1{5}/;
Branch: 1 branch
BranchTrueFalseTotal
if $number =~ /^([0-9])\1{5}/ 1 6 7
88  
89 6 $number =~ s/\D//g; # Remove non-digits
90 6 $number =~ s/^00//; # Remove the leading '00'.
91 6 return $number;
92 }
93  
94 4 ▶sub code_from_phone {
Subroutine code_from_phone: called CC=2 SCAR=6.9
Pod documented
95 4 my ($self, $number) = @_;
96  
97 4 ▶ if (my $first = $self->codes_from_phone($number)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $first = $self->codes_from_phone($number)) 3 1 4
98 3 return lc ${$first}[0];
99 }
100  
101 1 return '';
102 }
103  
104 7 ▶sub codes_from_phone {
Subroutine codes_from_phone: called CC=2 SCAR=6.9
Pod documented
105 7 my ($self, $number) = @_;
106  
107 7 ▶ if (my $phone = $self->get_valid_phone($number)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $phone = $self->get_valid_phone($number)) 6 1 7
108 6 my %codes = %{$self->_idd_codes};
109 6 return [sort grep { $phone =~ /^$codes{$_}/ } keys %codes];
110 }
111  
112 1 return '';
113 }
114  
115 1 ▶sub all_country_names {
Subroutine all_country_names: called CC=1 SCAR=0.0
Pod documented
116 1 my $self = shift;
117 1 return values %{$self->_country_codes};
118 }
119  
120 1 ▶sub all_country_codes {
Subroutine all_country_codes: called CC=1 SCAR=0.0
Pod documented
121 1 my $self = shift;
122 1 return keys %{$self->_country_codes};
123 }
124  
125 1 ▶sub localized_code2country {
Subroutine localized_code2country: called CC=1 SCAR=0.0
Pod documented
126 1 my ($self, $country_code, $lang) = @_;
127  
128 1 my $lcm = Locale::Country::Multilingual->new();
129 1 return $lcm->code2country($country_code, $lang);
130 }
131  
132 9 ▶sub _country_codes {
Subroutine _country_codes: called CC=1 SCAR=0.0
133 9 my ($self) = @_;
134 9 return $self->{_country_codes};
135 }
136  
137 1 ▶sub _build_country_codes {
Subroutine _build_country_codes: called CC=2 SCAR=6.9
138 1 my $lcm = Locale::Country::Multilingual->new();
139 1 my @codes = $lcm->all_country_codes();
140  
141 1 my $country_hash = {};
142 1 foreach my $code (@codes) {
143 249 $country_hash->{lc($code)} = $lcm->code2country($code);
144 }
145  
146 1 return $country_hash;
147 }
148  
149 17 ▶sub _idd_codes {
Subroutine _idd_codes: called CC=1 SCAR=0.0
150 17 my ($self) = @_;
151 17 return $self->{_idd_codes};
152 }
153  
154 1 ▶sub _build_idd_codes {
Subroutine _build_idd_codes: called CC=1 SCAR=0.0
155 # List is order by zones: https://en.wikipedia.org/wiki/List_of_country_calling_codes#Ordered_by_code
156 # TODO: Hardcoding this is not a good idea. IDD's change from time to time.
157 return {
158 1 "us" => 1,
159 "bs" => 1242,
160 "bb" => 1246,
161 "ai" => 1264,
162 "ag" => 1268,
163 "vg" => 1284,
164 "vi" => 1340,
165 "ky" => 1345,
166 "bm" => 1441,
167 "gd" => 1473,
168 "tc" => 1649,
169 "ms" => 1664,
170 "mp" => 1670,
171 "lc" => 1758,
172 "vc" => 1784,
173 "pr" => 1787,
174 "dm" => 1767,
175 "do" => 1809,
176 "tt" => 1868,
177 "kn" => 1869,
178 "jm" => 1876,
179 "eg" => 20,
180 "eh" => 21,
181 "ss" => 211,
182 "ma" => 212,
183 "dz" => 213,
184 "tn" => 216,
185 "ly" => 218,
186 "gm" => 220,
187 "sn" => 221,
188 "mr" => 222,
189 "ml" => 223,
190 "gn" => 224,
191 "ci" => 225,
192 "bf" => 226,
193 "ne" => 227,
194 "tg" => 228,
195 "bj" => 229,
196 "mu" => 230,
197 "lr" => 231,
198 "sl" => 232,
199 "gh" => 233,
200 "ng" => 234,
201 "td" => 235,
202 "cf" => 236,
203 "cm" => 237,
204 "cv" => 238,
205 "st" => 239,
206 "gq" => 240,
207 "ga" => 241,
208 "cg" => 242,
209 "cd" => 243,
210 "ao" => 244,
211 "gw" => 245,
212 "io" => 246,
213 "sc" => 248,
214 "sd" => 249,
215 "rw" => 250,
216 "et" => 251,
217 "so" => 252,
218 "dj" => 253,
219 "ke" => 254,
220 "tz" => 255,
221 "ug" => 256,
222 "bi" => 257,
223 "mz" => 258,
224 "zm" => 260,
225 "mg" => 261,
226 "re" => 262,
227 "zw" => 263,
228 "na" => 264,
229 "mw" => 265,
230 "ls" => 266,
231 "bw" => 267,
232 "sz" => 268,
233 "km" => 269,
234 "yt" => 262269,
235 "za" => 27,
236 "sh" => 290,
237 "er" => 291,
238 "aw" => 297,
239 "fo" => 298,
240 "gl" => 299,
241 "gr" => 30,
242 "nl" => 31,
243 "be" => 32,
244 "fr" => 33,
245 "es" => 34,
246 "gi" => 350,
247 "pt" => 351,
248 "lu" => 352,
249 "ie" => 353,
250 "is" => 354,
251 "al" => 355,
252 "mt" => 356,
253 "cy" => 357,
254 "fi" => 358,
255 "ax" => 35818,
256 "bg" => 359,
257 "hu" => 36,
258 "lt" => 370,
259 "lv" => 371,
260 "ee" => 372,
261 "md" => 373,
262 "am" => 374,
263 "by" => 375,
264 "ad" => 376,
265 "mc" => 377,
266 "sm" => 378,
267 "va" => 379,
268 "ua" => 380,
269 "rs" => 381,
270 "me" => 382,
271 "hr" => 385,
272 "si" => 386,
273 "ba" => 387,
274 "mk" => 389,
275 "it" => 39,
276 "ro" => 40,
277 "li" => 417,
278 "ch" => 41,
279 "cz" => 420,
280 "sk" => 421,
281 "at" => 43,
282 "gg" => 441481,
283 "je" => 441534,
284 "im" => 44,
285 "gb" => 44,
286 "dk" => 45,
287 "se" => 46,
288 "no" => 47,
289 "sj" => 4779,
290 "pl" => 48,
291 "de" => 49,
292 "fk" => 500,
293 "gs" => 500,
294 "bz" => 501,
295 "gt" => 502,
296 "sv" => 503,
297 "hn" => 504,
298 "ni" => 505,
299 "cr" => 506,
300 "pa" => 507,
301 "pm" => 508,
302 "ht" => 509,
303 "pe" => 51,
304 "mx" => 52,
305 "cu" => 53,
306 "ar" => 54,
307 "br" => 55,
308 "cl" => 56,
309 "co" => 57,
310 "ve" => 58,
311 "gp" => 590,
312 "mf" => 590,
313 "bl" => 590,
314 "bo" => 591,
315 "gy" => 592,
316 "ec" => 593,
317 "gf" => 594,
318 "py" => 595,
319 "mq" => 596,
320 "sr" => 597,
321 "uy" => 598,
322 "an" => 599,
323 "sx" => 1721,
324 "cw" => 5999,
325 "my" => 60,
326 "au" => 61,
327 "cx" => 618,
328 "cc" => 6189162,
329 "id" => 62,
330 "ph" => 63,
331 "nz" => 64,
332 "pn" => 649,
333 "sg" => 65,
334 "th" => 66,
335 "tl" => 670,
336 "gu" => 671,
337 "aq" => 672,
338 "nf" => 6723,
339 "bn" => 673,
340 "nr" => 674,
341 "pg" => 675,
342 "to" => 676,
343 "sb" => 677,
344 "vu" => 678,
345 "fj" => 679,
346 "pw" => 680,
347 "wf" => 681,
348 "ck" => 682,
349 "nu" => 683,
350 "as" => 684,
351 "ws" => 685,
352 "ki" => 686,
353 "nc" => 687,
354 "tv" => 688,
355 "pf" => 689,
356 "tk" => 690,
357 "fm" => 691,
358 "mh" => 692,
359 "ru" => 7,
360 "kz" => 7,
361 "jp" => 81,
362 "kr" => 82,
363 "vn" => 84,
364 "kp" => 850,
365 "hk" => 852,
366 "mo" => 853,
367 "kh" => 855,
368 "la" => 856,
369 "cn" => 86,
370 "bd" => 880,
371 "tw" => 886,
372 "tr" => 90,
373 "in" => 91,
374 "pk" => 92,
375 "af" => 93,
376 "lk" => 94,
377 "mm" => 95,
378 "mv" => 960,
379 "lb" => 961,
380 "jo" => 962,
381 "sy" => 963,
382 "iq" => 964,
383 "kw" => 965,
384 "sa" => 966,
385 "ye" => 967,
386 "om" => 968,
387 "ps" => 970,
388 "ae" => 971,
389 "il" => 972,
390 "bh" => 973,
391 "qa" => 974,
392 "bt" => 975,
393 "mn" => 976,
394 "np" => 977,
395 "ir" => 98,
396 "tj" => 992,
397 "tm" => 993,
398 "az" => 994,
399 "ge" => 995,
400 "kg" => 996,
401 "uz" => 998,
402 };
403 }
404  
405 3 1;
406  
407 __END__
↑ Summary