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