Skip to content

Commit bd44583

Browse files
committed
fix: VatId must allow different Vat formats for NIF/IFZ/EUVAT type as this id type MUST be used for intracomunitary transactions
Added deprecation error if previous check function was used as this should be private and will be removed on future.
1 parent 68937b4 commit bd44583

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

src/Barnetik/Tbai/ValueObject/VatId.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class VatId implements Stringable
99
{
1010
const VAT_ID_TYPE_IFZ = '02';
1111
const VAT_ID_TYPE_NIF = '02';
12+
const VAT_ID_TYPE_EUVAT = '02';
1213
const VAT_ID_TYPE_PASSPORT = '03';
1314
/**
1415
* Egoitza dagoen herrialdeak edo lurraldeak emandako nortasun agiri ofiziala
@@ -23,24 +24,30 @@ class VatId implements Stringable
2324

2425
public function __construct(string $vatId, string $type = self::VAT_ID_TYPE_IFZ)
2526
{
26-
$this->check($vatId, $type);
27+
$this->checkType($type);
2728
$this->type = $type;
2829
$this->value = $vatId;
2930
}
3031

31-
public function check(string $vatId, string $type): bool
32+
private function checkType(string $type): bool
3233
{
3334
if (!in_array($type, self::validIdTypeValues())) {
3435
throw new InvalidVatIdException('Wrong VatId Type provided');
3536
}
3637

37-
if ($type === self::VAT_ID_TYPE_NIF && !preg_match('/^(([a-z|A-Z]{1}\d{7}[a-z|A-Z]{1})|(\d{8}[a-z|A-Z]{1})|([a-z|A-Z]{1}\d{8}))$/', $vatId, $matches)) {
38-
throw new InvalidVatIdException('Wrong VatId provided');
39-
}
40-
4138
return true;
4239
}
4340

41+
public function check(string $vatId, string $type): bool
42+
{
43+
trigger_error(
44+
'Deprecated. This only checks if type is correct and will be removed on the future as NIF/IFZ type MUST be used for intracomunitary transactions',
45+
E_USER_DEPRECATED
46+
);
47+
48+
return $this->checkType($type);
49+
}
50+
4451
public function __toString(): string
4552
{
4653
return $this->value;

tests/Barnetik/Tbai/ValueObject/VatIdTest.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,29 +10,19 @@ class VatIdTest extends TestCase
1010
{
1111
public function test_ifz_with_wrong_format_throw_exception(): void
1212
{
13-
try {
14-
new VatId("1234567-S", VatId::VAT_ID_TYPE_IFZ);
15-
$this->fail();
16-
} catch (InvalidVatIdException $e) {
17-
}
18-
19-
try {
20-
new VatId("0134567S", VatId::VAT_ID_TYPE_IFZ);
21-
$this->fail();
22-
} catch (InvalidVatIdException $e) {
23-
}
24-
25-
$this->assertTrue(true);
13+
$this->expectException(InvalidVatIdException::class);
14+
new VatId("1234567S", "wrongType");
2615
}
2716

2817
public function test_ifz_with_leading_zeros_do_not_throw_exception(): void
2918
{
3019
try {
3120
new VatId("01234567S", VatId::VAT_ID_TYPE_IFZ);
3221
new VatId("00000567S", VatId::VAT_ID_TYPE_IFZ);
33-
$this->assertTrue(true);
3422
} catch (InvalidVatIdException $e) {
3523
$this->fail();
3624
}
25+
26+
$this->assertTrue(true);
3727
}
3828
}

0 commit comments

Comments
 (0)