From 4b79a7900ffc34c797c461c89e894fd99e57dadf Mon Sep 17 00:00:00 2001 From: Danny van Kooten Date: Tue, 8 Jan 2019 10:20:01 +0100 Subject: [PATCH] add Countries::validateCountryCode method --- src/Countries.php | 20 ++++++++++++++++++-- tests/CountriesTest.php | 17 +++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Countries.php b/src/Countries.php index 6dc6089..fbfd5e4 100644 --- a/src/Countries.php +++ b/src/Countries.php @@ -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', @@ -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 */ @@ -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 diff --git a/tests/CountriesTest.php b/tests/CountriesTest.php index 8f91b2e..6773bcf 100644 --- a/tests/CountriesTest.php +++ b/tests/CountriesTest.php @@ -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 */