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