-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidVatFormat.php
executable file
·90 lines (82 loc) · 2.32 KB
/
ValidVatFormat.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
<?php
/**
* @Package: MaplePHP - Validate vat number
* @Author: Daniel Ronkainen
* @Licence: Apache-2.0 license, Copyright © Daniel Ronkainen
Don't delete this comment, its part of the license.
*/
namespace MaplePHP\Validate;
class ValidVatFormat
{
/**
* Regular expression per country code
* @var array<string, string>
* @link http://ec.europa.eu/taxation_customs/vies/faq.html?locale=lt#item_11
*/
public const PATTERNS = [
'AT' => 'U[A-Z\d]{8}',
'BE' => '(0\d{9}|\d{10})',
'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' => '([A-Z]\d{7}[A-Z]|\d{8}[A-Z]|[A-Z]\d{8})',
'FI' => '\d{8}',
'FR' => '[A-Z\d]{2}\d{9}',
'GB' => '(\d{9}|\d{12}|(GD|HA)\d{3})',
'HR' => '\d{11}',
'HU' => '\d{8}',
'IE' => '([A-Z\d]{8}|[A-Z\d]{9})',
'IT' => '\d{11}',
'LT' => '(\d{9}|\d{12})',
'LU' => '\d{8}',
'LV' => '\d{11}',
'MT' => '\d{8}',
'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}'
];
private string $country;
private string $number;
public function __construct(string $vatNumber)
{
$this->country = substr($vatNumber, 0, 2);
$this->number = substr($vatNumber, 2);
}
/**
* Validate a VAT country and if is EU.
* @return bool
*/
public function validateCountry(): bool
{
return (isset($this::PATTERNS[$this->country]));
}
/**
* Get the selected country code
* @return string|null
*/
public function getCountryCode(): ?string
{
return $this->country;
}
/**
* Validate a VAT number format. This does not check whether the VAT number was really issued.
* @return bool
*/
public function validate(): bool
{
if ($this->validateCountry()) {
/** @var array<string, string> $pattern */
$pattern = $this::PATTERNS;
return (preg_match('/^' . $pattern[$this->country] . '$/', $this->number) > 0);
}
return false;
}
}