Total Complexity | 7 |
Total Lines | 65 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 10 | ||
Bugs | 0 | Features | 1 |
1 | <?php |
||
10 | class TaxNumberFormatter implements Formatter |
||
11 | { |
||
12 | /** |
||
13 | * @param string|null $tax_number |
||
14 | * @param string|null $country |
||
15 | */ |
||
16 | 14 | public function __construct( |
|
17 | public ?string $tax_number = null, |
||
18 | public ?string $country = null |
||
19 | ) { |
||
20 | 14 | $this->cleanup(); |
|
21 | } |
||
22 | |||
23 | /** |
||
24 | * Format the Tax Number. |
||
25 | * |
||
26 | * @return string |
||
27 | */ |
||
28 | 14 | public function format(): string |
|
29 | { |
||
30 | 14 | return ! blank($this->country) |
|
31 | 10 | ? $this->getFullTaxNumber() |
|
32 | 14 | : $this->tax_number ?? ''; |
|
33 | } |
||
34 | |||
35 | /** |
||
36 | * @return void |
||
37 | */ |
||
38 | 14 | private function cleanup(): void |
|
39 | { |
||
40 | 14 | $regexedTaxNumber = preg_replace_array( |
|
41 | '/[^\d\w]/', |
||
42 | 14 | [], |
|
43 | 14 | $this->tax_number |
|
44 | ); |
||
45 | |||
46 | 14 | $this->tax_number = Str::upper($regexedTaxNumber); |
|
47 | 14 | $this->country = Str::upper($this->country); |
|
48 | } |
||
49 | |||
50 | /** |
||
51 | * @return string |
||
52 | */ |
||
53 | 10 | private function getPrefix(): string |
|
54 | { |
||
55 | 10 | return (string) Str::of($this->tax_number) |
|
56 | 10 | ->substr(0, 2) |
|
57 | 10 | ->upper(); |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * @return string |
||
62 | */ |
||
63 | 10 | private function getFullTaxNumber(): string |
|
75 | } |
||
76 | } |
||
77 |