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