1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31:
32:
33: 34: 35: 36: 37:
38: class Eabi_Postoffice_Helper_Countrycode {
39:
40: protected $_countryCodes = array(
41: 'IL' => '972',
42: 'AF' => '93',
43: 'AL' => '355',
44: 'DZ' => '213',
45: 'AS' => '1684',
46: 'AD' => '376',
47: 'AO' => '244',
48: 'AI' => '1264',
49: 'AG' => '1268',
50: 'AR' => '54',
51: 'AM' => '374',
52: 'AW' => '297',
53: 'AU' => '61',
54: 'AT' => '43',
55: 'AZ' => '994',
56: 'BS' => '1242',
57: 'BH' => '973',
58: 'BD' => '880',
59: 'BB' => '1246',
60: 'BY' => '375',
61: 'BE' => '32',
62: 'BZ' => '501',
63: 'BJ' => '229',
64: 'BM' => '1441',
65: 'BT' => '975',
66: 'BA' => '387',
67: 'BW' => '267',
68: 'BR' => '55',
69: 'IO' => '246',
70: 'BG' => '359',
71: 'BF' => '226',
72: 'BI' => '257',
73: 'KH' => '855',
74: 'CM' => '237',
75: 'CA' => '1',
76: 'CV' => '238',
77: 'KY' => '345',
78: 'CF' => '236',
79: 'TD' => '235',
80: 'CL' => '56',
81: 'CN' => '86',
82: 'CX' => '61',
83: 'CO' => '57',
84: 'KM' => '269',
85: 'CG' => '242',
86: 'CK' => '682',
87: 'CR' => '506',
88: 'HR' => '385',
89: 'CU' => '53',
90: 'CY' => '537',
91: 'CZ' => '420',
92: 'DK' => '45',
93: 'DJ' => '253',
94: 'DM' => '1767',
95: 'DO' => '1849',
96: 'EC' => '593',
97: 'EG' => '20',
98: 'SV' => '503',
99: 'GQ' => '240',
100: 'ER' => '291',
101: 'EE' => '372',
102: 'ET' => '251',
103: 'FO' => '298',
104: 'FJ' => '679',
105: 'FI' => '358',
106: 'FR' => '33',
107: 'GF' => '594',
108: 'PF' => '689',
109: 'GA' => '241',
110: 'GM' => '220',
111: 'GE' => '995',
112: 'DE' => '49',
113: 'GH' => '233',
114: 'GI' => '350',
115: 'GR' => '30',
116: 'GL' => '299',
117: 'GD' => '1473',
118: 'GP' => '590',
119: 'GU' => '1671',
120: 'GT' => '502',
121: 'GN' => '224',
122: 'GW' => '245',
123: 'GY' => '595',
124: 'HT' => '509',
125: 'HN' => '504',
126: 'HU' => '36',
127: 'IS' => '354',
128: 'IN' => '91',
129: 'ID' => '62',
130: 'IQ' => '964',
131: 'IE' => '353',
132: 'IT' => '39',
133: 'JM' => '1876',
134: 'JP' => '81',
135: 'JO' => '962',
136: 'KZ' => '77',
137: 'KE' => '254',
138: 'KI' => '686',
139: 'KW' => '965',
140: 'KG' => '996',
141: 'LV' => '371',
142: 'LB' => '961',
143: 'LS' => '266',
144: 'LR' => '231',
145: 'LI' => '423',
146: 'LT' => '370',
147: 'LU' => '352',
148: 'MG' => '261',
149: 'MW' => '265',
150: 'MY' => '60',
151: 'MV' => '960',
152: 'ML' => '223',
153: 'MT' => '356',
154: 'MH' => '692',
155: 'MQ' => '596',
156: 'MR' => '222',
157: 'MU' => '230',
158: 'YT' => '262',
159: 'MX' => '52',
160: 'MC' => '377',
161: 'MN' => '976',
162: 'ME' => '382',
163: 'MS' => '1664',
164: 'MA' => '212',
165: 'MM' => '95',
166: 'NA' => '264',
167: 'NR' => '674',
168: 'NP' => '977',
169: 'NL' => '31',
170: 'AN' => '599',
171: 'NC' => '687',
172: 'NZ' => '64',
173: 'NI' => '505',
174: 'NE' => '227',
175: 'NG' => '234',
176: 'NU' => '683',
177: 'NF' => '672',
178: 'MP' => '1670',
179: 'NO' => '47',
180: 'OM' => '968',
181: 'PK' => '92',
182: 'PW' => '680',
183: 'PA' => '507',
184: 'PG' => '675',
185: 'PY' => '595',
186: 'PE' => '51',
187: 'PH' => '63',
188: 'PL' => '48',
189: 'PT' => '351',
190: 'PR' => '1939',
191: 'QA' => '974',
192: 'RO' => '40',
193: 'RW' => '250',
194: 'WS' => '685',
195: 'SM' => '378',
196: 'SA' => '966',
197: 'SN' => '221',
198: 'RS' => '381',
199: 'SC' => '248',
200: 'SL' => '232',
201: 'SG' => '65',
202: 'SK' => '421',
203: 'SI' => '386',
204: 'SB' => '677',
205: 'ZA' => '27',
206: 'GS' => '500',
207: 'ES' => '34',
208: 'LK' => '94',
209: 'SD' => '249',
210: 'SR' => '597',
211: 'SZ' => '268',
212: 'SE' => '46',
213: 'CH' => '41',
214: 'TJ' => '992',
215: 'TH' => '66',
216: 'TG' => '228',
217: 'TK' => '690',
218: 'TO' => '676',
219: 'TT' => '1868',
220: 'TN' => '216',
221: 'TR' => '90',
222: 'TM' => '993',
223: 'TC' => '1649',
224: 'TV' => '688',
225: 'UG' => '256',
226: 'UA' => '380',
227: 'AE' => '971',
228: 'GB' => '44',
229: 'US' => '1',
230: 'UY' => '598',
231: 'UZ' => '998',
232: 'VU' => '678',
233: 'WF' => '681',
234: 'YE' => '967',
235: 'ZM' => '260',
236: 'ZW' => '263',
237: 'BO' => '591',
238: 'BN' => '673',
239: 'CC' => '61',
240: 'CD' => '243',
241: 'CI' => '225',
242: 'FK' => '500',
243: 'GG' => '44',
244: 'VA' => '379',
245: 'HK' => '852',
246: 'IR' => '98',
247: 'IM' => '44',
248: 'JE' => '44',
249: 'KP' => '850',
250: 'KR' => '82',
251: 'LA' => '856',
252: 'LY' => '218',
253: 'MO' => '853',
254: 'MK' => '389',
255: 'FM' => '691',
256: 'MD' => '373',
257: 'MZ' => '258',
258: 'PS' => '970',
259: 'PN' => '872',
260: 'RE' => '262',
261: 'RU' => '7',
262: 'BL' => '590',
263: 'SH' => '290',
264: 'KN' => '1869',
265: 'LC' => '1758',
266: 'MF' => '590',
267: 'PM' => '508',
268: 'VC' => '1784',
269: 'ST' => '239',
270: 'SO' => '252',
271: 'SJ' => '47',
272: 'SY' => '963',
273: 'TW' => '886',
274: 'TZ' => '255',
275: 'TL' => '670',
276: 'VE' => '58',
277: 'VN' => '84',
278: 'VG' => '1284',
279: 'VI' => '1340',
280: );
281:
282: 283: 284: 285:
286: public function getCountryCodes() {
287: return $this->_countryCodes;
288: }
289:
290:
291: 292: 293: 294: 295:
296: public function getCountryCode($countryId) {
297: if (isset($this->_countryCodes[$countryId])) {
298: return $this->_countryCodes[$countryId];
299: }
300: return null;
301: }
302:
303:
304:
305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319:
320: public function separatePhoneNumberFromCountryCode($phonenumber, $countryId) {
321: $result = array(
322: 'dial_code' => '',
323: 'phone_number' => '',
324: );
325: $defaultDialCode = $this->getCountryCode($countryId);
326:
327:
328: $phonenumber = str_replace(' ', '', $phonenumber);
329:
330:
331:
332:
333:
334:
335: $containsCountryCode = strpos($phonenumber, $defaultDialCode) === 0
336: || strpos($phonenumber, '+') === 0
337: || strpos($phonenumber, '00') === 0
338: || strlen($phonenumber) > 10;
339:
340:
341:
342:
343: if (!$containsCountryCode) {
344: $result['dial_code'] = '+'.$defaultDialCode;
345: if (strpos($phonenumber, '0') === 0) {
346: $result['phone_number'] = substr($phonenumber, 1);
347: } else {
348: $result['phone_number'] = $phonenumber;
349: }
350: } else {
351:
352:
353:
354: $phonenumber = ltrim($phonenumber, '+0');
355: $dialCode = $this->_getCountryCodeFromPhonenumber($phonenumber, $defaultDialCode);
356: $result['dial_code'] = '+'.$dialCode;
357: $result['phone_number'] = substr($phonenumber, strlen($dialCode));
358: }
359:
360:
361: return $result;
362: }
363:
364:
365: 366: 367: 368: 369: 370: 371: 372:
373: protected function _getCountryCodeFromPhonenumber($phonenumber, $defaultCode) {
374: $matchingCountryCode = '';
375: foreach ($this->getCountryCodes() as $countryIso => $dialCode) {
376: if (strpos($phonenumber, $dialCode) === 0) {
377: if (strlen($dialCode) > $matchingCountryCode) {
378: $matchingCountryCode = $dialCode;
379: }
380: }
381: }
382:
383: if ($matchingCountryCode === '') {
384: return $defaultCode;
385: }
386: return $matchingCountryCode;
387: }
388:
389: }
390: