-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #181 from g3rd0/isco3166alpha2
Add ISO 3166 Alpha 2 Validation
- Loading branch information
Showing
2 changed files
with
310 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,275 @@ | ||
<?php | ||
|
||
namespace IsoCodes; | ||
|
||
/** | ||
* Validate Country Codes in ISO 3166 Alpha 2 format | ||
*/ | ||
class ISO3166A2 implements IsoCodeInterface | ||
{ | ||
private CONST COUNTRIES = ['AD', | ||
'AE', | ||
'AF', | ||
'AG', | ||
'AI', | ||
'AL', | ||
'AM', | ||
'AN', | ||
'AO', | ||
'AQ', | ||
'AR', | ||
'AS', | ||
'AT', | ||
'AU', | ||
'AW', | ||
'AX', | ||
'AZ', | ||
'BA', | ||
'BB', | ||
'BD', | ||
'BE', | ||
'BF', | ||
'BG', | ||
'BH', | ||
'BI', | ||
'BJ', | ||
'BL', | ||
'BM', | ||
'BN', | ||
'BO', | ||
'BQ', | ||
'BR', | ||
'BS', | ||
'BT', | ||
'BV', | ||
'BW', | ||
'BY', | ||
'BZ', | ||
'CA', | ||
'CC', | ||
'CD', | ||
'CF', | ||
'CG', | ||
'CH', | ||
'CI', | ||
'CK', | ||
'CL', | ||
'CM', | ||
'CN', | ||
'CO', | ||
'CR', | ||
'CS', | ||
'CU', | ||
'CV', | ||
'CW', | ||
'CX', | ||
'CY', | ||
'CZ', | ||
'DE', | ||
'DJ', | ||
'DK', | ||
'DM', | ||
'DO', | ||
'DZ', | ||
'EC', | ||
'EE', | ||
'EG', | ||
'EH', | ||
'ER', | ||
'ES', | ||
'ET', | ||
'FI', | ||
'FJ', | ||
'FK', | ||
'FM', | ||
'FO', | ||
'FR', | ||
'GA', | ||
'GB', | ||
'GD', | ||
'GE', | ||
'GF', | ||
'GG', | ||
'GH', | ||
'GI', | ||
'GL', | ||
'GM', | ||
'GN', | ||
'GP', | ||
'GQ', | ||
'GR', | ||
'GS', | ||
'GT', | ||
'GU', | ||
'GW', | ||
'GY', | ||
'HK', | ||
'HM', | ||
'HN', | ||
'HR', | ||
'HT', | ||
'HU', | ||
'ID', | ||
'IE', | ||
'IL', | ||
'IM', | ||
'IN', | ||
'IO', | ||
'IQ', | ||
'IR', | ||
'IS', | ||
'IT', | ||
'JE', | ||
'JM', | ||
'JO', | ||
'JP', | ||
'KE', | ||
'KG', | ||
'KH', | ||
'KI', | ||
'KM', | ||
'KN', | ||
'KP', | ||
'KR', | ||
'KW', | ||
'KY', | ||
'KZ', | ||
'LA', | ||
'LB', | ||
'LC', | ||
'LI', | ||
'LK', | ||
'LR', | ||
'LS', | ||
'LT', | ||
'LU', | ||
'LV', | ||
'LY', | ||
'MA', | ||
'MC', | ||
'MD', | ||
'ME', | ||
'MF', | ||
'MG', | ||
'MH', | ||
'MK', | ||
'ML', | ||
'MM', | ||
'MN', | ||
'MO', | ||
'MP', | ||
'MQ', | ||
'MR', | ||
'MS', | ||
'MT', | ||
'MU', | ||
'MV', | ||
'MW', | ||
'MX', | ||
'MY', | ||
'MZ', | ||
'NA', | ||
'NC', | ||
'NE', | ||
'NF', | ||
'NG', | ||
'NI', | ||
'NL', | ||
'NO', | ||
'NP', | ||
'NR', | ||
'NU', | ||
'NZ', | ||
'OM', | ||
'PA', | ||
'PE', | ||
'PF', | ||
'PG', | ||
'PH', | ||
'PK', | ||
'PL', | ||
'PM', | ||
'PN', | ||
'PR', | ||
'PS', | ||
'PT', | ||
'PW', | ||
'PY', | ||
'QA', | ||
'RE', | ||
'RO', | ||
'RS', | ||
'RU', | ||
'RW', | ||
'SA', | ||
'SB', | ||
'SC', | ||
'SD', | ||
'SE', | ||
'SG', | ||
'SH', | ||
'SI', | ||
'SJ', | ||
'SK', | ||
'SL', | ||
'SM', | ||
'SN', | ||
'SO', | ||
'SR', | ||
'SS', | ||
'ST', | ||
'SV', | ||
'SX', | ||
'SY', | ||
'SZ', | ||
'TC', | ||
'TD', | ||
'TF', | ||
'TG', | ||
'TH', | ||
'TJ', | ||
'TK', | ||
'TL', | ||
'TM', | ||
'TN', | ||
'TO', | ||
'TR', | ||
'TT', | ||
'TV', | ||
'TW', | ||
'TZ', | ||
'UA', | ||
'UG', | ||
'UM', | ||
'US', | ||
'UY', | ||
'UZ', | ||
'VA', | ||
'VC', | ||
'VE', | ||
'VG', | ||
'VI', | ||
'VN', | ||
'VU', | ||
'WF', | ||
'WS', | ||
'XK', | ||
'YE', | ||
'YT', | ||
'ZA', | ||
'ZM', | ||
'ZW' | ||
]; | ||
|
||
/** | ||
* @param string $alpha2 | ||
* @return bool | ||
*/ | ||
public static function validate($alpha2) | ||
{ | ||
if($alpha2 === null){ | ||
return false; | ||
} | ||
return in_array($alpha2, self::COUNTRIES, true ); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace IsoCodes\Tests; | ||
|
||
/** | ||
* Class ISO3166A2. | ||
* | ||
* @covers \IsoCodes\ISO3166A2 | ||
*/ | ||
class ISO3166A2Test extends AbstractIsoCodeTest | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getValidValues() | ||
{ | ||
return [ | ||
['US'], | ||
['DE'], | ||
['NL'] | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getInvalidValues() | ||
{ | ||
return [ | ||
['ZZ'], // Value doesn't exist | ||
['ZZZ'], // Value too long | ||
[123] // No string | ||
]; | ||
} | ||
} |