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