File Coverage

lib/Locale/Country/Extra.pm
Criterion Covered Total %
statement 72 72 100.0
branch 14 14 100.0
condition n/a
subroutine 19 19 100.0
pod 10 10 100.0
total 115 115 100.0


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