Skip to content

Commit b5d1cc2

Browse files
committed
add new methods to PhoneHelper
1 parent f28a9f8 commit b5d1cc2

File tree

1 file changed

+145
-0
lines changed

1 file changed

+145
-0
lines changed

src/PhoneHelper.php

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,130 @@
1919

2020
class PhoneHelper
2121
{
22+
private static $countryCodes = [
23+
'1' => 'США/Канада',
24+
'7' => 'Россия/Казахстан',
25+
'20' => 'Египет',
26+
'21' => 'Южная Африка',
27+
'22' => 'Марокко',
28+
'23' => 'Алжир',
29+
'24' => 'Замбия',
30+
'25' => 'Зимбабве',
31+
'26' => 'Мозамбик',
32+
'27' => 'Южная Африка',
33+
'28' => 'Эритрея',
34+
'29' => 'Эфиопия',
35+
'30' => 'Греция',
36+
'31' => 'Нидерланды',
37+
'32' => 'Бельгия',
38+
'33' => 'Франция',
39+
'34' => 'Испания',
40+
'36' => 'Венгрия',
41+
'39' => 'Италия',
42+
'40' => 'Румыния',
43+
'41' => 'Швейцария',
44+
'43' => 'Австрия',
45+
'44' => 'Великобритания',
46+
'45' => 'Дания',
47+
'46' => 'Швеция',
48+
'47' => 'Норвегия',
49+
'48' => 'Польша',
50+
'49' => 'Германия',
51+
'50' => 'Бангладеш',
52+
'51' => 'Перу',
53+
'52' => 'Мексика',
54+
'53' => 'Куба',
55+
'54' => 'Аргентина',
56+
'55' => 'Бразилия',
57+
'56' => 'Чили',
58+
'60' => 'Малайзия',
59+
'61' => 'Австралия',
60+
'62' => 'Индонезия',
61+
'63' => 'Филиппины',
62+
'64' => 'Новая Зеландия',
63+
'65' => 'Сингапур',
64+
'66' => 'Таиланд',
65+
'81' => 'Япония',
66+
'82' => 'Южная Корея',
67+
'84' => 'Вьетнам',
68+
'86' => 'Китай',
69+
'90' => 'Турция',
70+
'91' => 'Индия',
71+
'92' => 'Пакистан',
72+
'93' => 'Афганистан',
73+
'94' => 'Шри-Ланка',
74+
'95' => 'Мьянма',
75+
'98' => 'Иран',
76+
'370' => 'Литва',
77+
'371' => 'Латвия',
78+
'372' => 'Эстония',
79+
'373' => 'Молдова',
80+
'374' => 'Армения',
81+
'375' => 'Беларусь',
82+
'376' => 'Андорра',
83+
'377' => 'Монако',
84+
'378' => 'Сан-Марино',
85+
'380' => 'Украина',
86+
'381' => 'Сербия',
87+
'382' => 'Черногория',
88+
'383' => 'Косово',
89+
'385' => 'Хорватия',
90+
'386' => 'Словения',
91+
'387' => 'Босния и Герцеговина',
92+
'389' => 'Северная Македония',
93+
'420' => 'Чехия',
94+
'421' => 'Словакия',
95+
'423' => 'Лихтенштейн',
96+
'994' => 'Азербайджан',
97+
'995' => 'Грузия',
98+
'996' => 'Киргизия',
99+
'998' => 'Узбекистан',
100+
];
101+
102+
/**
103+
* Formats an international mobile phone number by removing non-numeric characters,
104+
* ensuring it starts with a plus sign, and determining the country based on the phone number.
105+
*
106+
* Форматируем международный мобильный номер телефона, удаляя нечисловые символы,
107+
* добавляя знак плюс в начале и определяя страну по номеру телефона.
108+
*
109+
* @param string|null $phoneNumber the original phone number
110+
*
111+
* @return array|null an array with the formatted phone number and country code, or null if the number is invalid
112+
*/
113+
public static function formatInternationalMobilePhoneNumber(?string $phoneNumber): ?array
114+
{
115+
if (empty($phoneNumber)) {
116+
return null;
117+
}
118+
119+
// Remove everything except numbers and plus sign
120+
$phoneNumber = \preg_replace('/[^\d+]/', '', $phoneNumber);
121+
122+
// If the number begins with 8 and has a length of 11 characters, replace 8 by +7 (for Russian numbers)
123+
if ($phoneNumber[0] === '8' && \strlen($phoneNumber) === 11) {
124+
$phoneNumber = '+7' . \substr($phoneNumber, 1);
125+
}
126+
127+
// Add plus sign if not present
128+
if ($phoneNumber[0] !== '+') {
129+
$phoneNumber = '+' . $phoneNumber;
130+
}
131+
132+
// Determine the country code based on the phone number
133+
$countryCode = self::getCountryCodeFromPhoneNumber($phoneNumber);
134+
135+
if ($countryCode === null) {
136+
return null;
137+
}
138+
139+
return [
140+
'formattedNumber' => $phoneNumber,
141+
'countryCode' => $countryCode,
142+
'countryName' => self::$countryCodes[$countryCode],
143+
];
144+
}
145+
22146
/**
23147
* Formats a Russian mobile phone number by removing non-numeric characters and converting it starting with +7.
24148
*
@@ -156,4 +280,25 @@ public static function isPhoneExist(string $string, string $apiToken)
156280

157281
return $oResult;
158282
}
283+
284+
/**
285+
* Determines the country code from the phone number.
286+
*
287+
* Определяем код страны по номеру телефона.
288+
*
289+
* @param string $phoneNumber the formatted phone number
290+
*
291+
* @return string|null the country code, or null if the country code cannot be determined
292+
*/
293+
private static function getCountryCodeFromPhoneNumber(string $phoneNumber): ?string
294+
{
295+
// Extract the country code from the phone number
296+
foreach (self::$countryCodes as $code => $country) {
297+
if (\str_starts_with($phoneNumber, '+' . $code)) {
298+
return $code;
299+
}
300+
}
301+
302+
return null;
303+
}
159304
}

0 commit comments

Comments
 (0)