> ./checkCountryName.sh China
People's Republic of China
Created
January 23, 2020 16:32
-
-
Save matthewpoer/36a72df5fd466108a1a7aea7dbe68212 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
countryNameInput=$1 | |
countryNameOutput=$(docker run \ | |
--interactive \ | |
--tty \ | |
--rm \ | |
--volume "$PWD":/source \ | |
php:cli-alpine php -r "require_once('/source/CountryApi.php'); echo CountryApi::_validateCountryName('$countryNameInput');"); | |
echo $countryNameOutput | |
if [ "$2" = "--trump-mode" ] && [ "$countryNameOutput" = "People's Republic of China" ] | |
then | |
echo "-- CHINA! -- https://www.youtube.com/watch?v=RDrfE9I8_hs" | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Copied from LF's SugarCRM system, but sans references to SugarAPI and ServiceBase | |
class CountryApi { | |
/** | |
* map of official country names to official 2-digit ISO | |
* @see https://en.wikipedia.org/wiki/ISO_3166-1 | |
* @var array | |
*/ | |
private static $officialCountryNametoIsoList = array( | |
'Afghanistan' => 'AF', | |
'Åland Islands' => 'AX', | |
'Albania' => 'AL', | |
'Algeria' => 'DZ', | |
'American Samoa' => 'AS', | |
'Andorra' => 'AD', | |
'Angola' => 'AO', | |
'Anguilla' => 'AI', | |
'Antarctica' => 'AQ', | |
'Antigua and Barbuda' => 'AG', | |
'Argentina' => 'AR', | |
'Armenia' => 'AM', | |
'Aruba' => 'AW', | |
'Australia' => 'AU', | |
'Austria' => 'AT', | |
'Azerbaijan' => 'AZ', | |
'Bahamas' => 'BS', | |
'Bahrain' => 'BH', | |
'Bangladesh' => 'BD', | |
'Barbados' => 'BB', | |
'Belarus' => 'BY', | |
'Belgium' => 'BE', | |
'Belize' => 'BZ', | |
'Benin' => 'BJ', | |
'Bermuda' => 'BM', | |
'Bhutan' => 'BT', | |
'Bolivia (Plurinational State of)' => 'BO', | |
'Bonaire, Sint Eustatius and Saba' => 'BQ', | |
'Bosnia and Herzegovina' => 'BA', | |
'Botswana' => 'BW', | |
'Bouvet Island' => 'BV', | |
'Brazil' => 'BR', | |
'British Indian Ocean Territory' => 'IO', | |
'Brunei Darussalam' => 'BN', | |
'Bulgaria' => 'BG', | |
'Burkina Faso' => 'BF', | |
'Burundi' => 'BI', | |
'Cabo Verde' => 'CV', | |
'Cambodia' => 'KH', | |
'Cameroon' => 'CM', | |
'Canada' => 'CA', | |
'Cayman Islands' => 'KY', | |
'Central African Republic' => 'CF', | |
'Chad' => 'TD', | |
'Chile' => 'CL', | |
'People\'s Republic of China' => 'CN', | |
'Christmas Island' => 'CX', | |
'Cocos (Keeling) Islands' => 'CC', | |
'Colombia' => 'CO', | |
'Comoros' => 'KM', | |
'Congo' => 'CG', | |
'Congo (Democratic Republic of the)' => 'CD', | |
'Cook Islands' => 'CK', | |
'Costa Rica' => 'CR', | |
'Côte d\'Ivoire' => 'CI', | |
'Croatia' => 'HR', | |
'Cuba' => 'CU', | |
'Curaçao' => 'CW', | |
'Cyprus' => 'CY', | |
'Czech Republic' => 'CZ', | |
'Denmark' => 'DK', | |
'Djibouti' => 'DJ', | |
'Dominica' => 'DM', | |
'Dominican Republic' => 'DO', | |
'Ecuador' => 'EC', | |
'Egypt' => 'EG', | |
'El Salvador' => 'SV', | |
'Equatorial Guinea' => 'GQ', | |
'Eritrea' => 'ER', | |
'Estonia' => 'EE', | |
'Ethiopia' => 'ET', | |
'Falkland Islands (Malvinas)' => 'FK', | |
'Faroe Islands' => 'FO', | |
'Fiji' => 'FJ', | |
'Finland' => 'FI', | |
'France' => 'FR', | |
'French Guiana' => 'GF', | |
'French Polynesia' => 'PF', | |
'French Southern Territories' => 'TF', | |
'Gabon' => 'GA', | |
'Gambia' => 'GM', | |
'Georgia' => 'GE', | |
'Germany' => 'DE', | |
'Ghana' => 'GH', | |
'Gibraltar' => 'GI', | |
'Greece' => 'GR', | |
'Greenland' => 'GL', | |
'Grenada' => 'GD', | |
'Guadeloupe' => 'GP', | |
'Guam' => 'GU', | |
'Guatemala' => 'GT', | |
'Guernsey' => 'GG', | |
'Guinea' => 'GN', | |
'Guinea-Bissau' => 'GW', | |
'Guyana' => 'GY', | |
'Haiti' => 'HT', | |
'Heard Island and McDonald Islands' => 'HM', | |
'Holy See' => 'VA', | |
'Honduras' => 'HN', | |
'Hong Kong' => 'HK', | |
'Hungary' => 'HU', | |
'Iceland' => 'IS', | |
'India' => 'IN', | |
'Indonesia' => 'ID', | |
'Iran (Islamic Republic of)' => 'IR', | |
'Iraq' => 'IQ', | |
'Ireland' => 'IE', | |
'Isle of Man' => 'IM', | |
'Israel' => 'IL', | |
'Italy' => 'IT', | |
'Jamaica' => 'JM', | |
'Japan' => 'JP', | |
'Jersey' => 'JE', | |
'Jordan' => 'JO', | |
'Kazakhstan' => 'KZ', | |
'Kenya' => 'KE', | |
'Kiribati' => 'KI', | |
'Korea (Democratic People\'s Republic of)' => 'KP', | |
'Korea (Republic of)' => 'KR', | |
'Kuwait' => 'KW', | |
'Kyrgyzstan' => 'KG', | |
'Lao People\'s Democratic Republic' => 'LA', | |
'Latvia' => 'LV', | |
'Lebanon' => 'LB', | |
'Lesotho' => 'LS', | |
'Liberia' => 'LR', | |
'Libya' => 'LY', | |
'Liechtenstein' => 'LI', | |
'Lithuania' => 'LT', | |
'Luxembourg' => 'LU', | |
'Macao' => 'MO', | |
'Macedonia (the former Yugoslav Republic of)' => 'MK', | |
'Madagascar' => 'MG', | |
'Malawi' => 'MW', | |
'Malaysia' => 'MY', | |
'Maldives' => 'MV', | |
'Mali' => 'ML', | |
'Malta' => 'MT', | |
'Marshall Islands' => 'MH', | |
'Martinique' => 'MQ', | |
'Mauritania' => 'MR', | |
'Mauritius' => 'MU', | |
'Mayotte' => 'YT', | |
'Mexico' => 'MX', | |
'Micronesia (Federated States of)' => 'FM', | |
'Moldova (Republic of)' => 'MD', | |
'Monaco' => 'MC', | |
'Mongolia' => 'MN', | |
'Montenegro' => 'ME', | |
'Montserrat' => 'MS', | |
'Morocco' => 'MA', | |
'Mozambique' => 'MZ', | |
'Myanmar' => 'MM', | |
'Namibia' => 'NA', | |
'Nauru' => 'NR', | |
'Nepal' => 'NP', | |
'Netherlands' => 'NL', | |
'New Caledonia' => 'NC', | |
'New Zealand' => 'NZ', | |
'Nicaragua' => 'NI', | |
'Niger' => 'NE', | |
'Nigeria' => 'NG', | |
'Niue' => 'NU', | |
'Norfolk Island' => 'NF', | |
'Northern Mariana Islands' => 'MP', | |
'Norway' => 'NO', | |
'Oman' => 'OM', | |
'Pakistan' => 'PK', | |
'Palau' => 'PW', | |
'Palestine, State of' => 'PS', | |
'Panama' => 'PA', | |
'Papua New Guinea' => 'PG', | |
'Paraguay' => 'PY', | |
'Peru' => 'PE', | |
'Philippines' => 'PH', | |
'Pitcairn' => 'PN', | |
'Poland' => 'PL', | |
'Portugal' => 'PT', | |
'Puerto Rico' => 'PR', | |
'Qatar' => 'QA', | |
'Réunion' => 'RE', | |
'Romania' => 'RO', | |
'Russian Federation' => 'RU', | |
'Rwanda' => 'RW', | |
'Saint Barthélemy' => 'BL', | |
'Saint Helena, Ascension and Tristan da Cunha' => 'SH', | |
'Saint Kitts and Nevis' => 'KN', | |
'Saint Lucia' => 'LC', | |
'Saint Martin (French part)' => 'MF', | |
'Saint Pierre and Miquelon' => 'PM', | |
'Saint Vincent and the Grenadines' => 'VC', | |
'Samoa' => 'WS', | |
'San Marino' => 'SM', | |
'Sao Tome and Principe' => 'ST', | |
'Saudi Arabia' => 'SA', | |
'Senegal' => 'SN', | |
'Serbia' => 'RS', | |
'Seychelles' => 'SC', | |
'Sierra Leone' => 'SL', | |
'Singapore' => 'SG', | |
'Sint Maarten (Dutch part)' => 'SX', | |
'Slovakia' => 'SK', | |
'Slovenia' => 'SI', | |
'Solomon Islands' => 'SB', | |
'Somalia' => 'SO', | |
'South Africa' => 'ZA', | |
'South Georgia and the South Sandwich Islands' => 'GS', | |
'South Sudan' => 'SS', | |
'Spain' => 'ES', | |
'Sri Lanka' => 'LK', | |
'Sudan' => 'SD', | |
'Suriname' => 'SR', | |
'Svalbard and Jan Mayen' => 'SJ', | |
'Swaziland' => 'SZ', | |
'Sweden' => 'SE', | |
'Switzerland' => 'CH', | |
'Syrian Arab Republic' => 'SY', | |
'Taiwan, Province of China[a]' => 'TW', | |
'Tajikistan' => 'TJ', | |
'Tanzania, United Republic of' => 'TZ', | |
'Thailand' => 'TH', | |
'Timor-Leste' => 'TL', | |
'Togo' => 'TG', | |
'Tokelau' => 'TK', | |
'Tonga' => 'TO', | |
'Trinidad and Tobago' => 'TT', | |
'Tunisia' => 'TN', | |
'Turkey' => 'TR', | |
'Turkmenistan' => 'TM', | |
'Turks and Caicos Islands' => 'TC', | |
'Tuvalu' => 'TV', | |
'Uganda' => 'UG', | |
'Ukraine' => 'UA', | |
'United Arab Emirates' => 'AE', | |
'United Kingdom of Great Britain and Northern Ireland' => 'GB', | |
'United States of America' => 'US', | |
'United States Minor Outlying Islands' => 'UM', | |
'Uruguay' => 'UY', | |
'Uzbekistan' => 'UZ', | |
'Vanuatu' => 'VU', | |
'Venezuela (Bolivarian Republic of)' => 'VE', | |
'Viet Nam' => 'VN', | |
'Virgin Islands (British)' => 'VG', | |
'Virgin Islands (U.S.)' => 'VI', | |
'Wallis and Futuna' => 'WF', | |
'Western Sahara' => 'EH', | |
'Yemen' => 'YE', | |
'Zambia' => 'ZM', | |
'Zimbabwe' => 'ZW', | |
); | |
/** | |
* Map common conventions, legacy country names and frequently found typo's | |
* to official country names. Keys are all lower case | |
* @var array | |
*/ | |
private static $commonCountryNametoOfficialCountryNameList = array( | |
'bolivia' => 'Bolivia (Plurinational State of)', | |
'bosnia/herzegovina' => 'Bosnia and Herzegovina', | |
'brasil' => 'Brazil', | |
'china' => 'People\'s Republic of China', | |
'china, peoples republic of' => 'People\'s Republic of China', | |
'cote d'ivoire' => 'Côte d\'Ivoire', | |
'côte d’ivoire' => 'Côte d\'Ivoire', | |
'hong kong s.a.r., china' => 'Hong Kong', | |
'iran' => 'Iran (Islamic Republic of)', | |
'iran, islamic republic of' => 'Iran (Islamic Republic of)', | |
'kazakstan' => 'Kazakhstan', | |
'korea' => 'Korea (Republic of)', | |
'korea, republic of' => 'Korea (Republic of)', | |
'korea, south' => 'Korea (Republic of)', | |
'lao people's democratic republic' => 'Lao People\'s Democratic Republic', | |
'laos' => 'Lao People\'s Democratic Republic', | |
'libyan arab jamahiriya' => 'Libya', | |
'macau' => 'Macao', | |
'macedonia' => 'Macedonia (the former Yugoslav Republic of)', | |
'moldova' => 'Moldova (Republic of)', | |
'moldova, republic of' => 'Moldova (Republic of)', | |
'netherlands antilles' => 'Netherlands', | |
'p.r. china' => 'People\'s Republic of China', | |
'p.r.china' => 'People\'s Republic of China', | |
'palestinian territory' => 'Palestine, State of', | |
'pr china' => 'People\'s Republic of China', | |
'prc china' => 'People\'s Republic of China', | |
'republic of korea' => 'Korea (Republic of)', | |
'republic of south korea' => 'Korea (Republic of)', | |
'roc' => 'Taiwan, Province of China[a]', | |
'russia' => 'Russian Federation', | |
'slovakia (slovak republic)' => 'Slovakia', | |
'south korea' => 'Korea (Republic of)', | |
'syria' => 'Syrian Arab Republic', | |
'taiwan roc' => 'Taiwan, Province of China[a]', | |
'taiwan' => 'Taiwan, Province of China[a]', | |
'tanzania' => 'Tanzania, United Republic of', | |
'the netherlands' => 'Netherlands', | |
'u.s.' => 'United States of America', | |
'u.s.a.' => 'United States of America', | |
'uk' => 'United Kingdom of Great Britain and Northern Ireland', | |
'united kingdom' => 'United Kingdom of Great Britain and Northern Ireland', | |
'united states' => 'United States of America', | |
'us' => 'United States of America', | |
'usa' => 'United States of America', | |
'venezuela' => 'Venezuela (Bolivarian Republic of)', | |
'vietnam' => 'Viet Nam', | |
); | |
public function registerApiRest() { | |
return array( | |
'getCountriesList' => array( | |
'reqType' => 'GET', | |
'path' => array('getCountriesList'), | |
'pathVars' => array(''), | |
'method' => 'getCountriesList' | |
), | |
'validateCountryName' => array( | |
'reqType' => 'GET', | |
'path' => array('validateCountryName', '?'), | |
'pathVars' => array('', 'country'), | |
'method' => 'validateCountryName' | |
) | |
); | |
} | |
/** | |
* get list of proper country names and their ISO mappings | |
* @return array | |
*/ | |
public static function getofficialCountryNametoIsoList() { | |
return self::$officialCountryNametoIsoList; | |
} | |
/** | |
* get list of common typo's mapped to official country names | |
* @return array | |
*/ | |
public static function getcommonCountryNametoOfficialCountryNameList() { | |
return self::$commonCountryNametoOfficialCountryNameList; | |
} | |
/** | |
* Get the list of valid Country Names | |
* @api | |
* @param \ServiceBase $api | |
* @param $args | |
* @return array | |
*/ | |
public function getCountriesList($api, $args) { | |
return array_keys($this->getofficialCountryNametoIsoList()); | |
} | |
/** | |
* Validate the passed-in name of a country, checking first for an official | |
* country name and then for an unofficial one | |
* | |
* @param string $country | |
* @return bool|string | |
*/ | |
public static function _validateCountryName($country) { | |
if (empty($country)) { | |
return FALSE; | |
} | |
/* | |
* if the country name received is valid, return it as it came in | |
*/ | |
$country_to_iso = self::getofficialCountryNametoIsoList(); | |
if (isset($country_to_iso[$country])) { | |
return $country; | |
} | |
/* | |
* if the country name is valid but the casing is off, correct the casing | |
*/ | |
foreach ($country_to_iso as $country_name => $iso_code) { | |
if (strtolower($country) == strtolower($country_name)) { | |
return $country_name; | |
} | |
if (strtolower($country) == strtolower($iso_code)) { | |
return $country_name; | |
} | |
} | |
/* | |
* if this is a valid ISO, return the associated country | |
*/ | |
if (in_array($country, $country_to_iso)) { | |
$flipped = array_flip($country_to_iso); | |
if (isset($flipped[$country])) { | |
return $flipped[$country]; | |
} | |
} | |
/* | |
* if the country name is a frequent convention or common typo, correct it | |
* to the official country name. For easier comparison, just set to | |
* lower-case | |
*/ | |
$typo_to_official = self::getcommonCountryNametoOfficialCountryNameList(); | |
foreach ($typo_to_official as $typo => $official) { | |
if (trim(strtolower($country)) == $typo) { | |
return $official; | |
} | |
} | |
/* | |
* check for metaphone for a sound-alike match, e.g. Chili => Chile | |
*/ | |
$metaphone = metaphone($country, 4); | |
foreach ($country_to_iso as $country_name => $iso_code) { | |
if (metaphone($country_name, 4) == $metaphone) { | |
return $country_name; | |
} | |
} | |
/* | |
* try really hard ... | |
*/ | |
$clean_country = self::try_harder_clean($country); | |
/* | |
* try-harder match with official list | |
*/ | |
foreach ($country_to_iso as $country_name => $iso_code) { | |
if ($clean_country == self::try_harder_clean($country_name)) { | |
return $country_name; | |
} | |
} | |
/* | |
* try-hard match with a common typo | |
*/ | |
foreach ($typo_to_official as $typo => $official) { | |
if ($clean_country == self::try_harder_clean($typo)) { | |
return $official; | |
} | |
} | |
return FALSE; | |
} | |
/** | |
* Scrub a country name to try *really* hard for a direct comparison... | |
* * trim | |
* * drop case | |
* * remove common words like "the" and "of" | |
* * remove non-alphabetic characters | |
* | |
* @param string $value | |
* @return string | |
*/ | |
private static function try_harder_clean($value) { | |
$value = trim(strtolower($value)); | |
$value = str_replace(' of ', '', $value); | |
$value = str_replace('the', '', $value); | |
$value = str_replace('republic', '', $value); | |
$value = str_replace('democratic', '', $value); | |
$value = str_replace(' ', '', $value); | |
$value = preg_replace("/[^A-Za-z]/", '', $value); | |
return $value; | |
} | |
/** | |
* @see CountryApi::_validateCountryName() | |
* @api | |
* @param \ServiceBase $api | |
* @param $args | |
* @return bool|string | |
*/ | |
public function validateCountryName($api, $args) { | |
return !empty($args['country']) | |
? self::_validateCountryName($args['country']) | |
: FALSE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment