From 0740bd0425fdbff94a1c9a4fd5b50c769f1e050a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timoth=C3=A9e=20Jaussoin?= Date: Tue, 9 Dec 2025 15:31:18 +0000 Subject: [PATCH] Fix FLEXIAPI-369 Refresh the getCountryCodes() list and add a console script... --- .../Console/Commands/UpdatePhoneCountries.php | 33 ++ flexiapi/app/Helpers/Utils.php | 301 +++++++++--------- ...15_135258_create_phone_countries_table.php | 13 +- 3 files changed, 199 insertions(+), 148 deletions(-) create mode 100644 flexiapi/app/Console/Commands/UpdatePhoneCountries.php diff --git a/flexiapi/app/Console/Commands/UpdatePhoneCountries.php b/flexiapi/app/Console/Commands/UpdatePhoneCountries.php new file mode 100644 index 0000000..b98f2e0 --- /dev/null +++ b/flexiapi/app/Console/Commands/UpdatePhoneCountries.php @@ -0,0 +1,33 @@ +toArray() + ) as $code) { + if ($resolvedMetadata = $phoneNumberUtils->getMetadataForRegion($code)) { + $phoneCountry = new PhoneCountry(); + $phoneCountry->code = $code; + $phoneCountry->country_code = $resolvedMetadata->getCountryCode(); + $phoneCountry->save(); + + $this->info($code . ' - ' . $countryCodes[$code] . ' inserted'); + } + } + } +} diff --git a/flexiapi/app/Helpers/Utils.php b/flexiapi/app/Helpers/Utils.php index f61d0a8..1105201 100644 --- a/flexiapi/app/Helpers/Utils.php +++ b/flexiapi/app/Helpers/Utils.php @@ -217,236 +217,253 @@ function validateIsoDate($attribute, $value, $parameters, $validator): bool function getCountryCodes() { return [ - 'AF' => 'Afghanistan', - 'AX' => 'Åland Islands', - 'AL' => 'Albania', - 'DZ' => 'Algeria', - 'AS' => 'American Samoa', 'AD' => 'Andorra', - 'AO' => 'Angola', - 'AI' => 'Anguilla', + 'AE' => 'United Arab Emirates', + 'AF' => 'Afghanistan', 'AG' => 'Antigua & Barbuda', + 'AI' => 'Anguilla', + 'AL' => 'Albania', + 'AM' => 'Armenia', + 'AO' => 'Angola', + 'AQ' => 'Antarctica', 'AR' => 'Argentina', - 'AU' => 'Australia', + 'AS' => 'American Samoa', 'AT' => 'Austria', + 'AU' => 'Australia', + 'AW' => 'Aruba', + 'AX' => 'Åland Islands', 'AZ' => 'Azerbaijan', - 'BS' => 'Bahamas', - 'BH' => 'Bahrain', - 'BD' => 'Bangladesh', - 'BB' => 'Barbados', - 'BY' => 'Belarus', - 'BE' => 'Belgium', - 'BZ' => 'Belize', - 'BJ' => 'Benin', - 'BM' => 'Bermuda', - 'BT' => 'Bhutan', - 'BO' => 'Bolivia', 'BA' => 'Bosnia & Herzegovina', - 'BW' => 'Botswana', - 'BR' => 'Brazil', - 'IO' => 'British Indian Ocean Territory', - 'BN' => 'Brunei', - 'BG' => 'Bulgaria', + 'BB' => 'Barbados', + 'BD' => 'Bangladesh', + 'BE' => 'Belgium', 'BF' => 'Burkina Faso', + 'BG' => 'Bulgaria', + 'BH' => 'Bahrain', 'BI' => 'Burundi', - 'KH' => 'Cambodia', - 'CM' => 'Cameroon', + 'BJ' => 'Benin', + 'BL' => 'St. Barthélemy', + 'BM' => 'Bermuda', + 'BN' => 'Brunei', + 'BO' => 'Bolivia', + 'BQ' => 'Bonaire, Sint Eustatius & Saba', + 'BR' => 'Brazil', + 'BS' => 'Bahamas', + 'BT' => 'Bhutan', + 'BV' => 'Bouvet Island', + 'BW' => 'Botswana', + 'BY' => 'Belarus', + 'BZ' => 'Belize', 'CA' => 'Canada', - 'CV' => 'Cape Verde', - 'KY' => 'Cayman Islands', - 'CF' => 'Central African Republic', - 'TD' => 'Chad', - 'CL' => 'Chile', - 'CN' => 'China', - 'CX' => 'Christmas Island', 'CC' => 'Cocos (Keeling) Islands', - 'CO' => 'Colombia', - 'KM' => 'Comoros', - 'CG' => 'Congo - Brazzaville', 'CD' => 'Congo - Kinshasa', + 'CF' => 'Central African Republic', + 'CG' => 'Congo - Brazzaville', + 'CH' => 'Switzerland', + 'CI' => "Côte d'Ivoire", 'CK' => 'Cook Islands', + 'CL' => 'Chile', + 'CM' => 'Cameroon', + 'CN' => 'China', + 'CO' => 'Colombia', 'CR' => 'Costa Rica', - 'CI' => 'Côte d’Ivoire', - 'HR' => 'Croatia', 'CU' => 'Cuba', + 'CV' => 'Cabo Verde', + 'CW' => 'Curaçao', + 'CX' => 'Christmas Island', 'CY' => 'Cyprus', 'CZ' => 'Czechia', - 'DK' => 'Denmark', + 'DE' => 'Germany', 'DJ' => 'Djibouti', + 'DK' => 'Denmark', 'DM' => 'Dominica', 'DO' => 'Dominican Republic', + 'DZ' => 'Algeria', 'EC' => 'Ecuador', - 'EG' => 'Egypt', - 'SV' => 'El Salvador', - 'GQ' => 'Equatorial Guinea', - 'ER' => 'Eritrea', 'EE' => 'Estonia', + 'EG' => 'Egypt', + 'EH' => 'Western Sahara', + 'ER' => 'Eritrea', + 'ES' => 'Spain', 'ET' => 'Ethiopia', - 'FK' => 'Falkland Islands', - 'FO' => 'Faroe Islands', - 'FJ' => 'Fiji', 'FI' => 'Finland', + 'FJ' => 'Fiji', + 'FK' => 'Falkland Islands', + 'FM' => 'Micronesia', + 'FO' => 'Faroe Islands', 'FR' => 'France', - 'GF' => 'French Guiana', - 'PF' => 'French Polynesia', 'GA' => 'Gabon', - 'GM' => 'Gambia', + 'GB' => 'United Kingdom', + 'GD' => 'Grenada', 'GE' => 'Georgia', - 'DE' => 'Germany', + 'GF' => 'French Guiana', + 'GG' => 'Guernsey', 'GH' => 'Ghana', 'GI' => 'Gibraltar', - 'GR' => 'Greece', 'GL' => 'Greenland', - 'GD' => 'Grenada', - 'GP' => 'Guadeloupe', - 'GU' => 'Guam', - 'GT' => 'Guatemala', - 'GG' => 'Guernsey', + 'GM' => 'Gambia', 'GN' => 'Guinea', + 'GP' => 'Guadeloupe', + 'GQ' => 'Equatorial Guinea', + 'GR' => 'Greece', + 'GS' => 'South Georgia & South Sandwich Islands', + 'GT' => 'Guatemala', + 'GU' => 'Guam', 'GW' => 'Guinea-Bissau', 'GY' => 'Guyana', - 'HT' => 'Haiti', + 'HK' => 'Hong Kong', + 'HM' => 'Heard & McDonald Islands', 'HN' => 'Honduras', - 'HK' => 'Hong Kong SAR China', + 'HR' => 'Croatia', + 'HT' => 'Haiti', 'HU' => 'Hungary', - 'IS' => 'Iceland', - 'IN' => 'India', 'ID' => 'Indonesia', - 'IR' => 'Iran', - 'IQ' => 'Iraq', 'IE' => 'Ireland', - 'IM' => 'Isle of Man', 'IL' => 'Israel', + 'IM' => 'Isle of Man', + 'IN' => 'India', + 'IO' => 'British Indian Ocean Territory', + 'IQ' => 'Iraq', + 'IR' => 'Iran', + 'IS' => 'Iceland', 'IT' => 'Italy', - 'JM' => 'Jamaica', - 'JP' => 'Japan', 'JE' => 'Jersey', + 'JM' => 'Jamaica', 'JO' => 'Jordan', - 'KZ' => 'Kazakhstan', + 'JP' => 'Japan', 'KE' => 'Kenya', + 'KG' => 'Kyrgyzstan', + 'KH' => 'Cambodia', 'KI' => 'Kiribati', + 'KM' => 'Comoros', + 'KN' => 'St. Kitts & Nevis', 'KP' => 'North Korea', 'KR' => 'South Korea', 'KW' => 'Kuwait', - 'KG' => 'Kyrgyzstan', + 'KY' => 'Cayman Islands', + 'KZ' => 'Kazakhstan', 'LA' => 'Laos', - 'LV' => 'Latvia', 'LB' => 'Lebanon', - 'LS' => 'Lesotho', - 'LR' => 'Liberia', - 'LY' => 'Libya', + 'LC' => 'St. Lucia', 'LI' => 'Liechtenstein', + 'LK' => 'Sri Lanka', + 'LR' => 'Liberia', + 'LS' => 'Lesotho', 'LT' => 'Lithuania', 'LU' => 'Luxembourg', - 'MO' => 'Macao SAR China', - 'MK' => 'North Macedonia', + 'LV' => 'Latvia', + 'LY' => 'Libya', + 'MA' => 'Morocco', + 'MC' => 'Monaco', + 'MD' => 'Moldova', + 'ME' => 'Montenegro', + 'MF' => 'St. Martin', 'MG' => 'Madagascar', - 'MW' => 'Malawi', - 'MY' => 'Malaysia', - 'MV' => 'Maldives', - 'ML' => 'Mali', - 'MT' => 'Malta', 'MH' => 'Marshall Islands', + 'MK' => 'North Macedonia', + 'ML' => 'Mali', + 'MM' => 'Myanmar', + 'MN' => 'Mongolia', + 'MO' => 'Macao', + 'MP' => 'Northern Mariana Islands', 'MQ' => 'Martinique', 'MR' => 'Mauritania', - 'MU' => 'Mauritius', - 'YT' => 'Mayotte', - 'MX' => 'Mexico', - 'FM' => 'Micronesia', - 'MD' => 'Moldova', - 'MC' => 'Monaco', - 'MN' => 'Mongolia', - 'ME' => 'Montenegro', 'MS' => 'Montserrat', - 'MA' => 'Morocco', + 'MT' => 'Malta', + 'MU' => 'Mauritius', + 'MV' => 'Maldives', + 'MW' => 'Malawi', + 'MX' => 'Mexico', + 'MY' => 'Malaysia', 'MZ' => 'Mozambique', - 'MM' => 'Myanmar (Burma)', 'NA' => 'Namibia', - 'NR' => 'Nauru', - 'NP' => 'Nepal', - 'NL' => 'Netherlands', 'NC' => 'New Caledonia', - 'NZ' => 'New Zealand', - 'NI' => 'Nicaragua', 'NE' => 'Niger', - 'NG' => 'Nigeria', - 'NU' => 'Niue', 'NF' => 'Norfolk Island', - 'MP' => 'Northern Mariana Islands', + 'NG' => 'Nigeria', + 'NI' => 'Nicaragua', + 'NL' => 'Netherlands', 'NO' => 'Norway', + 'NP' => 'Nepal', + 'NR' => 'Nauru', + 'NU' => 'Niue', + 'NZ' => 'New Zealand', 'OM' => 'Oman', - 'PK' => 'Pakistan', - 'PW' => 'Palau', - 'PS' => 'Palestinian Territories', 'PA' => 'Panama', - 'PG' => 'Papua New Guinea', - 'PY' => 'Paraguay', 'PE' => 'Peru', + 'PF' => 'French Polynesia', + 'PG' => 'Papua New Guinea', 'PH' => 'Philippines', + 'PK' => 'Pakistan', 'PL' => 'Poland', - 'PT' => 'Portugal', + 'PM' => 'St. Pierre & Miquelon', + 'PN' => 'Pitcairn Islands', 'PR' => 'Puerto Rico', + 'PS' => 'Palestine', + 'PT' => 'Portugal', + 'PW' => 'Palau', + 'PY' => 'Paraguay', 'QA' => 'Qatar', 'RE' => 'Réunion', 'RO' => 'Romania', + 'RS' => 'Serbia', 'RU' => 'Russia', 'RW' => 'Rwanda', - 'SH' => 'St. Helena', - 'KN' => 'St. Kitts & Nevis', - 'LC' => 'St. Lucia', - 'PM' => 'St. Pierre & Miquelon', - 'VC' => 'St. Vincent & Grenadines', - 'WS' => 'Samoa', - 'SM' => 'San Marino', - 'ST' => 'São Tomé & Príncipe', 'SA' => 'Saudi Arabia', - 'SN' => 'Senegal', - 'RS' => 'Serbia', - 'SC' => 'Seychelles', - 'SL' => 'Sierra Leone', - 'SG' => 'Singapore', - 'SK' => 'Slovakia', - 'SI' => 'Slovenia', 'SB' => 'Solomon Islands', - 'SO' => 'Somalia', - 'ZA' => 'South Africa', - 'ES' => 'Spain', - 'LK' => 'Sri Lanka', + 'SC' => 'Seychelles', 'SD' => 'Sudan', - 'SR' => 'Suriname', - 'SJ' => 'Svalbard & Jan Mayen', - 'SZ' => 'Eswatini', 'SE' => 'Sweden', - 'CH' => 'Switzerland', + 'SG' => 'Singapore', + 'SH' => 'St. Helena', + 'SI' => 'Slovenia', + 'SJ' => 'Svalbard & Jan Mayen', + 'SK' => 'Slovakia', + 'SL' => 'Sierra Leone', + 'SM' => 'San Marino', + 'SN' => 'Senegal', + 'SO' => 'Somalia', + 'SR' => 'Suriname', + 'SS' => 'South Sudan', + 'ST' => 'São Tomé & Príncipe', + 'SV' => 'El Salvador', + 'SX' => 'Sint Maarten', 'SY' => 'Syria', - 'TW' => 'Taiwan', - 'TJ' => 'Tajikistan', - 'TZ' => 'Tanzania', - 'TH' => 'Thailand', - 'TL' => 'Timor-Leste', - 'TG' => 'Togo', - 'TK' => 'Tokelau', - 'TO' => 'Tonga', - 'TT' => 'Trinidad & Tobago', - 'TN' => 'Tunisia', - 'TM' => 'Turkmenistan', + 'SZ' => 'Eswatini', 'TC' => 'Turks & Caicos Islands', + 'TD' => 'Chad', + 'TF' => 'French Southern Territories', + 'TG' => 'Togo', + 'TH' => 'Thailand', + 'TJ' => 'Tajikistan', + 'TK' => 'Tokelau', + 'TL' => 'Timor-Leste', + 'TM' => 'Turkmenistan', + 'TN' => 'Tunisia', + 'TO' => 'Tonga', + 'TR' => 'Türkiye', + 'TT' => 'Trinidad & Tobago', 'TV' => 'Tuvalu', - 'UG' => 'Uganda', + 'TW' => 'Taiwan', + 'TZ' => 'Tanzania', 'UA' => 'Ukraine', - 'AE' => 'United Arab Emirates', - 'GB' => 'United Kingdom', + 'UG' => 'Uganda', + 'UM' => 'U.S. Minor Outlying Islands', 'US' => 'United States', 'UY' => 'Uruguay', 'UZ' => 'Uzbekistan', - 'VU' => 'Vanuatu', + 'VA' => 'Holy See (Vatican City)', + 'VC' => 'St. Vincent & Grenadines', 'VE' => 'Venezuela', - 'VN' => 'Vietnam', 'VG' => 'British Virgin Islands', 'VI' => 'U.S. Virgin Islands', + 'VN' => 'Vietnam', + 'VU' => 'Vanuatu', 'WF' => 'Wallis & Futuna', - 'EH' => 'Western Sahara', + 'WS' => 'Samoa', 'YE' => 'Yemen', + 'YT' => 'Mayotte', + 'ZA' => 'South Africa', 'ZM' => 'Zambia', 'ZW' => 'Zimbabwe', ]; diff --git a/flexiapi/database/migrations/2024_07_15_135258_create_phone_countries_table.php b/flexiapi/database/migrations/2024_07_15_135258_create_phone_countries_table.php index 5aa8bbf..bc54887 100644 --- a/flexiapi/database/migrations/2024_07_15_135258_create_phone_countries_table.php +++ b/flexiapi/database/migrations/2024_07_15_135258_create_phone_countries_table.php @@ -7,8 +7,7 @@ use Illuminate\Support\Facades\Schema; use libphonenumber\PhoneNumberUtil; -return new class extends Migration -{ +return new class extends Migration { public function up() { Schema::create('phone_countries', function (Blueprint $table) { @@ -20,10 +19,12 @@ return new class extends Migration $phoneNumberUtils = PhoneNumberUtil::getInstance(); foreach (getCountryCodes() as $code => $name) { - $phoneCountry = new PhoneCountry(); - $phoneCountry->code = $code; - $phoneCountry->country_code = $phoneNumberUtils->getMetadataForRegion($code)->getCountryCode(); - $phoneCountry->save(); + if ($resolvedMetadata = $phoneNumberUtils->getMetadataForRegion($code)) { + $phoneCountry = new PhoneCountry(); + $phoneCountry->code = $code; + $phoneCountry->country_code = $resolvedMetadata->getCountryCode(); + $phoneCountry->save(); + } } }