Passed
Push — main ( 8b709c...25ab63 )
by Michael
03:35
created

TaxNumber::split()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
ccs 8
cts 8
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 MichaelRubel\Formatters\Collection\TaxNumberFormatter;
16
use MichaelRubel\ValueObjects\ValueObject;
17
18
/**
19
 * "TaxNumber" object presenting VAT identification number.
20
 *
21
 * @author Michael Rubél <[email protected]>
22
 *
23
 * @template TKey of array-key
24
 * @template TValue
25
 *
26
 * @method static static make(string|null $number, string|null $prefix = null)
27
 * @method static static from(string|null $number, string|null $prefix = null)
28
 *
29
 * @extends ValueObject<TKey, TValue>
30
 */
31
class TaxNumber extends ValueObject
32
{
33
    /**
34
     * Create a new instance of the value object.
35
     *
36
     * @param  string|null  $number
37
     * @param  string|null  $prefix
38
     */
39 23
    public function __construct(
40
        protected ?string $number = null,
41
        protected ?string $prefix = null,
42
    ) {
43 23
        $this->number = $this->format();
44
45 23
        if ($this->canSplit()) {
46 22
            $this->split();
47
        }
48
    }
49
50
    /**
51
     * Get the tax number with a country prefix.
52
     *
53
     * @return string
54
     */
55 13
    public function fullTaxNumber(): string
56
    {
57 13
        return $this->prefix() . $this->taxNumber();
58
    }
59
60
    /**
61
     * Get the tax number without country prefix.
62
     *
63
     * @return string
64
     */
65 23
    public function taxNumber(): string
66
    {
67 23
        return str($this->number)
68 23
            ->upper()
69 23
            ->value();
70
    }
71
72
    /**
73
     * Get the tax number prefix.
74
     *
75
     * @return string
76
     */
77 23
    public function prefix(): string
78
    {
79 23
        return str($this->prefix)
80 23
            ->upper()
81 23
            ->value();
82
    }
83
84
    /**
85
     * Get the country prefix for a given tax number.
86
     *
87
     * @return string
88
     */
89 13
    public function country(): string
90
    {
91 13
        return $this->prefix();
92
    }
93
94
    /**
95
     * Get the object value.
96
     *
97
     * @return string
98
     */
99 5
    public function value(): string
100
    {
101 5
        return $this->fullTaxNumber();
102
    }
103
104
    /**
105
     * Format the value.
106
     *
107
     * @return string
108
     */
109 23
    protected function format(): string
110
    {
111 23
        return format(TaxNumberFormatter::class, $this->taxNumber(), $this->prefix());
112
    }
113
114
    /**
115
     * Determines whether to split the value.
116
     *
117
     * @return bool
118
     */
119 23
    protected function canSplit(): bool
120
    {
121 23
        return ! is_numeric($this->number);
122
    }
123
124
    /**
125
     * Split the value.
126
     *
127
     * @return void
128
     */
129 22
    protected function split(): void
130
    {
131 22
        $this->prefix = str($this->number)
132 22
            ->substr(0, 2)
133 22
            ->upper()
134 22
            ->value();
135
136 22
        $this->number = str($this->number)
137 22
            ->substr(2)
138 22
            ->value();
139
    }
140
141
    /**
142
     * Get an array representation of the value object.
143
     *
144
     * @return array
145
     */
146 1
    public function toArray(): array
147
    {
148
        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...
149 1
            'fullTaxNumber' => $this->fullTaxNumber(),
150 1
            'taxNumber'     => $this->taxNumber(),
151 1
            'prefix'        => $this->prefix(),
152
        ];
153
    }
154
}
155