Passed
Pull Request — main (#5)
by Michael
05:54 queued 02:27
created

TaxNumber   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 0
Metric Value
eloc 29
dl 0
loc 135
ccs 38
cts 38
cp 1
rs 10
c 6
b 0
f 0
wmc 13

11 Methods

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