|
20 | 20 | class PhoneHelper
|
21 | 21 | {
|
22 | 22 | private static $countryCodes = [
|
| 23 | + '7' => [ |
| 24 | + 'Российская Федерация' => ['9'], |
| 25 | + 'Казахстан' => ['7'], |
| 26 | + ], |
23 | 27 | '1' => 'США/Канада',
|
24 |
| - '7' => 'Россия/Казахстан', |
25 | 28 | '20' => 'Египет',
|
26 | 29 | '21' => 'Южная Африка',
|
27 | 30 | '22' => 'Марокко',
|
@@ -139,7 +142,7 @@ public static function formatInternationalMobilePhoneNumber(?string $phoneNumber
|
139 | 142 | return [
|
140 | 143 | 'formattedNumber' => $phoneNumber,
|
141 | 144 | 'countryCode' => $countryCode,
|
142 |
| - 'countryName' => self::$countryCodes[$countryCode], |
| 145 | + 'countryName' => self::getCountryNameFromPhoneNumber($phoneNumber, $countryCode), |
143 | 146 | ];
|
144 | 147 | }
|
145 | 148 |
|
@@ -294,9 +297,48 @@ private static function getCountryCodeFromPhoneNumber(string $phoneNumber): ?str
|
294 | 297 | {
|
295 | 298 | // Extract the country code from the phone number
|
296 | 299 | foreach (self::$countryCodes as $code => $country) {
|
297 |
| - if (\str_starts_with($phoneNumber, '+' . $code)) { |
298 |
| - return $code; |
| 300 | + if (\is_array($country)) { |
| 301 | + foreach ($country as $subCode => $prefixes) { |
| 302 | + foreach ($prefixes as $prefix) { |
| 303 | + if (\str_starts_with($phoneNumber, '+' . $code . $prefix)) { |
| 304 | + return $code; |
| 305 | + } |
| 306 | + } |
| 307 | + } |
| 308 | + } else { |
| 309 | + if (\str_starts_with($phoneNumber, '+' . $code)) { |
| 310 | + return $code; |
| 311 | + } |
| 312 | + } |
| 313 | + } |
| 314 | + |
| 315 | + return null; |
| 316 | + } |
| 317 | + |
| 318 | + /** |
| 319 | + * Determines the name of the country by phone number and code of the country. |
| 320 | + * |
| 321 | + * Определяем название страны по номеру телефона и коду страны. |
| 322 | + * |
| 323 | + * @param string $phoneNumber the formatted phone number |
| 324 | + * @param string $countryCode the country code |
| 325 | + * |
| 326 | + * @return string|null the country code, or null if the country code cannot be determined |
| 327 | + */ |
| 328 | + private static function getCountryNameFromPhoneNumber(string $phoneNumber, string $countryCode): ?string |
| 329 | + { |
| 330 | + $country = self::$countryCodes[$countryCode]; |
| 331 | + |
| 332 | + if (\is_array($country)) { |
| 333 | + foreach ($country as $name => $prefixes) { |
| 334 | + foreach ($prefixes as $prefix) { |
| 335 | + if (\str_starts_with($phoneNumber, '+' . $countryCode . $prefix)) { |
| 336 | + return $name; |
| 337 | + } |
| 338 | + } |
299 | 339 | }
|
| 340 | + } else { |
| 341 | + return $country; |
300 | 342 | }
|
301 | 343 |
|
302 | 344 | return null;
|
|
0 commit comments