TaxNumber   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 35
dl 0
loc 150
ccs 44
cts 44
cp 1
rs 10
c 6
b 0
f 0
wmc 14

11 Methods

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