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