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   83172 use strict;
  2         14  
  2         58  
3 2     2   10 use warnings;
  2         2  
  2         61  
4 2     2   535 use utf8;
  2         15  
  2         11  
5             our $VERSION = '1.06';
6              
7 2     2   905 use Locale::Country qw();
  2         87131  
  2         94  
8 2     2   960 use Locale::Country::Multilingual {use_io_layer => 1};
  2         8004  
  2         11  
9              
10             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             sub new {
39 1     1 1 97 my $class = shift;
40              
41 1         3 my $self = {};
42 1         3 bless $self, $class;
43              
44 1         6 $self->{_country_codes} = $self->_build_country_codes;
45 1         6 $self->{_idd_codes} = $self->_build_idd_codes;
46              
47 1         7 return $self;
48             }
49              
50             sub country_from_code {
51 7     7 1 1202 my ($self, $code) = @_;
52 7         11 $code = lc $code;
53              
54             # we need gb
55 7 100       21 $code = 'gb' if $code eq 'uk';
56              
57 7         15 return $self->_country_codes->{$code};
58             }
59              
60             sub code_from_country {
61 31     31 1 5475 my ($self, $country) = @_;
62              
63 31         168 $country =~ s/^\s+|\s+$//g;
64 31         60 $country = lc $country;
65              
66 31 100       172 return $COUNTRY_MAP{$country} if $COUNTRY_MAP{$country};
67              
68 7         30 my $code = Locale::Country::Multilingual->new()->country2code($country);
69              
70 7 100       309 return $code ? lc $code : undef;
71              
72             }
73              
74             sub idd_from_code {
75 10     10 1 3051 my ($self, $code) = @_;
76 10         19 $code = lc $code;
77              
78             # we need gb
79 10 100       27 $code = 'gb' if $code eq 'uk';
80              
81 10         24 return $self->_idd_codes->{$code};
82             }
83              
84             sub get_valid_phone {
85 7     7 1 13 my ($self, $number) = @_;
86              
87 7 100       39 return '' if $number =~ /^([0-9])\1{5}/;
88              
89 6         28 $number =~ s/\D//g; # Remove non-digits
90 6         18 $number =~ s/^00//; # Remove the leading '00'.
91 6         21 return $number;
92             }
93              
94             sub code_from_phone {
95 4     4 1 2604 my ($self, $number) = @_;
96              
97 4 100       11 if (my $first = $self->codes_from_phone($number)) {
98 3         6 return lc ${$first}[0];
  3         24  
99             }
100              
101 1         7 return '';
102             }
103              
104             sub codes_from_phone {
105 7     7 1 2665 my ($self, $number) = @_;
106              
107 7 100       17 if (my $phone = $self->get_valid_phone($number)) {
108 6         9 my %codes = %{$self->_idd_codes};
  6         14  
109 6         163 return [sort grep { $phone =~ /^$codes{$_}/ } keys %codes];
  1464         10542  
110             }
111              
112 1         4 return '';
113             }
114              
115             sub all_country_names {
116 1     1 1 2968 my $self = shift;
117 1         2 return values %{$self->_country_codes};
  1         5  
118             }
119              
120             sub all_country_codes {
121 1     1 1 2429 my $self = shift;
122 1         2 return keys %{$self->_country_codes};
  1         3  
123             }
124              
125             sub localized_code2country {
126 1     1 1 2457 my ($self, $country_code, $lang) = @_;
127              
128 1         8 my $lcm = Locale::Country::Multilingual->new();
129 1         18 return $lcm->code2country($country_code, $lang);
130             }
131              
132             sub _country_codes {
133 9     9   51 my ($self) = @_;
134 9         219 return $self->{_country_codes};
135             }
136              
137             sub _build_country_codes {
138 1     1   10 my $lcm = Locale::Country::Multilingual->new();
139 1         18 my @codes = $lcm->all_country_codes();
140              
141 1         32176 my $country_hash = {};
142 1         4 foreach my $code (@codes) {
143 249         6839 $country_hash->{lc($code)} = $lcm->code2country($code);
144             }
145              
146 1         69 return $country_hash;
147             }
148              
149             sub _idd_codes {
150 17     17   2549 my ($self) = @_;
151 17         600 return $self->{_idd_codes};
152             }
153              
154             sub _build_idd_codes {
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     1   132 "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             1;
406              
407             __END__