diff --git a/src/Countries.php b/src/Countries.php index 0075051..6dc6089 100644 --- a/src/Countries.php +++ b/src/Countries.php @@ -332,7 +332,7 @@ public function name(string $code) : string } /** - * Checks whether the given country is in the EU + * Checks whether the given string is a country code in the EU * * @param string $code * @@ -344,6 +344,17 @@ public function inEurope(string $code) : bool return in_array($code, self::$eu); } + /** + * Checks whether the given string is a valid public IP address + * + * @param string $ipAddress + * @return bool + */ + public function validateIpAddress(string $ipAddress) : bool + { + return (bool) filter_var($ipAddress, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE); + } + /** * Gets the country code by IP address * @@ -355,7 +366,7 @@ public function inEurope(string $code) : bool */ public function ip(string $ipAddress) : string { - if (empty($ipAddress)) { + if (empty($ipAddress) || !$this->validateIpAddress($ipAddress)) { return ''; } diff --git a/src/Validator.php b/src/Validator.php index 10d03b6..799850d 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -52,10 +52,11 @@ class Validator { * * @param Vies\Client $client (optional) */ - public function __construct( Vies\Client $client = null ) { + public function __construct(Vies\Client $client = null) + { $this->client = $client; - if( ! $this->client ) { + if (! $this->client) { $this->client = new Vies\Client(); } } @@ -67,16 +68,17 @@ public function __construct( Vies\Client $client = null ) { * * @return boolean */ - public function validateFormat(string $vatNumber) : bool { - $vatNumber = strtoupper( $vatNumber ); - $country = substr( $vatNumber, 0, 2 ); - $number = substr( $vatNumber, 2 ); + public function validateFormat(string $vatNumber) : bool + { + $vatNumber = strtoupper($vatNumber); + $country = substr($vatNumber, 0, 2); + $number = substr($vatNumber, 2); - if( ! isset( self::$patterns[$country]) ) { + if( ! isset(self::$patterns[$country]) ) { return false; } - $matches = preg_match( '/^' . self::$patterns[$country] . '$/', $number ) > 0; + $matches = preg_match('/^' . self::$patterns[$country] . '$/', $number) > 0; return $matches; } @@ -88,10 +90,11 @@ public function validateFormat(string $vatNumber) : bool { * * @throws Vies\ViesException */ - public function validateExistence(string $vatNumber) : bool { - $vatNumber = strtoupper( $vatNumber ); - $country = substr( $vatNumber, 0, 2 ); - $number = substr( $vatNumber, 2 ); + public function validateExistence(string $vatNumber) : bool + { + $vatNumber = strtoupper( $vatNumber); + $country = substr($vatNumber, 0, 2); + $number = substr($vatNumber, 2); return $this->client->checkVat($country, $number); } @@ -104,7 +107,8 @@ public function validateExistence(string $vatNumber) : bool { * * @throws Vies\ViesException */ - public function validate(string $vatNumber) : bool { + public function validate(string $vatNumber) : bool + { return $this->validateFormat($vatNumber) && $this->validateExistence($vatNumber); } diff --git a/tests/CountriesTest.php b/tests/CountriesTest.php index 3e09fdc..8f91b2e 100644 --- a/tests/CountriesTest.php +++ b/tests/CountriesTest.php @@ -34,6 +34,23 @@ public function testInEurope() { } } + /** + * @covers Countries::validateIpAddress + */ + public function testValidateIpAddress() { + $countries = new Countries(); + $map = [ + 'foo' => false, + '192.168.1.10' => false, + '8.8.8.8' => true, + '54.18.12.111' => true, + ]; + + foreach($map as $ip => $expected) { + self::assertEquals($expected, $countries->validateIpAddress($ip)); + } + } + /** * @covers Countries::ip */