1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of michael-rubel/laravel-value-objects. (https://github.com/michael-rubel/laravel-value-objects) |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/michael-rubel/laravel-value-objects for the canonical source repository |
9
|
|
|
* @copyright Copyright (c) 2022 Michael Rubél. (https://github.com/michael-rubel/) |
10
|
|
|
* @license https://raw.githubusercontent.com/michael-rubel/laravel-value-objects/main/LICENSE.md MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace MichaelRubel\ValueObjects\Collection\Complex; |
14
|
|
|
|
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use MichaelRubel\Formatters\Collection\TaxNumberFormatter; |
17
|
|
|
use MichaelRubel\ValueObjects\ValueObject; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* "TaxNumber" object presenting VAT identification number. |
21
|
|
|
* |
22
|
|
|
* @author Michael Rubél <[email protected]> |
23
|
|
|
* |
24
|
|
|
* @template TKey of array-key |
25
|
|
|
* @template TValue |
26
|
|
|
* |
27
|
|
|
* @method static static make(string|null $number, string|null $prefix = null) |
28
|
|
|
* @method static static from(string|null $number, string|null $prefix = null) |
29
|
|
|
* |
30
|
|
|
* @extends ValueObject<TKey, TValue> |
31
|
|
|
*/ |
32
|
|
|
class TaxNumber extends ValueObject |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Create a new instance of the value object. |
36
|
|
|
* |
37
|
|
|
* @param string $number |
38
|
|
|
* @param string|null $prefix |
39
|
|
|
*/ |
40
|
20 |
|
public function __construct( |
41
|
|
|
protected string $number, |
42
|
|
|
protected ?string $prefix = null, |
43
|
|
|
) { |
44
|
20 |
|
$this->validate(); |
45
|
20 |
|
$this->format(); |
46
|
|
|
|
47
|
20 |
|
if ($this->canSplit()) { |
48
|
19 |
|
$this->split(); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get the tax number with a country prefix. |
54
|
|
|
* |
55
|
|
|
* @return string |
56
|
|
|
*/ |
57
|
20 |
|
public function fullTaxNumber(): string |
58
|
|
|
{ |
59
|
20 |
|
return $this->prefix() . $this->taxNumber(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get the tax number without country prefix. |
64
|
|
|
* |
65
|
|
|
* @return string |
66
|
|
|
*/ |
67
|
20 |
|
public function taxNumber(): string |
68
|
|
|
{ |
69
|
20 |
|
return str($this->number) |
70
|
20 |
|
->upper() |
71
|
20 |
|
->value(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the tax number prefix. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
20 |
|
public function prefix(): string |
80
|
|
|
{ |
81
|
20 |
|
return str($this->prefix) |
82
|
20 |
|
->upper() |
83
|
20 |
|
->value(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Get the country prefix for a given tax number. |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
10 |
|
public function country(): string |
92
|
|
|
{ |
93
|
10 |
|
return $this->prefix(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Get the object value. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
20 |
|
public function value(): string |
102
|
|
|
{ |
103
|
20 |
|
return $this->fullTaxNumber(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Get an array representation of the value object. |
108
|
|
|
* |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
1 |
|
public function toArray(): array |
112
|
|
|
{ |
113
|
|
|
return [ |
|
|
|
|
114
|
1 |
|
'fullTaxNumber' => $this->fullTaxNumber(), |
115
|
1 |
|
'taxNumber' => $this->taxNumber(), |
116
|
1 |
|
'prefix' => $this->prefix(), |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Validate the value object data. |
122
|
|
|
* |
123
|
|
|
* @return void |
124
|
|
|
*/ |
125
|
20 |
|
protected function validate(): void |
126
|
|
|
{ |
127
|
20 |
|
if (empty($this->value())) { |
128
|
1 |
|
throw new InvalidArgumentException('Tax number cannot be empty.'); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Format the value. |
134
|
|
|
* |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
20 |
|
protected function format(): void |
138
|
|
|
{ |
139
|
20 |
|
$this->number = format(TaxNumberFormatter::class, $this->taxNumber(), $this->prefix()); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Determines whether to split the value. |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
20 |
|
protected function canSplit(): bool |
148
|
|
|
{ |
149
|
20 |
|
return ! is_numeric($this->number); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Split the value. |
154
|
|
|
* |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
19 |
|
protected function split(): void |
158
|
|
|
{ |
159
|
19 |
|
$this->prefix = str($this->number) |
160
|
19 |
|
->substr(0, 2) |
161
|
19 |
|
->upper() |
162
|
19 |
|
->value(); |
163
|
|
|
|
164
|
19 |
|
$this->number = str($this->number) |
165
|
19 |
|
->substr(2) |
166
|
19 |
|
->value(); |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: