TaxNumberFormatter::getFullTaxNumber()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 4
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\Formatters\Collection;
6
7
use Illuminate\Support\Str;
8
use MichaelRubel\Formatters\Formatter;
9
10
class TaxNumberFormatter implements Formatter
11
{
12
    /**
13
     * @param  string|null  $tax_number
14
     * @param  string|null  $country
15
     */
16 17
    public function __construct(
17
        public ?string $tax_number = null,
18
        public ?string $country = null
19
    ) {
20 17
        $filteredTaxNumber = preg_replace('/[^\d\w]/', '', (string) $this->tax_number);
21
22 17
        $this->tax_number = Str::upper($filteredTaxNumber);
23 17
        $this->country    = Str::upper($this->country);
24
    }
25
26
    /**
27
     * Format the Tax Number.
28
     *
29
     * @return string|null
30
     */
31 17
    public function format(): ?string
32
    {
33 17
        return ! blank($this->country)
34 13
            ? $this->getFullTaxNumber()
35 17
            : $this->tax_number;
36
    }
37
38
    /**
39
     * @return string
40
     */
41 13
    private function getFullTaxNumber(): string
42
    {
43 13
        $string = str($this->tax_number);
44
45 13
        $value = $string->startsWith($this->country)
46 4
            ? $string->substr(2)->start($this->country)
47 9
            : $string->start($this->country);
48
49 13
        return $value->toString();
50
    }
51
}
52