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