|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ddeboer\Vatin; |
|
4
|
|
|
|
|
5
|
|
|
use Ddeboer\Vatin\Vies\Client; |
|
6
|
|
|
use Ddeboer\Vatin\Exception\ViesException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Validate a VAT identification number (VATIN) |
|
10
|
|
|
* |
|
11
|
|
|
* @link http://en.wikipedia.org/wiki/VAT_identification_number |
|
12
|
|
|
* @link http://sima.cat/nif.php |
|
13
|
|
|
* @link https://github.com/jonathanmaron/zf2_proposal/blob/master/library/Zend/Validator/Vatin.php |
|
14
|
|
|
*/ |
|
15
|
|
|
class Validator |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* Regular expression patterns per country code |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
* @link http://ec.europa.eu/taxation_customs/vies/faq.html?locale=lt#item_11 |
|
22
|
|
|
*/ |
|
23
|
|
|
private $patterns = array( |
|
24
|
|
|
'AT' => 'U[A-Z\d]{8}', |
|
25
|
|
|
'BE' => '[0|1]{1}\d{9}', |
|
26
|
|
|
'BG' => '\d{9,10}', |
|
27
|
|
|
'CY' => '\d{8}[A-Z]', |
|
28
|
|
|
'CZ' => '\d{8,10}', |
|
29
|
|
|
'DE' => '\d{9}', |
|
30
|
|
|
'DK' => '(\d{2} ?){3}\d{2}', |
|
31
|
|
|
'EE' => '\d{9}', |
|
32
|
|
|
'EL' => '\d{9}', |
|
33
|
|
|
'ES' => '[A-Z]\d{7}[A-Z]|\d{8}[A-Z]|[A-Z]\d{8}', |
|
34
|
|
|
'FI' => '\d{8}', |
|
35
|
|
|
'FR' => '([A-Z0-9]{2})\d{9}', |
|
36
|
|
|
'GB' => '\d{9}|\d{12}|(GD|HA)\d{3}', |
|
37
|
|
|
'HR' => '\d{11}', |
|
38
|
|
|
'HU' => '\d{8}', |
|
39
|
|
|
'IE' => '[A-Z\d]{8}|[A-Z\d]{9}', |
|
40
|
|
|
'IT' => '\d{11}', |
|
41
|
|
|
'LT' => '(\d{9}|\d{12})', |
|
42
|
|
|
'LU' => '\d{8}', |
|
43
|
|
|
'LV' => '\d{11}', |
|
44
|
|
|
'MT' => '\d{8}', |
|
45
|
|
|
'NL' => '\d{9}B\d{2}', |
|
46
|
|
|
'PL' => '\d{10}', |
|
47
|
|
|
'PT' => '\d{9}', |
|
48
|
|
|
'RO' => '\d{2,10}', |
|
49
|
|
|
'SE' => '\d{12}', |
|
50
|
|
|
'SI' => '\d{8}', |
|
51
|
|
|
'SK' => '\d{10}' |
|
52
|
|
|
); |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Client for the VIES web service |
|
56
|
|
|
* |
|
57
|
|
|
* @var Client |
|
58
|
|
|
*/ |
|
59
|
|
|
private $viesClient; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Constructor |
|
63
|
|
|
* |
|
64
|
|
|
* @param Client $viesClient Client for the VIES web service |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct(Client $viesClient) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->viesClient = $viesClient; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns true if value is a valid VAT identification number, false |
|
73
|
|
|
* otherwise |
|
74
|
|
|
* |
|
75
|
|
|
* @param string $value Value |
|
76
|
|
|
* @param bool $checkExistence In addition to checking the VATIN's format |
|
77
|
|
|
* for validity, also check whether the VATIN |
|
78
|
|
|
* exists. This requires a call to the VIES |
|
79
|
|
|
* web service. |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
public function isValid($value, $checkExistence = false) |
|
84
|
|
|
{ |
|
85
|
|
|
if (null === $value || '' === $value) { |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$countryCode = substr($value, 0, 2); |
|
90
|
|
|
$vatin = substr($value, 2); |
|
91
|
|
|
|
|
92
|
|
|
if (false === $this->isValidCountryCode($countryCode)) { |
|
93
|
|
|
return false; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
if (0 === preg_match('/^'.$this->patterns[$countryCode].'$/', $vatin)) { |
|
97
|
|
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
if (true === $checkExistence) { |
|
101
|
|
|
$result = $this->getViesClient()->checkVat($countryCode, $vatin); |
|
102
|
|
|
|
|
103
|
|
|
return $result->isValid(); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return true; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Returns true if value is valid country code, false otherwise |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $value Value |
|
113
|
|
|
* |
|
114
|
|
|
* @return bool |
|
115
|
|
|
*/ |
|
116
|
|
|
private function isValidCountryCode($value) |
|
117
|
|
|
{ |
|
118
|
|
|
return isset($this->patterns[$value]); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Get VIES client |
|
123
|
|
|
* |
|
124
|
|
|
* @return Client |
|
125
|
|
|
*/ |
|
126
|
|
|
private function getViesClient() |
|
127
|
|
|
{ |
|
128
|
|
|
if ($this->viesClient === null) { |
|
129
|
|
|
$this->viesClient = new Client(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $this->viesClient; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|