|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MichaelRubel\ValueObjects\Complex; |
|
6
|
|
|
|
|
7
|
|
|
use MichaelRubel\Formatters\Collection\TaxNumberFormatter; |
|
8
|
|
|
use MichaelRubel\ValueObjects\ValueObject; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @method static static make(string $taxNumber, string $country) |
|
12
|
|
|
*/ |
|
13
|
|
|
class TaxNumber extends ValueObject |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Create a new instance of the value object. |
|
17
|
|
|
* |
|
18
|
|
|
* @param string|null $taxNumber |
|
19
|
|
|
* @param string|null $country |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct( |
|
22
|
|
|
protected ?string $taxNumber = null, |
|
23
|
21 |
|
protected ?string $country = null, |
|
24
|
|
|
) { |
|
25
|
|
|
$this->taxNumber = format(TaxNumberFormatter::class, $this->taxNumber, $this->country); |
|
26
|
|
|
|
|
27
|
21 |
|
$this->when($this->isWithCountry(), function () { |
|
28
|
|
|
$this->country = str($this->taxNumber) |
|
29
|
21 |
|
->substr(0, 2) |
|
30
|
15 |
|
->upper() |
|
31
|
15 |
|
->value(); |
|
32
|
15 |
|
|
|
33
|
15 |
|
$this->taxNumber = str($this->taxNumber) |
|
34
|
|
|
->substr(2) |
|
35
|
15 |
|
->value(); |
|
36
|
15 |
|
}); |
|
37
|
15 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get the tax number with a country prefix. |
|
41
|
|
|
* |
|
42
|
|
|
* @return string |
|
43
|
|
|
*/ |
|
44
|
|
|
public function fullTaxNumber(): string |
|
45
|
|
|
{ |
|
46
|
11 |
|
return $this->country() . $this->taxNumber(); |
|
47
|
|
|
} |
|
48
|
11 |
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Get the tax number without country prefix. |
|
51
|
|
|
* |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
|
|
public function taxNumber(): string |
|
55
|
|
|
{ |
|
56
|
19 |
|
return str($this->taxNumber) |
|
57
|
|
|
->upper() |
|
58
|
19 |
|
->value(); |
|
59
|
19 |
|
} |
|
60
|
19 |
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get the country prefix for a given tax number. |
|
63
|
|
|
* |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
public function country(): string |
|
67
|
|
|
{ |
|
68
|
20 |
|
return str($this->country) |
|
69
|
|
|
->upper() |
|
70
|
20 |
|
->value(); |
|
71
|
20 |
|
} |
|
72
|
20 |
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Check if the tax number length is less or equal two. |
|
75
|
|
|
* |
|
76
|
|
|
* @return bool |
|
77
|
|
|
*/ |
|
78
|
|
|
public function isWithCountry(): bool |
|
79
|
|
|
{ |
|
80
|
21 |
|
return strlen($this->taxNumber) >= 2 && ! is_numeric($this->taxNumber); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
21 |
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the object value. |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
public function value(): string |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->fullTaxNumber(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get an array representation of the value object. |
|
95
|
|
|
* |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function toArray(): array |
|
99
|
|
|
{ |
|
100
|
|
|
return [ |
|
|
|
|
|
|
101
|
1 |
|
'fullTaxNumber' => $this->fullTaxNumber(), |
|
102
|
1 |
|
'taxNumber' => $this->taxNumber(), |
|
103
|
1 |
|
'country' => $this->country(), |
|
104
|
|
|
]; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|