Passed
Pull Request — main (#2)
by Michael
04:04
created

TaxNumber::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
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|null $number, string|null $prefix = null)
28
 * @method static static from(string|null $number, string|null $prefix = null)
29
 *
30
 * @extends ValueObject<TKey, TValue>
31
 */
32
class TaxNumber extends ValueObject
33
{
34
    /**
35
     * Create a new instance of the value object.
36
     *
37
     * @param  string  $number
38
     * @param  string|null  $prefix
39
     */
40 20
    public function __construct(
41
        protected string $number,
42
        protected ?string $prefix = null,
43
    ) {
44 20
        $this->validate();
45
46 20
        $this->number = $this->format();
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 [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('fullTaxNum...ix' => $this->prefix()) returns the type array<string,string> which is incompatible with the return type mandated by Illuminate\Contracts\Support\Arrayable::toArray() of Illuminate\Contracts\Support\TValue[].

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
115 1
            'fullTaxNumber' => $this->fullTaxNumber(),
116 1
            'taxNumber'     => $this->taxNumber(),
117 1
            'prefix'        => $this->prefix(),
118
        ];
119
    }
120
121
    /**
122
     * Format the value.
123
     *
124
     * @return string
125
     */
126 20
    protected function format(): string
127
    {
128 20
        return format(TaxNumberFormatter::class, $this->taxNumber(), $this->prefix());
129
    }
130
131
    /**
132
     * Determines whether to split the value.
133
     *
134
     * @return bool
135
     */
136 20
    protected function canSplit(): bool
137
    {
138 20
        return ! is_numeric($this->number);
139
    }
140
141
    /**
142
     * Split the value.
143
     *
144
     * @return void
145
     */
146 19
    protected function split(): void
147
    {
148 19
        $this->prefix = str($this->number)
149 19
            ->substr(0, 2)
150 19
            ->upper()
151 19
            ->value();
152
153 19
        $this->number = str($this->number)
154 19
            ->substr(2)
155 19
            ->value();
156
    }
157
158
    /**
159
     * Validate the value object data.
160
     *
161
     * @return void
162
     */
163 20
    protected function validate(): void
164
    {
165 20
        if (empty($this->value())) {
166 1
            throw new InvalidArgumentException('Tax number cannot be empty.');
167
        }
168
    }
169
}
170