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..33f15de4e5 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; } @@ -104,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 diff --git a/src/Vies/Client.php b/src/Vies/Client.php index c16869fb8a..e06327f9f1 100644 --- a/src/Vies/Client.php +++ b/src/Vies/Client.php @@ -43,7 +43,25 @@ public function checkVat( $countryCode, $vatNumber ) { } catch( SoapFault $e ) { throw new ViesException( $e->getMessage(), $e->getCode() ); } - + return (bool) $response->valid; } + + public function getVatHolderInfo($countryCode, $vatNumber, $throwExc = false){ + try { + $response = $this->client->checkVat( + array( + 'countryCode' => $countryCode, + 'vatNumber' => $vatNumber + ) + ); + } catch( SoapFault $e ) { + if (! $throwExc) + return null; + + 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 ) {