-
Notifications
You must be signed in to change notification settings - Fork 76
/
Vat.php
117 lines (103 loc) · 3 KB
/
Vat.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
namespace IsoCodes;
/**
* VAT
* The Value Added Tax, or VAT, is a general, broadly based consumption tax
* assessed on the value added to goods and services.
*/
class Vat implements IsoCodeInterface
{
/**
* Regular expression patterns per country code.
*
* @var array
*
* @see http://ec.europa.eu/taxation_customs/vies/faq.html?locale=lt#item_11
* @see http://www.iecomputersystems.com/ordering/eu_vat_numbers.htm
* @see http://en.wikipedia.org/wiki/VAT_identification_number
*/
public static $patterns = [
'AT' => 'U[A-Z\d]{8}',
'BE' => '(0|1)\d{9}',
'BG' => '\d{9,10}',
'CY' => '\d{8}[A-Z]',
'CZ' => '\d{8,10}',
'DE' => '\d{9}',
'DK' => '(\d{2} ?){3}\d{2}',
'EE' => '\d{9}',
'EL' => '\d{9}',
// ES: The first and last characters may be alpha or numeric; but they may not both be numeric:
'ES' => '[A-Z]\d{7}[A-Z]|\d{8}[A-Z]|[A-Z]\d{8}',
'FI' => '\d{8}',
'FR' => '([A-HJ-NP-Z0-9]{2})\d{9}',
'GB' => '\d{9}|\d{12}|(GD|HA)\d{3}',
'HU' => '\d{8}',
// IE: Seven digits and one last letter or six digits and two letters (second & last)
'IE' => '\d{7}[A-Z]|\d[A-Z]\d{5}[A-Z]',
'IT' => '\d{11}',
'LT' => '(\d{9}|\d{12})',
'LU' => '\d{8}',
'LV' => '\d{11}',
'MT' => '\d{8}',
// NL: The 10th position following the prefix is always "B".
'NL' => '\d{9}B\d{2}',
'PL' => '\d{10}',
'PT' => '\d{9}',
'RO' => '\d{2,10}',
'SE' => '\d{12}',
'SI' => '\d{8}',
'SK' => '\d{10}',
'AL' => '[KJ]\d{8}L',
'AU' => '\d{9}',
'BY' => '\d{9}',
'HR' => '\d{11}',
'CA' => '[A-Z\d]{15}',
'NO' => '\d{9}MVA',
'PH' => '\d{12}',
'RU' => '(\d{10}|\d{12})',
'CH' => '(\d{6}|E\d{9}(TVA|MWST|IVA))',
'TR' => '\d{10}',
'UA' => '\d{12}',
'AR' => '\d{11}',
'CL' => '\d{8}-\d',
'CO' => '\d{10}',
'EC' => '\d{13}',
'GT' => '\d{7}-\d',
'MX' => '\d{3} \d{6} \d{3}',
'VE' => '[JGVE]-\d{8}-?\d',
];
/**
* validate
* Checks if $vat is a valid Union VAT number.
*
* @param mixed $vat
*
* @return bool
*/
public static function validate($vat)
{
if (empty($vat) || null === $vat || '' === $vat) {
return false;
}
$countryCode = substr($vat, 0, 2);
if (false === self::isValidCountryCode($countryCode)) {
return false;
}
$vat = substr($vat, 2);
if (0 === preg_match('/^'.self::$patterns[$countryCode].'$/', $vat)) {
return false;
}
return true;
}
/**
* Returns true if value is valid country code, false otherwise.
*
* @param string $vat
*
* @return bool
*/
public static function isValidCountryCode($vat)
{
return isset(self::$patterns[$vat]);
}
}