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