Completed
Pull Request — master (#24)
by
unknown
02:47
created

Validator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 120
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 120
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B isValid() 0 25 6
A isValidCountryCode() 0 4 1
A __construct() 0 4 1
A getViesClient() 0 8 2
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|null $viesClient Client for the VIES web service
65
     */
66 75
    public function __construct(Client $viesClient = null)
67
    {
68 75
        $this->viesClient = $viesClient;
69 75
    }
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 75
    public function isValid($value, $checkExistence = false)
84
    {
85 75
        if (null === $value || '' === $value) {
86 2
            return false;
87
        }
88
89 73
        $countryCode = substr($value, 0, 2);
90 73
        $vatin = substr($value, 2);
91
92 73
        if (false === $this->isValidCountryCode($countryCode)) {
93 2
            return false;
94
        }
95
96 71
        if (0 === preg_match('/^(?:'.$this->patterns[$countryCode].')$/', $vatin)) {
97 2
            return false;
98
        }
99
100 69
        if (true === $checkExistence) {
101 3
            $result = $this->getViesClient()->checkVat($countryCode, $vatin);
102
103 2
            return $result->isValid();
104
        }
105
106 66
        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 73
    private function isValidCountryCode($value)
117
    {
118 73
        return isset($this->patterns[$value]);
119
    }
120
121
    /**
122
     * Get VIES client
123
     *
124
     * @return Client
125
     */
126 3
    private function getViesClient()
127
    {
128 3
        if ($this->viesClient === null) {
129
            $this->viesClient = new Client();
130
        }
131
132 3
        return $this->viesClient;
133
    }
134
}
135