Skip to content

Commit

Permalink
add Countries::validateCountryCode method
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Jan 8, 2019
1 parent 466df89 commit 4b79a79
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/Countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
class Countries {

/**
* @var array List of ISO-3166-1-alpha2 country-codes + names
*/
private static $all = [
'AF' => 'Afghanistan',
'AX' => 'Aland Islands',
Expand Down Expand Up @@ -292,7 +295,7 @@ class Countries {
];

/**
* Get all countries in code => name format
* Get all countries in ISO-3166-1-alpha2 country code => name format
*
* @return array
*/
Expand Down Expand Up @@ -344,8 +347,21 @@ public function inEurope(string $code) : bool
return in_array($code, self::$eu);
}


/**
* Checks whether the given string is a valid public IPv4 or IPv6 address
*
* @param string $countryCode
* @return bool
*/
public function validateCountryCode(string $countryCode) : bool
{
return isset(self::$all[$countryCode]);
}


/**
* Checks whether the given string is a valid public IP address
* Checks whether the given string is a valid public IPv4 or IPv6 address
*
* @param string $ipAddress
* @return bool
Expand Down
17 changes: 17 additions & 0 deletions tests/CountriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ public function testValidateIpAddress() {
}
}

/**
* @covers Countries::validateCountryCode
*/
public function testValidateCountryCode() {
$countries = new Countries();
$map = [
'foo' => false,
'' => false,
'NL' => true,
'US' => true,
];

foreach($map as $input => $expected) {
self::assertEquals($expected, $countries->validateCountryCode($input));
}
}

/**
* @covers Countries::ip
*/
Expand Down

0 comments on commit 4b79a79

Please sign in to comment.