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
|
21 |
|
public function __construct( |
22
|
|
|
protected ?string $taxNumber = null, |
23
|
|
|
protected ?string $country = null, |
24
|
|
|
) { |
25
|
21 |
|
$this->taxNumber = format(TaxNumberFormatter::class, $this->taxNumber, $this->country); |
26
|
|
|
|
27
|
21 |
|
$this->when($this->isWithCountry(), function () { |
28
|
15 |
|
$this->country = str($this->taxNumber) |
29
|
15 |
|
->substr(0, 2) |
30
|
15 |
|
->upper() |
31
|
15 |
|
->value(); |
32
|
|
|
|
33
|
15 |
|
$this->taxNumber = str($this->taxNumber) |
34
|
15 |
|
->substr(2) |
35
|
15 |
|
->value(); |
36
|
|
|
}); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the tax number with a country prefix. |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
11 |
|
public function fullTaxNumber(): string |
45
|
|
|
{ |
46
|
11 |
|
return $this->country() . $this->taxNumber(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the tax number without country prefix. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
19 |
|
public function taxNumber(): string |
55
|
|
|
{ |
56
|
19 |
|
return str($this->taxNumber) |
57
|
19 |
|
->upper() |
58
|
19 |
|
->value(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the country prefix for a given tax number. |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
20 |
|
public function country(): string |
67
|
|
|
{ |
68
|
20 |
|
return str($this->country) |
69
|
20 |
|
->upper() |
70
|
20 |
|
->value(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Check if the tax number length is less or equal two. |
75
|
|
|
* |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
21 |
|
public function isWithCountry(): bool |
79
|
|
|
{ |
80
|
21 |
|
return strlen($this->taxNumber) >= 2 && ! is_numeric($this->taxNumber); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get the object value. |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
3 |
|
public function value(): string |
89
|
|
|
{ |
90
|
3 |
|
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
|
|
|
|