Skip to content

Commit 6d314f6

Browse files
author
Venelin Manchev
committed
Initial commit
0 parents  commit 6d314f6

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
nbproject/
2+

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# VAT validator
2+
3+
Validate a VAT number against the official European Commission VIES VAT number validation SOAP service.
4+
5+
## How to install
6+
7+
TBD
8+
9+
## How to use
10+
11+
<pre>
12+
<?php
13+
14+
use Toxic\Validator\Vat as VatValidator;
15+
16+
$validator = VatValidator::getInstance();
17+
18+
//Returns true or false
19+
$validator->isValid("XY123456789");
20+
21+
//If VAT number is valid, company info is available
22+
$validator->getCompany();
23+
24+
</pre>

src/Toxic/Validator/Vat.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Toxic\Validator;
4+
5+
use Toxic\Validator\Vat\Company;
6+
7+
class Vat
8+
{
9+
10+
const config = 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl';
11+
12+
private static $instance;
13+
private $client;
14+
private $company;
15+
16+
private function __construct() {
17+
$this->client = new \SoapClient(self::config, [
18+
'exceptions' => false
19+
]);
20+
$this->company = new Company();
21+
}
22+
23+
public static function getInstance(): \Toxic\Validator\Vat {
24+
25+
if (!isset(static::$instance)) {
26+
static::$instance = new Vat();
27+
}
28+
29+
return static::$instance;
30+
}
31+
32+
public function isValid(string $code): bool {
33+
34+
$code = $this->filter($code);
35+
36+
$result = $this->client->checkVat([
37+
'countryCode' => $this->getCountryCode($code),
38+
'vatNumber' => $this->getVatNumber($code)
39+
]);
40+
41+
if ($result instanceof \SoapFault) {
42+
return false;
43+
}
44+
45+
return $this->setCompany($result)->isValid();
46+
}
47+
48+
private function filter(string $code): string {
49+
50+
$code = strtoupper($code);
51+
$code = filter_var($code, FILTER_SANITIZE_STRING);
52+
$code = preg_replace("/[^A-Z0-9]/", "", $code);
53+
54+
return trim($code);
55+
}
56+
57+
private function getCountryCode(string $code): string {
58+
return substr($code, 0, 2);
59+
}
60+
61+
private function getVatNumber(string $code): string {
62+
return substr($code, 2);
63+
}
64+
65+
private function setCompany(\stdClass $data): Company {
66+
67+
foreach ($this->company as $property => $value) {
68+
if (!isset($data->{$property})) {
69+
continue;
70+
}
71+
72+
$this->company->{$property} = $data->{$property};
73+
}
74+
75+
return $this->company;
76+
}
77+
78+
public function getCompany(): Company {
79+
return $this->company;
80+
}
81+
82+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Toxic\Validator\Vat;
4+
5+
class Company
6+
{
7+
8+
public $name;
9+
public $address;
10+
public $vatNumber;
11+
public $countryCode;
12+
public $valid;
13+
14+
public function isValid(): bool {
15+
return $this->valid;
16+
}
17+
18+
}

0 commit comments

Comments
 (0)