Passed
Pull Request — main (#9)
by
unknown
12:19
created

TaxNumberFormatter::format()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 16
c 1
b 0
f 1
nc 3
nop 1
dl 0
loc 28
ccs 18
cts 18
cp 1
crap 3
rs 9.7333
1
<?php
2
3
declare(strict_types=1);
4
5
namespace MichaelRubel\Formatters\Collection;
6
7
use Illuminate\Support\Collection;
8
use MichaelRubel\Formatters\Formatter;
9
10
class TaxNumberFormatter implements Formatter
11
{
12
    /**
13
     * @var string
14
     */
15
    public string $key_number = 'tax_number';
16
17
    /**
18
     * @var string
19
     */
20
    public string $key_country = 'country';
21
22
    /**
23
     * Format the Tax Number.
24
     *
25
     * @param Collection $items
26
     *
27
     * @return string
28
     */
29 8
    public function format(Collection $items): string
30
    {
31 8
        $tax_number = preg_replace(
32 8
            '/[^\d\w]/',
33 8
            '',
34 8
            $items->get($this->key_number, '')
35
        );
36
37 8
        if (empty($items->get($this->key_country))) {
38 2
            return $tax_number;
39
        }
40
41 6
        $country = strtoupper($items->get($this->key_country));
42
43 6
        $prefix = strtoupper(
44 6
            substr(
45 6
                $tax_number,
46 6
                0,
47 6
                strlen($country)
48
            )
49
        );
50
51 6
        return $prefix === $country
52 2
            ? $country . substr(
53 2
                $tax_number,
54 2
                strlen($country)
55
            )
56 6
            : $country . $tax_number;
57
    }
58
}
59