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

TaxNumberFormatter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 19
c 2
b 0
f 1
dl 0
loc 47
ccs 18
cts 18
cp 1
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A format() 0 28 3
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