Passed
Push — main ( 761ebb...8b709c )
by Michael
03:28
created

TaxNumber::isWithPrefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
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 (! is_numeric($this->number)) {
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
     * Split the value.
116
     *
117
     * @return void
118
     */
119 22
    protected function split(): void
120
    {
121 22
        $this->prefix = str($this->number)
122 22
            ->substr(0, 2)
123 22
            ->upper()
124 22
            ->value();
125
126 22
        $this->number = str($this->number)
127 22
            ->substr(2)
128 22
            ->value();
129
    }
130
131
    /**
132
     * Get an array representation of the value object.
133
     *
134
     * @return array
135
     */
136 1
    public function toArray(): array
137
    {
138
        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...
139 1
            'fullTaxNumber' => $this->fullTaxNumber(),
140 1
            'taxNumber'     => $this->taxNumber(),
141 1
            'prefix'        => $this->prefix(),
142
        ];
143
    }
144
}
145