Completed
Pull Request — master (#21)
by Artur
03:04
created

Validator::getViesClient()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 8
ccs 3
cts 4
cp 0.75
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2.0625
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
        'NO' => '\d{9}',
47
        'PL' => '\d{10}',
48
        'PT' => '\d{9}',
49
        'RO' => '\d{2,10}',
50
        'SE' => '\d{12}',
51
        'SI' => '\d{8}',
52
        'SK' => '\d{10}'
53
    );
54
55
    /**
56
     * Client for the VIES web service
57
     *
58
     * @var Client
59
     */
60
    private $viesClient;
61
62
    /**
63
     * Constructor
64
     *
65
     * @param Client|null $viesClient Client for the VIES web service
66
     */
67 75
    public function __construct(Client $viesClient = null)
68
    {
69 75
        $this->viesClient = $viesClient;
70 75
    }
71
72
    /**
73
     * Returns true if value is a valid VAT identification number, false
74
     * otherwise
75
     *
76
     * @param string $value          Value
77
     * @param bool   $checkExistence In addition to checking the VATIN's format
78
     *                               for validity, also check whether the VATIN
79
     *                               exists. This requires a call to the VIES
80
     *                               web service.
81
     *
82
     * @return bool
83
     */
84 75
    public function isValid($value, $checkExistence = false)
85
    {
86 75
        if (null === $value || '' === $value) {
87 2
            return false;
88
        }
89
90 73
        $countryCode = substr($value, 0, 2);
91 73
        $vatin = substr($value, 2);
92
93 73
        if (false === $this->isValidCountryCode($countryCode)) {
94 2
            return false;
95
        }
96
97 71
        if (0 === preg_match('/^'.$this->patterns[$countryCode].'$/', $vatin)) {
98 1
            return false;
99
        }
100
101 70
        if (true === $checkExistence) {
102 3
            $result = $this->getViesClient()->checkVat($countryCode, $vatin);
103
104 2
            return $result->isValid();
105
        }
106
107 67
        return true;
108
    }
109
110
    /**
111
     * Returns true if value is valid country code, false otherwise
112
     *
113
     * @param string $value Value
114
     *
115
     * @return bool
116
     */
117 73
    private function isValidCountryCode($value)
118
    {
119 73
        return isset($this->patterns[$value]);
120
    }
121
122
    /**
123
     * Get VIES client
124
     *
125
     * @return Client
126
     */
127 3
    private function getViesClient()
128
    {
129 3
        if ($this->viesClient === null) {
130
            $this->viesClient = new Client();
131
        }
132
133 3
        return $this->viesClient;
134
    }
135
}
136