Skip to content
This repository has been archived by the owner on Oct 4, 2022. It is now read-only.

- Added non eu pattern array to validate class #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea
composer.lock
vendor
.vs/*

26 changes: 21 additions & 5 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}',
Expand Down Expand Up @@ -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 ) {
Expand All @@ -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;
}

Expand Down Expand Up @@ -104,5 +118,7 @@ public function validate( $vatNumber ) {
return $this->validateFormat( $vatNumber ) && $this->validateExistence( $vatNumber );
}


public function getPatterns() {
return $this->patterns;
}
}
20 changes: 19 additions & 1 deletion src/Vies/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 2 additions & 0 deletions tests/ValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function test_validateFormat() {
'SE123456789012',
'SI12345678',
'SK1234567890',
'CHE112233445',
];

$validator = new Validator();
Expand Down Expand Up @@ -90,6 +91,7 @@ public function test_validateFormat() {
'SI1234567',
'SK123456789',
'fooGB999999973', // valid VAT number but with string prefix
'CHE11223344',
];

foreach( $invalid as $format ) {
Expand Down