Skip to content

Commit

Permalink
add Countries::validateIpAddress method|
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Jan 8, 2019
1 parent e5092c5 commit 466df89
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 15 deletions.
15 changes: 13 additions & 2 deletions src/Countries.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand All @@ -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
*
Expand All @@ -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 '';
}

Expand Down
30 changes: 17 additions & 13 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand All @@ -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;
}

Expand All @@ -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);
}

Expand All @@ -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);
}

Expand Down
17 changes: 17 additions & 0 deletions tests/CountriesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 466df89

Please sign in to comment.