From 1612253f34bb2ae148d83c3aea1dbf3fad8e4666 Mon Sep 17 00:00:00 2001 From: Gian Luca Mutinelli Date: Tue, 3 Jul 2018 14:30:53 +0200 Subject: [PATCH 1/3] - Added non eu pattern array to validate class - Merge in __contruct() EU & non-EU array to check in validation - Added Swiss VAT regex - Added tests for Swiss regex - Added function to get VAT infos from Vies - Added VS Code subdir to .gitignore --- .gitignore | 2 ++ src/Validator.php | 22 ++++++++++++++++++---- src/Vies/Client.php | 17 ++++++++++++++++- tests/ValidatorTest.php | 2 ++ 4 files changed, 38 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 82cfc4e957..98c496b655 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ .idea composer.lock vendor +.vs/* + diff --git a/src/Validator.php b/src/Validator.php index 2b943b1d12..98b6bb9e9c 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -9,12 +9,12 @@ class Validator { /** - * Regular expression patterns per country code + * Regular expression patterns per EU country code * * @var array * @link http://ec.europa.eu/taxation_customs/vies/faq.html?locale=lt#item_11 */ - protected static $patterns = array( + protected static $eu_patterns = array( 'AT' => 'U[A-Z\d]{8}', 'BE' => '(0\d{9}|\d{10})', 'BG' => '\d{9,10}', @@ -45,12 +45,26 @@ class Validator { 'SK' => '\d{10}' ); + /** + * Regular expression patterns per NON EU country code + * + * @var array + * @link https://en.wikipedia.org/wiki/VAT_identification_number + */ + protected static $non_eu_patterns = array( + 'CH' => 'E\d{9}' + ); + + protected $patterns; + /** * VatValidator constructor. * * @param Vies\Client $client (optional) */ public function __construct( Vies\Client $client = null ) { + $this->patterns = array_merge(self::$eu_patterns,self::$non_eu_patterns); + $this->client = $client; if( ! $this->client ) { @@ -70,11 +84,11 @@ public function validateFormat( $vatNumber ) { $country = substr( $vatNumber, 0, 2 ); $number = substr( $vatNumber, 2 ); - if( ! isset( self::$patterns[$country]) ) { + if( ! isset( $this->patterns[$country]) ) { return false; } - $matches = preg_match( '/^' . self::$patterns[$country] . '$/', $number ) > 0; + $matches = preg_match( '/^' . $this->patterns[$country] . '$/', $number ) > 0; return $matches; } diff --git a/src/Vies/Client.php b/src/Vies/Client.php index c16869fb8a..da63c3c9a1 100644 --- a/src/Vies/Client.php +++ b/src/Vies/Client.php @@ -43,7 +43,22 @@ public function checkVat( $countryCode, $vatNumber ) { } catch( SoapFault $e ) { throw new ViesException( $e->getMessage(), $e->getCode() ); } - + return (bool) $response->valid; } + + public function getVatHolderInfo($countryCode, $vatNumber){ + try { + $response = $this->client->checkVat( + array( + 'countryCode' => $countryCode, + 'vatNumber' => $vatNumber + ) + ); + } catch( SoapFault $e ) { + throw new ViesException( $e->getMessage(), $e->getCode() ); + } + + return $response; + } } \ No newline at end of file diff --git a/tests/ValidatorTest.php b/tests/ValidatorTest.php index bdb37ee3fd..906d3a2913 100644 --- a/tests/ValidatorTest.php +++ b/tests/ValidatorTest.php @@ -51,6 +51,7 @@ public function test_validateFormat() { 'SE123456789012', 'SI12345678', 'SK1234567890', + 'CHE112233445', ]; $validator = new Validator(); @@ -90,6 +91,7 @@ public function test_validateFormat() { 'SI1234567', 'SK123456789', 'fooGB999999973', // valid VAT number but with string prefix + 'CHE11223344', ]; foreach( $invalid as $format ) { From 91dc8d8385f65fdb0d9299d235956bba0b65f4ea Mon Sep 17 00:00:00 2001 From: Gian Luca Mutinelli Date: Tue, 3 Jul 2018 14:48:20 +0200 Subject: [PATCH 2/3] - Added getPatterns function to check if ISO Code is alread present --- src/Validator.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Validator.php b/src/Validator.php index 98b6bb9e9c..33f15de4e5 100644 --- a/src/Validator.php +++ b/src/Validator.php @@ -118,5 +118,7 @@ public function validate( $vatNumber ) { return $this->validateFormat( $vatNumber ) && $this->validateExistence( $vatNumber ); } - + public function getPatterns() { + return $this->patterns; + } } \ No newline at end of file From 9f552037e8476c4ff5369579459430de8bbc5914 Mon Sep 17 00:00:00 2001 From: Gian Luca Mutinelli Date: Fri, 27 Jul 2018 12:24:37 +0200 Subject: [PATCH 3/3] - Added flag to throw exception or not --- src/Vies/Client.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Vies/Client.php b/src/Vies/Client.php index da63c3c9a1..e06327f9f1 100644 --- a/src/Vies/Client.php +++ b/src/Vies/Client.php @@ -47,7 +47,7 @@ public function checkVat( $countryCode, $vatNumber ) { return (bool) $response->valid; } - public function getVatHolderInfo($countryCode, $vatNumber){ + public function getVatHolderInfo($countryCode, $vatNumber, $throwExc = false){ try { $response = $this->client->checkVat( array( @@ -56,6 +56,9 @@ public function getVatHolderInfo($countryCode, $vatNumber){ ) ); } catch( SoapFault $e ) { + if (! $throwExc) + return null; + throw new ViesException( $e->getMessage(), $e->getCode() ); }