Created
June 17, 2022 07:49
-
-
Save sasin91/bc41f2b22121a4f4e1cefba112b5b66b to your computer and use it in GitHub Desktop.
extensible Enum with country names, alpha2, alpha3
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 | |
namespace App\Enums; | |
use ArchTech\Enums\From; | |
use function array_flip; | |
use ArchTech\Enums\Values; | |
use ArchTech\Enums\Options; | |
use Illuminate\Support\Str; | |
use function array_key_exists; | |
/** | |
* @src https://github.com/lukes/ISO-3166-Countries-with-Regional-Codes | |
*/ | |
enum Country: string | |
{ | |
use Values, From, Options; | |
case DNK = 'Denmark'; | |
case SWE = 'Sweden'; | |
case IRQ = 'Iraq'; | |
case AFG = 'Afghanistan'; | |
case ALA = 'Åland Islands'; | |
case ALB = 'Albania'; | |
case DZA = 'Algeria'; | |
case ASM = 'American Samoa'; | |
case AND = 'Andorra'; | |
case ANT = 'Netherlands Antilles'; | |
case AGO = 'Angola'; | |
case AIA = 'Anguilla'; | |
case ATA = 'Antarctica'; | |
case ATG = 'Antigua and Barbuda'; | |
case ARG = 'Argentina'; | |
case ARM = 'Armenia'; | |
case ABW = 'Aruba'; | |
case AUS = 'Australia'; | |
case AUT = 'Austria'; | |
case AZE = 'Azerbaijan'; | |
case BHS = 'Bahamas'; | |
case BHR = 'Bahrain'; | |
case BGD = 'Bangladesh'; | |
case BRB = 'Barbados'; | |
case BLR = 'Belarus'; | |
case BEL = 'Belgium'; | |
case BLZ = 'Belize'; | |
case BEN = 'Benin'; | |
case BMU = 'Bermuda'; | |
case BTN = 'Bhutan'; | |
case BOL = 'Bolivia (Plurinational State of)'; | |
case BES = 'Bonaire, Sint Eustatius and Saba'; | |
case BIH = 'Bosnia and Herzegovina'; | |
case BWA = 'Botswana'; | |
case BVT = 'Bouvet Island'; | |
case BRA = 'Brazil'; | |
case IOT = 'British Indian Ocean Territory'; | |
case BRN = 'Brunei Darussalam'; | |
case BGR = 'Bulgaria'; | |
case BFA = 'Burkina Faso'; | |
case BDI = 'Burundi'; | |
case KHM = 'Cambodia'; | |
case CMR = 'Cameroon'; | |
case CAN = 'Canada'; | |
case CPV = 'Cabo Verde'; | |
case CYM = 'Cayman Islands'; | |
case CAF = 'Central African Republic'; | |
case TCD = 'Chad'; | |
case CHL = 'Chile'; | |
case CHN = 'China'; | |
case CXR = 'Christmas Island'; | |
case CCK = 'Cocos (Keeling) Islands'; | |
case COL = 'Colombia'; | |
case COM = 'Comoros'; | |
case COG = 'Congo'; | |
case COD = 'Congo, Democratic Republic of the'; | |
case COK = 'Cook Islands'; | |
case CRI = 'Costa Rica'; | |
case CIV = 'Côte d\'Ivoire'; | |
case HRV = 'Croatia'; | |
case CUB = 'Cuba'; | |
case CYP = 'Cyprus'; | |
case CZE = 'Czechia'; | |
case DJI = 'Djibouti'; | |
case DMA = 'Dominica'; | |
case DOM = 'Dominican Republic'; | |
case ECU = 'Ecuador'; | |
case EGY = 'Egypt'; | |
case SLV = 'El Salvador'; | |
case GNQ = 'Equatorial Guinea'; | |
case ERI = 'Eritrea'; | |
case EST = 'Estonia'; | |
case ETH = 'Ethiopia'; | |
case FLK = 'Falkland Islands (Malvinas)'; | |
case FRO = 'Faroe Islands'; | |
case FJI = 'Fiji'; | |
case FIN = 'Finland'; | |
case FRA = 'France'; | |
case GUF = 'French Guiana'; | |
case PYF = 'French Polynesia'; | |
case ATF = 'French Southern Territories'; | |
case GAB = 'Gabon'; | |
case GMB = 'Gambia'; | |
case GEO = 'Georgia'; | |
case DEU = 'Germany'; | |
case GHA = 'Ghana'; | |
case GIB = 'Gibraltar'; | |
case GRC = 'Greece'; | |
case GRL = 'Greenland'; | |
case GRD = 'Grenada'; | |
case GLP = 'Guadeloupe'; | |
case GUM = 'Guam'; | |
case GTM = 'Guatemala'; | |
case GGY = 'Guernsey'; | |
case GIN = 'Guinea'; | |
case GNB = 'Guinea-Bissau'; | |
case GUY = 'Guyana'; | |
case HTI = 'Haiti'; | |
case HMD = 'Heard Island and McDonald Islands'; | |
case VAT = 'Holy See'; | |
case HND = 'Honduras'; | |
case HKG = 'Hong Kong'; | |
case HUN = 'Hungary'; | |
case ISL = 'Iceland'; | |
case IND = 'India'; | |
case IDN = 'Indonesia'; | |
case IRN = 'Iran (Islamic Republic of)'; | |
case IRL = 'Ireland'; | |
case IMN = 'Isle of Man'; | |
case ISR = 'Israel'; | |
case ITA = 'Italy'; | |
case JAM = 'Jamaica'; | |
case JPN = 'Japan'; | |
case JEY = 'Jersey'; | |
case JOR = 'Jordan'; | |
case KAZ = 'Kazakhstan'; | |
case KEN = 'Kenya'; | |
case KIR = 'Kiribati'; | |
case PRK = "Korea (Democratic People's Republic of)"; | |
case KOR = 'Korea, Republic of'; | |
case KWT = 'Kuwait'; | |
case KGZ = 'Kyrgyzstan'; | |
case LAO = "Lao People's Democratic Republic"; | |
case LVA = 'Latvia'; | |
case LBN = 'Lebanon'; | |
case LSO = 'Lesotho'; | |
case LBR = 'Liberia'; | |
case LBY = 'Libya'; | |
case LIE = 'Liechtenstein'; | |
case LTU = 'Lithuania'; | |
case LUX = 'Luxembourg'; | |
case MAC = 'Macao'; | |
case MKD = 'North Macedonia'; | |
case MDG = 'Madagascar'; | |
case MWI = 'Malawi'; | |
case MDV = 'Maldives'; | |
case MLI = 'Mali'; | |
case MLT = 'Malta'; | |
case MHL = 'Marshall Islands'; | |
case MTQ = 'Martinique'; | |
case MRT = 'Mauritania'; | |
case MUS = 'Mauritius'; | |
case MYT = 'Mayotte'; | |
case MEX = 'Mexico'; | |
case FSM = 'Micronesia (Federated States of)'; | |
case MDA = 'Moldova, Republic of'; | |
case MCO = 'Monaco'; | |
case MNG = 'Mongolia'; | |
case MNE = 'Montenegro'; | |
case MSR = 'Montserrat'; | |
case MAR = 'Morocco'; | |
case MOZ = 'Mozambique'; | |
case MMR = 'Myanmar'; | |
case NAM = 'Namibia'; | |
case NRU = 'Nauru'; | |
case NPL = 'Nepal'; | |
case NLD = 'Netherlands'; | |
case NCL = 'New Caledonia'; | |
case NZL = 'New Zealand'; | |
case NIC = 'Nicaragua'; | |
case NER = 'Niger'; | |
case NGA = 'Nigeria'; | |
case NIU = 'Niue'; | |
case NFK = 'Norfolk Island'; | |
case MNP = 'Northern Mariana Islands'; | |
case NOR = 'Norway'; | |
case OMN = 'Oman'; | |
case PAK = 'Pakistan'; | |
case PLW = 'Palau'; | |
case PAN = 'Panama'; | |
case PNG = 'Papua New Guinea'; | |
case PRY = 'Paraguay'; | |
case PER = 'Peru'; | |
case PHL = 'Philippines'; | |
case PCN = 'Pitcairn'; | |
case POL = 'Poland'; | |
case PRT = 'Portugal'; | |
case PRI = 'Puerto Rico'; | |
case QAT = 'Qatar'; | |
case ROU = 'Romania'; | |
case RUS = 'Russian Federation'; | |
case RWA = 'Rwanda'; | |
case WSM = 'Samoa'; | |
case SMR = 'San Marino'; | |
case STP = 'Sao Tome and Principe'; | |
case SAU = 'Saudi Arabia'; | |
case SEN = 'Senegal'; | |
case SRB = 'Serbia'; | |
case SYC = 'Seychelles'; | |
case SLE = 'Sierra Leone'; | |
case SGP = 'Singapore'; | |
case SVK = 'Slovakia'; | |
case SVN = 'Slovenia'; | |
case SLB = 'Solomon Islands'; | |
case SOM = 'Somalia'; | |
case ZAF = 'South Africa'; | |
case SGS = 'South Georgia and the South Sandwich Islands'; | |
case ESP = 'Spain'; | |
case LKA = 'Sri Lanka'; | |
case SDN = 'Sudan'; | |
case SSD = 'South Sudan'; | |
case SUR = 'Suriname'; | |
case SJM = 'Svalbard and Jan Mayen'; | |
case SWZ = 'Eswatini'; | |
case CHE = 'Switzerland'; | |
case SYR = 'Syrian Arab Republic'; | |
case TWN = 'Taiwan, Province of China'; | |
case TJK = 'Tajikistan'; | |
case TZA = 'Tanzania, United Republic of'; | |
case THA = 'Thailand'; | |
case TLS = 'Timor-Leste'; | |
case TGO = 'Togo'; | |
case TKL = 'Tokelau'; | |
case TON = 'Tonga'; | |
case TTO = 'Trinidad and Tobago'; | |
case TUN = 'Tunisia'; | |
case TUR = 'Turkey'; | |
case TKM = 'Turkmenistan'; | |
case TCA = 'Turks and Caicos Islands'; | |
case TUV = 'Tuvalu'; | |
case UGA = 'Uganda'; | |
case UKR = 'Ukraine'; | |
case ARE = 'United Arab Emirates'; | |
case GBR = 'United Kingdom of Great Britain and Northern Ireland'; | |
case USA = 'United States of America'; | |
case UMI = 'United States Minor Outlying Islands'; | |
case URY = 'Uruguay'; | |
case UZB = 'Uzbekistan'; | |
case VUT = 'Vanuatu'; | |
case VEN = 'Venezuela (Bolivarian Republic of)'; | |
case VNM = 'Viet Nam'; | |
case VGB = 'Virgin Islands (British)'; | |
case VIR = 'Virgin Islands (U.S.)'; | |
case WLF = 'Wallis and Futuna'; | |
case ESH = 'Western Sahara'; | |
case YEM = 'Yemen'; | |
case ZMB = 'Zambia'; | |
case ZWE = 'Zimbabwe'; | |
case KUL = 'Malaysia'; | |
case PSE = 'Palestine, State of'; | |
case CUW = 'Curaçao'; | |
case REU = 'Réunion'; | |
case BLM = 'Saint Barthélemy'; | |
case SHN = 'Saint Helena, Ascension and Tristan da Cunha'; | |
case KNA = 'Saint Kitts and Nevis'; | |
case LCA = 'Saint Lucia'; | |
case MAF = 'Saint Martin (French part)'; | |
case SPM = 'Saint Pierre and Miquelon'; | |
case VCT = 'Saint Vincent and the Grenadines'; | |
case SXM = 'Sint Maarten (Dutch part)'; | |
case UNK = 'Unknown'; | |
public static function make(null|string $value): static | |
{ | |
if (!$value) { | |
return static::UNK; | |
} | |
return with( | |
Str::upper($value), | |
static function (string $uppercased) use ($value): Country { | |
if (self::isAlpha2($uppercased)) { | |
return self::fromAlpha2($uppercased); | |
} | |
return static::tryFromName($uppercased) ?? static::tryFrom($uppercased) ?? static::from($value); | |
} | |
); | |
} | |
// public static function isAlpha3(string $value): bool | |
// { | |
// return array_key_exists($value, self::alpha3Map()); | |
// } | |
// public static function fromAlpha3(string $value): Country | |
// { | |
// return self::alpha3Map()[$value]; | |
// } | |
// public static function alpha3Map(): array | |
// { | |
// return collect( | |
// static::cases() | |
// )->mapWithKeys(static function (Country $case): array { | |
// return [ | |
// $case->name => $case | |
// ]; | |
// })->all(); | |
// } | |
public static function fromAlpha2(string $value): Country | |
{ | |
return self::tryFrom(self::alpha2Map()[$value]); | |
} | |
public static function isAlpha2(string $value): bool | |
{ | |
return array_key_exists($value, self::alpha2Map()); | |
} | |
/** | |
* Get the alpha2 value of the current country | |
* | |
* @return string | |
*/ | |
public function alpha2(): string | |
{ | |
return array_flip(self::alpha2Map())[$this->value]; | |
} | |
/** | |
* Get the alpha3 aka. the key of this enum | |
* | |
* @return string | |
*/ | |
public function alpha3(): string | |
{ | |
return $this->name; | |
} | |
/** | |
* Mappings from alpha2 to alpha3 which corresponds to a Country instance | |
* | |
* @return array | |
*/ | |
public static function alpha2Map(): array | |
{ | |
return [ | |
"AF" => self::AFG->value, | |
"AX" => self::ALA->value, | |
"AL" => self::ALB->value, | |
"DZ" => self::DZA->value, | |
"AS" => self::ASM->value, | |
"AD" => self::AND->value, | |
"AO" => self::AGO->value, | |
"AI" => self::AIA->value, | |
"AQ" => self::ATA->value, | |
"AG" => self::ATG->value, | |
"AR" => self::ARG->value, | |
"AM" => self::ARM->value, | |
"AW" => self::ABW->value, | |
"AU" => self::AUS->value, | |
"AT" => self::AUT->value, | |
"AZ" => self::AZE->value, | |
"BS" => self::BHS->value, | |
"BH" => self::BHR->value, | |
"BD" => self::BGD->value, | |
"BB" => self::BRB->value, | |
"BY" => self::BLR->value, | |
"BE" => self::BEL->value, | |
"BZ" => self::BLZ->value, | |
"BJ" => self::BEN->value, | |
"BM" => self::BMU->value, | |
"BT" => self::BTN->value, | |
"BO" => self::BOL->value, | |
"BQ" => self::BES->value, | |
"BA" => self::BIH->value, | |
"BW" => self::BWA->value, | |
"BV" => self::BVT->value, | |
"BR" => self::BRA->value, | |
"IO" => self::IOT->value, | |
"BN" => self::BRN->value, | |
"BG" => self::BGR->value, | |
"BF" => self::BFA->value, | |
"BI" => self::BDI->value, | |
"CV" => self::CPV->value, | |
"KH" => self::KHM->value, | |
"CM" => self::CMR->value, | |
"CA" => self::CAN->value, | |
"KY" => self::CYM->value, | |
"CF" => self::CAF->value, | |
"TD" => self::TCD->value, | |
"CL" => self::CHL->value, | |
"CN" => self::CHN->value, | |
"CX" => self::CXR->value, | |
"CC" => self::CCK->value, | |
"CO" => self::COL->value, | |
"KM" => self::COM->value, | |
"CG" => self::COG->value, | |
"CD" => self::COD->value, | |
"CK" => self::COK->value, | |
"CR" => self::CRI->value, | |
"CI" => self::CIV->value, | |
"HR" => self::HRV->value, | |
"CU" => self::CUB->value, | |
"CW" => self::CUW->value, | |
"CY" => self::CYP->value, | |
"CZ" => self::CZE->value, | |
"DK" => self::DNK->value, | |
"DJ" => self::DJI->value, | |
"DM" => self::DMA->value, | |
"DO" => self::DOM->value, | |
"EC" => self::ECU->value, | |
"EG" => self::EGY->value, | |
"SV" => self::SLV->value, | |
"GQ" => self::GNQ->value, | |
"ER" => self::ERI->value, | |
"EE" => self::EST->value, | |
"SZ" => self::SWZ->value, | |
"ET" => self::ETH->value, | |
"FK" => self::FLK->value, | |
"FO" => self::FRO->value, | |
"FJ" => self::FJI->value, | |
"FI" => self::FIN->value, | |
"FR" => self::FRA->value, | |
"GF" => self::GUF->value, | |
"PF" => self::PYF->value, | |
"TF" => self::ATF->value, | |
"GA" => self::GAB->value, | |
"GM" => self::GMB->value, | |
"GE" => self::GEO->value, | |
"DE" => self::DEU->value, | |
"GH" => self::GHA->value, | |
"GI" => self::GIB->value, | |
"GR" => self::GRC->value, | |
"GL" => self::GRL->value, | |
"GD" => self::GRD->value, | |
"GP" => self::GLP->value, | |
"GU" => self::GUM->value, | |
"GT" => self::GTM->value, | |
"GG" => self::GGY->value, | |
"GN" => self::GIN->value, | |
"GW" => self::GNB->value, | |
"GY" => self::GUY->value, | |
"HT" => self::HTI->value, | |
"HM" => self::HMD->value, | |
"VA" => self::VAT->value, | |
"HN" => self::HND->value, | |
"HK" => self::HKG->value, | |
"HU" => self::HUN->value, | |
"IS" => self::ISL->value, | |
"IN" => self::IND->value, | |
"ID" => self::IDN->value, | |
"IR" => self::IRN->value, | |
"IQ" => self::IRQ->value, | |
"IE" => self::IRL->value, | |
"IM" => self::IMN->value, | |
"IL" => self::ISR->value, | |
"IT" => self::ITA->value, | |
"JM" => self::JAM->value, | |
"JP" => self::JPN->value, | |
"JE" => self::JEY->value, | |
"JO" => self::JOR->value, | |
"KZ" => self::KAZ->value, | |
"KE" => self::KEN->value, | |
"KI" => self::KIR->value, | |
"KP" => self::PRK->value, | |
"KR" => self::KOR->value, | |
"KW" => self::KWT->value, | |
"KG" => self::KGZ->value, | |
"LA" => self::LAO->value, | |
"LV" => self::LVA->value, | |
"LB" => self::LBN->value, | |
"LS" => self::LSO->value, | |
"LR" => self::LBR->value, | |
"LY" => self::LBY->value, | |
"LI" => self::LIE->value, | |
"LT" => self::LTU->value, | |
"LU" => self::LUX->value, | |
"MO" => self::MAC->value, | |
"MG" => self::MDG->value, | |
"MW" => self::MWI->value, | |
"MY" => self::KUL->value, | |
"MV" => self::MDV->value, | |
"ML" => self::MLI->value, | |
"MT" => self::MLT->value, | |
"MH" => self::MHL->value, | |
"MQ" => self::MTQ->value, | |
"MR" => self::MRT->value, | |
"MU" => self::MUS->value, | |
"YT" => self::MYT->value, | |
"MX" => self::MEX->value, | |
"FM" => self::FSM->value, | |
"MD" => self::MDA->value, | |
"MC" => self::MCO->value, | |
"MN" => self::MNG->value, | |
"ME" => self::MNE->value, | |
"MS" => self::MSR->value, | |
"MA" => self::MAR->value, | |
"MZ" => self::MOZ->value, | |
"MM" => self::MMR->value, | |
"NA" => self::NAM->value, | |
"NR" => self::NRU->value, | |
"NP" => self::NPL->value, | |
"NL" => self::NLD->value, | |
"NC" => self::NCL->value, | |
"NZ" => self::NZL->value, | |
"NI" => self::NIC->value, | |
"NE" => self::NER->value, | |
"NG" => self::NGA->value, | |
"NU" => self::NIU->value, | |
"NF" => self::NFK->value, | |
"MK" => self::MKD->value, | |
"MP" => self::MNP->value, | |
"NO" => self::NOR->value, | |
"OM" => self::OMN->value, | |
"PK" => self::PAK->value, | |
"PW" => self::PLW->value, | |
"PS" => self::PSE->value, | |
"PA" => self::PAN->value, | |
"PG" => self::PNG->value, | |
"PY" => self::PRY->value, | |
"PE" => self::PER->value, | |
"PH" => self::PHL->value, | |
"PN" => self::PCN->value, | |
"PL" => self::POL->value, | |
"PT" => self::PRT->value, | |
"PR" => self::PRI->value, | |
"QA" => self::QAT->value, | |
"RE" => self::REU->value, | |
"RO" => self::ROU->value, | |
"RU" => self::RUS->value, | |
"RW" => self::RWA->value, | |
"BL" => self::BLM->value, | |
"SH" => self::SHN->value, | |
"KN" => self::KNA->value, | |
"LC" => self::LCA->value, | |
"MF" => self::MAF->value, | |
"PM" => self::SPM->value, | |
"VC" => self::VCT->value, | |
"WS" => self::WSM->value, | |
"SM" => self::SMR->value, | |
"ST" => self::STP->value, | |
"SA" => self::SAU->value, | |
"SN" => self::SEN->value, | |
"RS" => self::SRB->value, | |
"SC" => self::SYC->value, | |
"SL" => self::SLE->value, | |
"SG" => self::SGP->value, | |
"SX" => self::SXM->value, | |
"SK" => self::SVK->value, | |
"SI" => self::SVN->value, | |
"SB" => self::SLB->value, | |
"SO" => self::SOM->value, | |
"ZA" => self::ZAF->value, | |
"GS" => self::SGS->value, | |
"SS" => self::SSD->value, | |
"ES" => self::ESP->value, | |
"LK" => self::LKA->value, | |
"SD" => self::SDN->value, | |
"SR" => self::SUR->value, | |
"SJ" => self::SJM->value, | |
"SE" => self::SWE->value, | |
"CH" => self::CHE->value, | |
"SY" => self::SYR->value, | |
"TW" => self::TWN->value, | |
"TJ" => self::TJK->value, | |
"TZ" => self::TZA->value, | |
"TH" => self::THA->value, | |
"TL" => self::TLS->value, | |
"TG" => self::TGO->value, | |
"TK" => self::TKL->value, | |
"TO" => self::TON->value, | |
"TT" => self::TTO->value, | |
"TN" => self::TUN->value, | |
"TR" => self::TUR->value, | |
"TM" => self::TKM->value, | |
"TC" => self::TCA->value, | |
"TV" => self::TUV->value, | |
"UG" => self::UGA->value, | |
"UA" => self::UKR->value, | |
"AE" => self::ARE->value, | |
"GB" => self::GBR->value, | |
"US" => self::USA->value, | |
"UM" => self::UMI->value, | |
"UY" => self::URY->value, | |
"UZ" => self::UZB->value, | |
"VU" => self::VUT->value, | |
"VE" => self::VEN->value, | |
"VN" => self::VNM->value, | |
"VG" => self::VGB->value, | |
"VI" => self::VIR->value, | |
"WF" => self::WLF->value, | |
"EH" => self::ESH->value, | |
"YE" => self::YEM->value, | |
"ZM" => self::ZMB->value, | |
"ZW" => self::ZWE->value, | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment