Completed
Push — master ( 9de2ae...02afd8 )
by Ronan
06:45
created

Vat   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 1
cbo 0
dl 0
loc 108
rs 10

2 Methods

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