Skip to content

Commit 668d486

Browse files
committed
Add support for vat_eu_if validator to make vat validation possible for specfic rules
1 parent 8bbc46a commit 668d486

File tree

3 files changed

+115
-58
lines changed

3 files changed

+115
-58
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88

9+
## [0.3.0] - 2020-12-16
10+
11+
### Added
12+
- Support for vat_eu_if:field,compare_value
13+
914
## [0.2.2] - 2020-10-04
1015
### Fixed
1116
- Remove composer.lock from source code.

src/VatValidatorServiceProvider.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
namespace AMBERSIVE\VatValidator;
44

55
use App;
6-
use Validator;
76
use Illuminate\Foundation\AliasLoader;
8-
97
use Illuminate\Support\ServiceProvider;
8+
use Validator;
109

1110
class VatValidatorServiceProvider extends ServiceProvider
1211
{
13-
1412
/**
1513
* Register services.
1614
*
@@ -32,11 +30,11 @@ public function register()
3230
*/
3331
public function boot()
3432
{
35-
33+
3634
// Configs
3735
$this->publishes([
3836
__DIR__.'/Configs/vat-validator.php' => config_path('vat-validator.php'),
39-
],'vat-validator');
37+
], 'vat-validator');
4038

4139
$this->mergeConfigFrom(
4240
__DIR__.'/Configs/vat-validator.php', 'vat-validator.php'
@@ -47,12 +45,28 @@ public function boot()
4745
try {
4846
$instance = new \AMBERSIVE\VatValidator\Classes\VatValidator();
4947
$result = $instance->check($value);
48+
5049
return $result->isValid();
51-
} catch(\Symfony\Component\HttpKernel\Exception\HttpException $ex) {
50+
} catch (\Symfony\Component\HttpKernel\Exception\HttpException $ex) {
5251
return false;
5352
}
5453
});
5554

56-
}
55+
Validator::extend('vat_eu_if', function ($attribute, $value, $parameters, $validator) {
56+
try {
57+
$data = $validator->getData();
58+
59+
if (isset($data[$parameters[0]]) === false || $data[$parameters[0]] != $parameters[1]) {
60+
return true;
61+
}
5762

63+
$instance = new \AMBERSIVE\VatValidator\Classes\VatValidator();
64+
$result = $instance->check($value);
65+
66+
return $result->isValid();
67+
} catch (\Symfony\Component\HttpKernel\Exception\HttpException $ex) {
68+
return false;
69+
}
70+
});
71+
}
5872
}

tests/Units/VatValidatorTest.php

Lines changed: 89 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,28 @@
11
<?php
22

33
use AMBERSIVE\Tests\TestCase;
4-
54
use AMBERSIVE\VatValidator\Classes\VatValidator;
6-
75
use VatValidator as VV;
86

97
class VatValidatorTest extends TestCase
108
{
11-
129
private VatValidator $validator;
1310

1411
protected function setUp(): void
1512
{
1613
parent::setUp();
1714

1815
$mockedResult = [
19-
"countryCode" => "AT",
20-
"vatNumber" => "U69434328",
21-
"requestDate" => "2020-07-10+02:00",
22-
"valid" => false,
23-
"name" => "---",
24-
"address" => "---"
16+
'countryCode' => 'AT',
17+
'vatNumber' => 'U69434328',
18+
'requestDate' => '2020-07-10+02:00',
19+
'valid' => false,
20+
'name' => '---',
21+
'address' => '---',
2522
];
2623

2724
$client = $this->getMockFromWsdl(
28-
__DIR__.'/../checkVatService.wsdl', 'checkVat' . md5( time().rand())
25+
__DIR__.'/../checkVatService.wsdl', 'checkVat'.md5(time().rand())
2926
);
3027

3128
$client
@@ -36,37 +33,39 @@ protected function setUp(): void
3633
}
3734

3835
/**
39-
* Test if the the method will throw an exeception cause the id complete wrong
36+
* Test if the the method will throw an exeception cause the id complete wrong.
4037
*/
41-
public function testIfVatValidatorThrowsExceptionIfInvalidVatId():void {
38+
public function testIfVatValidatorThrowsExceptionIfInvalidVatId():void
39+
{
4240
$this->expectException(\Symfony\Component\HttpKernel\Exception\HttpException::class);
43-
$result = $this->validator->check("TEST");
41+
$result = $this->validator->check('TEST');
4442
}
4543

4644
/**
47-
* Test if the vat return invalid if the id seems to be in the right structure but is wrong
45+
* Test if the vat return invalid if the id seems to be in the right structure but is wrong.
4846
*/
49-
public function testIfVatValidatorWillNotThrowExeceptionIfTheVatIdIsWrong():void {
50-
$result = $this->validator->check("ATU69434328");
47+
public function testIfVatValidatorWillNotThrowExeceptionIfTheVatIdIsWrong():void
48+
{
49+
$result = $this->validator->check('ATU69434328');
5150
$this->assertNotNull($result);
52-
$this->assertEquals(false, $result->isValid());
51+
$this->assertEquals(false, $result->isValid());
5352
}
5453

5554
/**
56-
* Test if the validator returns the correct company profile
55+
* Test if the validator returns the correct company profile.
5756
*/
58-
public function testIfVatValidatorReturnsValidObject():void {
59-
57+
public function testIfVatValidatorReturnsValidObject():void
58+
{
6059
$mockedResult = [
61-
"countryCode" => "AT",
62-
"vatNumber" => "U69434329",
63-
"valid" => true,
64-
"name" => "PICAPIPE GmbH",
65-
"address" => "Geylinggasse 15-17\\nAT-1130 Wien"
60+
'countryCode' => 'AT',
61+
'vatNumber' => 'U69434329',
62+
'valid' => true,
63+
'name' => 'PICAPIPE GmbH',
64+
'address' => 'Geylinggasse 15-17\\nAT-1130 Wien',
6665
];
6766

6867
$client = $this->getMockFromWsdl(
69-
__DIR__.'/../checkVatService.wsdl', 'checkVat' . md5( time().rand())
68+
__DIR__.'/../checkVatService.wsdl', 'checkVat'.md5(time().rand())
7069
);
7170

7271
$client
@@ -75,79 +74,118 @@ public function testIfVatValidatorReturnsValidObject():void {
7574

7675
$validator = new VatValidator($client);
7776

78-
$result = $validator->check("ATU69434329");
77+
$result = $validator->check('ATU69434329');
7978
$this->assertNotNull($result);
80-
$this->assertEquals("AT", $result->getCountry());
79+
$this->assertEquals('AT', $result->getCountry());
8180
$this->assertEquals(true, $result->isValid());
82-
$this->assertEquals("PICAPIPE GmbH", $result->getName());
83-
81+
$this->assertEquals('PICAPIPE GmbH', $result->getName());
8482
}
8583

8684
/**
87-
* Test if the Facade is working
85+
* Test if the Facade is working.
8886
*/
89-
public function testIfFacadeIsWorking():void {
90-
$result = VV::check("ATU69434329");
91-
$this->assertEquals("AT", $result->getCountry());
87+
public function testIfFacadeIsWorking():void
88+
{
89+
$result = VV::check('ATU69434329');
90+
$this->assertEquals('AT', $result->getCountry());
9291
$this->assertEquals(true, $result->isValid());
93-
$this->assertEquals("PICAPIPE GmbH", $result->getName());
92+
$this->assertEquals('PICAPIPE GmbH', $result->getName());
9493
}
95-
96-
public function testIfValidationExtentionsWorks():void {
9794

98-
// Prepare
95+
public function testIfValidationExtentionsWorks():void
96+
{
97+
98+
// Prepare
9999
$data = [
100-
'vatid' => 'ATU69434329'
100+
'vatid' => 'ATU69434329',
101101
];
102102

103103
$rules = [
104-
'vatid' => 'vat_eu'
104+
'vatid' => 'vat_eu',
105105
];
106106

107107
// Execute
108108
$validator = Validator::make($data, $rules);
109109

110110
// Test
111111
$this->assertFalse($validator->fails());
112-
113112
}
114113

115-
public function testIfValidationExtentionsWorksButReturnsInvalid():void {
114+
public function testIfValidationExtentionsWorksButReturnsInvalid():void
115+
{
116116

117-
// Prepare
117+
// Prepare
118118
$data = [
119-
'vatid' => 'ATU69434328'
119+
'vatid' => 'ATU69434328',
120120
];
121121

122122
$rules = [
123-
'vatid' => 'vat_eu'
123+
'vatid' => 'vat_eu',
124124
];
125125

126126
// Execute
127127
$validator = Validator::make($data, $rules);
128128

129129
// Test
130130
$this->assertTrue($validator->fails());
131-
132131
}
133132

134-
public function testIfValidationExtentionsWorksButReturnsInvalidEvenIfPassedValueIsTotallyWrong():void {
133+
public function testIfValidationExtentionsWorksButReturnsInvalidEvenIfPassedValueIsTotallyWrong():void
134+
{
135135

136-
// Prepare
136+
// Prepare
137137
$data = [
138-
'vatid' => 'XXX'
138+
'vatid' => 'XXX',
139139
];
140140

141141
$rules = [
142-
'vatid' => 'vat_eu'
142+
'vatid' => 'vat_eu',
143143
];
144144

145145
// Execute
146146
$validator = Validator::make($data, $rules);
147147

148148
// Test
149149
$this->assertTrue($validator->fails());
150+
}
150151

152+
public function testIfValidationExtensionWorksWithVatEuIf():void
153+
{
154+
155+
// Prepare
156+
$data = [
157+
'vatid' => 'XXX',
158+
'company' => true,
159+
];
160+
161+
$rules = [
162+
'vatid' => 'vat_eu_if:company,true',
163+
];
164+
165+
// Execute
166+
$validator = Validator::make($data, $rules);
167+
168+
// Test
169+
$this->assertTrue($validator->fails());
151170
}
152171

153-
}
172+
public function testIfValidadtionExtensionsIsValidIfRequiredIfIsFalse():void
173+
{
174+
175+
// Prepare
176+
$data = [
177+
'vatid' => 'XXX',
178+
'company' => false,
179+
];
180+
181+
$rules = [
182+
'vatid' => 'vat_eu_if:company,true',
183+
];
184+
185+
// Execute
186+
$validator = Validator::make($data, $rules);
187+
188+
// Test
189+
$this->assertFalse($validator->fails());
190+
}
191+
}

0 commit comments

Comments
 (0)