diff --git a/src/app/Console/Commands/DataCountries.php b/src/app/Console/Commands/DataCountries.php index 3c6bbd60..ac94cdf6 100644 --- a/src/app/Console/Commands/DataCountries.php +++ b/src/app/Console/Commands/DataCountries.php @@ -1,142 +1,97 @@ currency + 'LT' => 'EUR', + ]; + /** * The name and signature of the console command. * * @var string */ protected $signature = 'data:countries'; /** * The console command description. * * @var string */ protected $description = 'Fetches countries map from wikipedia'; /** * Execute the console command. * * @return mixed */ public function handle() { $countries = []; $currencies = []; - $currencies_url = 'http://en.wikipedia.org/wiki/ISO_4217'; - $countries_url = 'http://en.wikipedia.org/wiki/ISO_3166-1'; + $currencies_url = 'http://country.io/currency.json'; + $countries_url = 'http://country.io/names.json'; $this->info("Fetching currencies from $currencies_url..."); // fetch currency table and create an index by country page url - $page = file_get_contents($currencies_url); + $currencies_json = file_get_contents($currencies_url); - if (!$page) { + if (!$currencies_json) { $this->error("Failed to fetch currencies"); return; } - $table_regexp = '!!ims'; - if (preg_match_all($table_regexp, $page, $matches, PREG_PATTERN_ORDER)) { - foreach ($matches[0] as $currency_table) { - preg_match_all('!\s*\s*!Ums', $currency_table, $rows); - - foreach ($rows[1] as $row) { - $cells = preg_split('!\s*]*>!', $row); - - if (count($cells) == 5) { - // actual currency table - $currency = preg_match('/([A-Z]{3})/', $cells[0], $m) ? $m[1] : ''; - - if (preg_match('/(\d+)/', $cells[1], $m)) { - $isocode = $m[1]; - $currencies[$m[1]] = $currency; - } - - preg_match_all('!]+href="(/wiki/[^"]+)"[^>]*>!', $cells[4], $links, PREG_PATTERN_ORDER); - - foreach ($links[1] as $link) { - $currencies[strtolower($link)] = $currency; - } - } elseif (count($cells) == 7) { - // replacements table - $currency = preg_match('/([A-Z]{3})/', $cells[6], $m) ? $m[1] : ''; - - if (preg_match('/(\d+)/', $cells[1], $m)) { - $currencies[$m[1]] = $currency; - } - } - } - } - } - - $namecol = 0; - $codecol = 1; - $numcol = 3; - $lang = 'en'; - $this->info("Fetching countries from $countries_url..."); - $page = file_get_contents($countries_url); + $countries_json = file_get_contents($countries_url); - if (!$page) { + if (!$countries_json) { $this->error("Failed to fetch countries"); return; } - if (preg_match($table_regexp, $page, $matches)) { - preg_match_all('!\s*\s*!Ums', $matches[0], $rows); - - foreach ($rows[1] as $row) { - $cells = preg_split('!\s*]*>!', $row); - - if (count($cells) < 5) { - continue; - } - - $regexp = '!]+href="(/wiki/[^"]+)"[^>]*>([^>]+)!i'; - $content = preg_match($regexp, $cells[$namecol], $m) ? $m : null; - - if (preg_match('/>([A-Z]{2})error("Invalid countries data"); + return; + } + + if (!is_array($currencies) || empty($currencies)) { + $this->error("Invalid currencies data"); + return; } $file = resource_path('countries.php'); $this->info("Generating resource file $file..."); + asort($countries); + $out = " $names) { - if (!empty($names['en']) && !empty($names['currency'])) { - $out .= sprintf(" '%s' => ['%s','%s'],\n", $code, $names['currency'], addslashes($names['en'])); + foreach ($countries as $code => $name) { + $currency = $currencies[$code] ?? null; + + if (!empty($this->currency_fixes[$code])) { + $currency = $this->currency_fixes[$code]; + } + + if (!$currency) { + $this->error("Unknown currency for {$name} ({$code}). Skipped."); + continue; } + + $out .= sprintf(" '%s' => ['%s','%s'],\n", $code, $currency, addslashes($name)); } $out .= "];\n"; file_put_contents($file, $out); } } diff --git a/src/resources/countries.php b/src/resources/countries.php index b2a7aad8..42708918 100644 --- a/src/resources/countries.php +++ b/src/resources/countries.php @@ -1,238 +1,251 @@ ['AFN','Afghanistan'], - 'AX' => ['EUR','Åland Islands'], + 'AX' => ['EUR','Aland Islands'], 'AL' => ['ALL','Albania'], 'DZ' => ['DZD','Algeria'], 'AS' => ['USD','American Samoa'], 'AD' => ['EUR','Andorra'], 'AO' => ['AOA','Angola'], 'AI' => ['XCD','Anguilla'], 'AG' => ['XCD','Antigua and Barbuda'], 'AR' => ['ARS','Argentina'], 'AM' => ['AMD','Armenia'], 'AW' => ['AWG','Aruba'], 'AU' => ['AUD','Australia'], 'AT' => ['EUR','Austria'], 'AZ' => ['AZN','Azerbaijan'], + 'BS' => ['BSD','Bahamas'], 'BH' => ['BHD','Bahrain'], 'BD' => ['BDT','Bangladesh'], - 'BB' => ['USD','Barbados'], - 'BY' => ['BYN','Belarus'], + 'BB' => ['BBD','Barbados'], + 'BY' => ['BYR','Belarus'], 'BE' => ['EUR','Belgium'], 'BZ' => ['BZD','Belize'], 'BJ' => ['XOF','Benin'], - 'BM' => ['USD','Bermuda'], - 'BT' => ['INR','Bhutan'], - 'BO' => ['BOV','Bolivia (Plurinational State of)'], - 'BQ' => ['USD','Bonaire, Sint Eustatius and Saba'], + 'BM' => ['BMD','Bermuda'], + 'BT' => ['BTN','Bhutan'], + 'BO' => ['BOB','Bolivia'], + 'BQ' => ['USD','Bonaire, Saint Eustatius and Saba '], 'BA' => ['BAM','Bosnia and Herzegovina'], 'BW' => ['BWP','Botswana'], 'BV' => ['NOK','Bouvet Island'], 'BR' => ['BRL','Brazil'], 'IO' => ['USD','British Indian Ocean Territory'], + 'VG' => ['USD','British Virgin Islands'], + 'BN' => ['BND','Brunei'], 'BG' => ['BGN','Bulgaria'], 'BF' => ['XOF','Burkina Faso'], 'BI' => ['BIF','Burundi'], - 'KH' => ['USD','Cambodia'], + 'KH' => ['KHR','Cambodia'], 'CM' => ['XAF','Cameroon'], 'CA' => ['CAD','Canada'], + 'CV' => ['CVE','Cape Verde'], 'KY' => ['KYD','Cayman Islands'], 'CF' => ['XAF','Central African Republic'], 'TD' => ['XAF','Chad'], 'CL' => ['CLP','Chile'], 'CN' => ['CNY','China'], 'CX' => ['AUD','Christmas Island'], - 'CC' => ['AUD','Cocos (Keeling) Islands'], - 'CO' => ['COU','Colombia'], + 'CC' => ['AUD','Cocos Islands'], + 'CO' => ['COP','Colombia'], 'KM' => ['KMF','Comoros'], - 'CG' => ['XAF','Congo'], - 'CD' => ['CDF','Congo, Democratic Republic of the'], 'CK' => ['NZD','Cook Islands'], 'CR' => ['CRC','Costa Rica'], - 'CI' => ['XOF','Côte d\'Ivoire'], 'HR' => ['HRK','Croatia'], 'CU' => ['CUP','Cuba'], - 'CW' => ['ANG','Curaçao'], + 'CW' => ['ANG','Curacao'], 'CY' => ['EUR','Cyprus'], - 'CZ' => ['CZK','Czechia'], + 'CZ' => ['CZK','Czech Republic'], + 'CD' => ['CDF','Democratic Republic of the Congo'], 'DK' => ['DKK','Denmark'], 'DJ' => ['DJF','Djibouti'], 'DM' => ['XCD','Dominica'], 'DO' => ['DOP','Dominican Republic'], + 'TL' => ['USD','East Timor'], 'EC' => ['USD','Ecuador'], 'EG' => ['EGP','Egypt'], 'SV' => ['USD','El Salvador'], 'GQ' => ['XAF','Equatorial Guinea'], 'ER' => ['ERN','Eritrea'], 'EE' => ['EUR','Estonia'], - 'SZ' => ['SZL','Eswatini'], 'ET' => ['ETB','Ethiopia'], - 'FK' => ['FKP','Falkland Islands (Malvinas)'], + 'FK' => ['FKP','Falkland Islands'], 'FO' => ['DKK','Faroe Islands'], 'FJ' => ['FJD','Fiji'], 'FI' => ['EUR','Finland'], 'FR' => ['EUR','France'], 'GF' => ['EUR','French Guiana'], 'PF' => ['XPF','French Polynesia'], 'TF' => ['EUR','French Southern Territories'], 'GA' => ['XAF','Gabon'], + 'GM' => ['GMD','Gambia'], 'GE' => ['GEL','Georgia'], 'DE' => ['EUR','Germany'], 'GH' => ['GHS','Ghana'], 'GI' => ['GIP','Gibraltar'], 'GR' => ['EUR','Greece'], 'GL' => ['DKK','Greenland'], 'GD' => ['XCD','Grenada'], 'GP' => ['EUR','Guadeloupe'], 'GU' => ['USD','Guam'], 'GT' => ['GTQ','Guatemala'], + 'GG' => ['GBP','Guernsey'], 'GN' => ['GNF','Guinea'], 'GW' => ['XOF','Guinea-Bissau'], 'GY' => ['GYD','Guyana'], - 'HT' => ['USD','Haiti'], + 'HT' => ['HTG','Haiti'], 'HM' => ['AUD','Heard Island and McDonald Islands'], 'HN' => ['HNL','Honduras'], 'HK' => ['HKD','Hong Kong'], 'HU' => ['HUF','Hungary'], 'IS' => ['ISK','Iceland'], 'IN' => ['INR','India'], 'ID' => ['IDR','Indonesia'], - 'IR' => ['IRR','Iran (Islamic Republic of)'], + 'IR' => ['IRR','Iran'], 'IQ' => ['IQD','Iraq'], 'IE' => ['EUR','Ireland'], 'IM' => ['GBP','Isle of Man'], 'IL' => ['ILS','Israel'], 'IT' => ['EUR','Italy'], + 'CI' => ['XOF','Ivory Coast'], 'JM' => ['JMD','Jamaica'], 'JP' => ['JPY','Japan'], 'JE' => ['GBP','Jersey'], 'JO' => ['JOD','Jordan'], 'KZ' => ['KZT','Kazakhstan'], 'KE' => ['KES','Kenya'], 'KI' => ['AUD','Kiribati'], - 'KP' => ['KPW','Korea (Democratic People\'s Republic of)'], - 'KR' => ['KRW','Korea, Republic of'], + 'XK' => ['EUR','Kosovo'], 'KW' => ['KWD','Kuwait'], 'KG' => ['KGS','Kyrgyzstan'], - 'LA' => ['LAK','Lao People\'s Democratic Republic'], + 'LA' => ['LAK','Laos'], 'LV' => ['EUR','Latvia'], 'LB' => ['LBP','Lebanon'], - 'LS' => ['ZAR','Lesotho'], + 'LS' => ['LSL','Lesotho'], 'LR' => ['LRD','Liberia'], 'LY' => ['LYD','Libya'], 'LI' => ['CHF','Liechtenstein'], 'LT' => ['EUR','Lithuania'], 'LU' => ['EUR','Luxembourg'], 'MO' => ['MOP','Macao'], + 'MK' => ['MKD','Macedonia'], 'MG' => ['MGA','Madagascar'], 'MW' => ['MWK','Malawi'], 'MY' => ['MYR','Malaysia'], 'MV' => ['MVR','Maldives'], 'ML' => ['XOF','Mali'], 'MT' => ['EUR','Malta'], 'MH' => ['USD','Marshall Islands'], 'MQ' => ['EUR','Martinique'], - 'MR' => ['MRU','Mauritania'], + 'MR' => ['MRO','Mauritania'], 'MU' => ['MUR','Mauritius'], 'YT' => ['EUR','Mayotte'], - 'MX' => ['MXV','Mexico'], - 'FM' => ['USD','Micronesia (Federated States of)'], - 'MD' => ['MDL','Moldova, Republic of'], + 'MX' => ['MXN','Mexico'], + 'FM' => ['USD','Micronesia'], + 'MD' => ['MDL','Moldova'], 'MC' => ['EUR','Monaco'], 'MN' => ['MNT','Mongolia'], 'ME' => ['EUR','Montenegro'], 'MS' => ['XCD','Montserrat'], 'MA' => ['MAD','Morocco'], 'MZ' => ['MZN','Mozambique'], 'MM' => ['MMK','Myanmar'], - 'NA' => ['ZAR','Namibia'], + 'NA' => ['NAD','Namibia'], 'NR' => ['AUD','Nauru'], 'NP' => ['NPR','Nepal'], + 'NL' => ['EUR','Netherlands'], 'NC' => ['XPF','New Caledonia'], 'NZ' => ['NZD','New Zealand'], 'NI' => ['NIO','Nicaragua'], 'NE' => ['XOF','Niger'], 'NG' => ['NGN','Nigeria'], 'NU' => ['NZD','Niue'], 'NF' => ['AUD','Norfolk Island'], - 'MK' => ['MKD','North Macedonia'], + 'KP' => ['KPW','North Korea'], 'MP' => ['USD','Northern Mariana Islands'], - 'NL' => ['EUR','The Netherlands'], 'NO' => ['NOK','Norway'], 'OM' => ['OMR','Oman'], 'PK' => ['PKR','Pakistan'], 'PW' => ['USD','Palau'], - 'PA' => ['USD','Panama'], + 'PS' => ['ILS','Palestinian Territory'], + 'PA' => ['PAB','Panama'], 'PG' => ['PGK','Papua New Guinea'], 'PY' => ['PYG','Paraguay'], 'PE' => ['PEN','Peru'], 'PH' => ['PHP','Philippines'], 'PN' => ['NZD','Pitcairn'], 'PL' => ['PLN','Poland'], 'PT' => ['EUR','Portugal'], 'PR' => ['USD','Puerto Rico'], 'QA' => ['QAR','Qatar'], - 'RE' => ['EUR','Réunion'], + 'CG' => ['XAF','Republic of the Congo'], + 'RE' => ['EUR','Reunion'], 'RO' => ['RON','Romania'], - 'RU' => ['RUB','Russian Federation'], + 'RU' => ['RUB','Russia'], 'RW' => ['RWF','Rwanda'], - 'BL' => ['EUR','Saint Barthélemy'], + 'BL' => ['EUR','Saint Barthelemy'], + 'SH' => ['SHP','Saint Helena'], 'KN' => ['XCD','Saint Kitts and Nevis'], 'LC' => ['XCD','Saint Lucia'], - 'MF' => ['EUR','Saint Martin (French part)'], + 'MF' => ['EUR','Saint Martin'], 'PM' => ['EUR','Saint Pierre and Miquelon'], 'VC' => ['XCD','Saint Vincent and the Grenadines'], 'WS' => ['WST','Samoa'], 'SM' => ['EUR','San Marino'], + 'ST' => ['STD','Sao Tome and Principe'], 'SA' => ['SAR','Saudi Arabia'], 'SN' => ['XOF','Senegal'], 'RS' => ['RSD','Serbia'], 'SC' => ['SCR','Seychelles'], 'SL' => ['SLL','Sierra Leone'], 'SG' => ['SGD','Singapore'], - 'SX' => ['ANG','Sint Maarten (Dutch part)'], + 'SX' => ['ANG','Sint Maarten'], 'SK' => ['EUR','Slovakia'], 'SI' => ['EUR','Slovenia'], 'SB' => ['SBD','Solomon Islands'], 'SO' => ['SOS','Somalia'], 'ZA' => ['ZAR','South Africa'], + 'GS' => ['GBP','South Georgia and the South Sandwich Islands'], + 'KR' => ['KRW','South Korea'], 'SS' => ['SSP','South Sudan'], 'ES' => ['EUR','Spain'], 'LK' => ['LKR','Sri Lanka'], 'SD' => ['SDG','Sudan'], 'SR' => ['SRD','Suriname'], + 'SJ' => ['NOK','Svalbard and Jan Mayen'], + 'SZ' => ['SZL','Swaziland'], 'SE' => ['SEK','Sweden'], - 'CH' => ['CHW','Switzerland'], - 'SY' => ['SYP','Syrian Arab Republic'], + 'CH' => ['CHF','Switzerland'], + 'SY' => ['SYP','Syria'], + 'TW' => ['TWD','Taiwan'], 'TJ' => ['TJS','Tajikistan'], - 'TZ' => ['TZS','Tanzania, United Republic of'], + 'TZ' => ['TZS','Tanzania'], 'TH' => ['THB','Thailand'], - 'TL' => ['USD','Timor-Leste'], 'TG' => ['XOF','Togo'], 'TK' => ['NZD','Tokelau'], 'TO' => ['TOP','Tonga'], 'TT' => ['TTD','Trinidad and Tobago'], 'TN' => ['TND','Tunisia'], 'TR' => ['TRY','Turkey'], 'TM' => ['TMT','Turkmenistan'], 'TC' => ['USD','Turks and Caicos Islands'], 'TV' => ['AUD','Tuvalu'], + 'VI' => ['USD','U.S. Virgin Islands'], 'UG' => ['UGX','Uganda'], 'UA' => ['UAH','Ukraine'], 'AE' => ['AED','United Arab Emirates'], - 'GB' => ['GBP','United Kingdom of Great Britain and Northern Ireland'], - 'US' => ['USN','United States of America'], + 'GB' => ['GBP','United Kingdom'], + 'US' => ['USD','United States'], 'UM' => ['USD','United States Minor Outlying Islands'], - 'UY' => ['UYW','Uruguay'], + 'UY' => ['UYU','Uruguay'], 'UZ' => ['UZS','Uzbekistan'], 'VU' => ['VUV','Vanuatu'], - 'VE' => ['VES','Venezuela (Bolivarian Republic of)'], - 'VN' => ['VND','Viet Nam'], - 'VG' => ['USD','Virgin Islands (British)'], - 'VI' => ['USD','Virgin Islands (U.S.)'], + 'VA' => ['EUR','Vatican'], + 'VE' => ['VEF','Venezuela'], + 'VN' => ['VND','Vietnam'], 'WF' => ['XPF','Wallis and Futuna'], 'EH' => ['MAD','Western Sahara'], 'YE' => ['YER','Yemen'], - 'ZM' => ['ZMW','Zambia'], + 'ZM' => ['ZMK','Zambia'], 'ZW' => ['ZWL','Zimbabwe'], ];
(.+)
(.+)