Skip to content

Commit 9c85178

Browse files
committed
Add additional vat validation rule
1 parent 668d486 commit 9c85178

File tree

4 files changed

+107
-36
lines changed

4 files changed

+107
-36
lines changed

CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased]
88

99
## [0.3.0] - 2020-12-16
10-
1110
### Added
1211
- Support for vat_eu_if:field,compare_value
13-
12+
- Support for vat_eu_address_and_if:countryfield,field,compare_value
1413
## [0.2.2] - 2020-10-04
1514
### Fixed
1615
- Remove composer.lock from source code.

src/Classes/VatValidator.php

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,66 @@
1-
<?php
1+
<?php
22

33
namespace AMBERSIVE\VatValidator\Classes;
44

5+
use AMBERSIVE\VatValidator\Classes\VatCompany;
56
use App;
7+
use Illuminate\Validation\ValidationException;
68
use SoapClient;
79
use SoapFault;
810

9-
use Illuminate\Validation\ValidationException;
10-
11-
use AMBERSIVE\VatValidator\Classes\VatCompany;
12-
13-
class VatValidator {
14-
11+
class VatValidator
12+
{
1513
public SoapClient $client;
1614

17-
public function __construct(SoapClient $client = null) {
15+
public function __construct(SoapClient $client = null)
16+
{
1817
$client = null;
19-
$this->client = $client === null ? new SoapClient(config('vat-validator.wsdl', "http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl")) : $client;
18+
$this->client = $client === null ? new SoapClient(config('vat-validator.wsdl', 'http://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl')) : $client;
2019
}
21-
22-
20+
2321
/**
24-
* Returns a VatCompany data object if the vat id is not invalid at all
22+
* Returns a VatCompany data object if the vat id is not invalid at all.
2523
*
2624
* @param mixed $vatId
2725
* @return VatCompany
2826
*/
29-
public function check(String $vatId): VatCompany {
30-
31-
32-
33-
$vatId = str_replace(array(' ', '.', '-', ',', ', '), '', trim($vatId));
27+
public function check(String $vatId): VatCompany
28+
{
29+
$vatId = str_replace([' ', '.', '-', ',', ', '], '', trim($vatId));
3430

3531
$requestParams = [
36-
'countryCode' => $this->getCountryCode($vatId),
37-
'vatNumber' => $this->getVatNumber($vatId)
32+
'countryCode' => $this->getCountryCode($vatId),
33+
'vatNumber' => $this->getVatNumber($vatId),
3834
];
3935

4036
try {
41-
4237
$result = $this->client->checkVat($requestParams);
43-
4438
} catch (SoapFault $e) {
4539
abort(400, $e->getMessage());
4640
}
4741

4842
return new VatCompany($result);
49-
5043
}
51-
44+
5245
/**
53-
* Return the country code of the vat id
46+
* Return the country code of the vat id.
5447
*
5548
* @param mixed $vatId
56-
* @return String
49+
* @return string
5750
*/
58-
protected function getCountryCode(String $vatId): String {
59-
return substr($vatId, 0, 2);
51+
protected function getCountryCode(String $vatId): String
52+
{
53+
return substr($vatId, 0, 2);
6054
}
61-
55+
6256
/**
63-
* Returs the vat number
57+
* Returs the vat number.
6458
*
6559
* @param mixed $vatId
66-
* @return String
60+
* @return string
6761
*/
68-
protected function getVatNumber(String $vatId): String {
69-
return substr($vatId, 2);
62+
protected function getVatNumber(String $vatId): String
63+
{
64+
return substr($vatId, 2);
7065
}
71-
72-
}
66+
}

src/VatValidatorServiceProvider.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,35 @@
99

1010
class VatValidatorServiceProvider extends ServiceProvider
1111
{
12+
public static $countries = [
13+
'be',
14+
'bg',
15+
'cz',
16+
'dk',
17+
'de',
18+
'ee',
19+
'ie',
20+
'el',
21+
'es',
22+
'fr',
23+
'hr',
24+
'it',
25+
'cy',
26+
'lv',
27+
'lt',
28+
'lu',
29+
'hu',
30+
'mt',
31+
'nl',
32+
'at',
33+
'pt',
34+
'ro',
35+
'si',
36+
'sk',
37+
'fi',
38+
'se',
39+
];
40+
1241
/**
1342
* Register services.
1443
*
@@ -68,5 +97,33 @@ public function boot()
6897
return false;
6998
}
7099
});
100+
101+
Validator::extend('vat_eu_address_and_if', function ($attribute, $value, $parameters, $validator) {
102+
try {
103+
$data = $validator->getData();
104+
105+
if (isset($data[$parameters[0]]) === false || isset($data[$parameters[1]]) === false) {
106+
// Required fields does not exists in the provided input
107+
return false;
108+
}
109+
110+
if (in_array($data[$parameters[0]], static::$countries) === false) {
111+
// Cannot find the country in the list of european countries
112+
// This validator cannot check if the id is correct so mark it as true
113+
return true;
114+
}
115+
116+
if ($data[$parameters[1]] != $parameters[2]) {
117+
return true;
118+
}
119+
120+
$instance = new \AMBERSIVE\VatValidator\Classes\VatValidator();
121+
$result = $instance->check($value);
122+
123+
return $result->isValid();
124+
} catch (\Symfony\Component\HttpKernel\Exception\HttpException $ex) {
125+
return false;
126+
}
127+
});
71128
}
72129
}

tests/Units/VatValidatorTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,25 @@ public function testIfValidadtionExtensionsIsValidIfRequiredIfIsFalse():void
188188
// Test
189189
$this->assertFalse($validator->fails());
190190
}
191+
192+
public function testIfValidationExtentionsVatEuAddressAndIfFailsIfCountryIsNotFromEU():void
193+
{
194+
195+
// Prepare
196+
$data = [
197+
'vatid' => 'XXX',
198+
'company' => false,
199+
'country' => 'at',
200+
];
201+
202+
$rules = [
203+
'vatid' => 'vat_eu_address_and_if:country,company,true',
204+
];
205+
206+
// Execute
207+
$validator = Validator::make($data, $rules);
208+
209+
// Test
210+
$this->assertFalse($validator->fails());
211+
}
191212
}

0 commit comments

Comments
 (0)