TaxNumberFormatter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 17
Bugs 0 Features 1
Metric Value
eloc 12
c 17
b 0
f 1
dl 0
loc 40
ccs 14
cts 14
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getFullTaxNumber() 0 9 2
A format() 0 5 2
A __construct() 0 8 1
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