Total Complexity | 8 |
Total Lines | 63 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | <?php |
||
10 | class PostCode extends ValueObject |
||
11 | { |
||
12 | /** |
||
13 | * @var string|null |
||
14 | */ |
||
15 | protected ?string $postcode = null; |
||
16 | |||
17 | /** |
||
18 | * @var City|null |
||
19 | */ |
||
20 | protected ?City $city = null; |
||
21 | |||
22 | /** |
||
23 | * @var Country|null |
||
24 | */ |
||
25 | protected ?Country $country = null; |
||
26 | |||
27 | /** |
||
28 | * @param array|null $data |
||
29 | */ |
||
30 | 22 | final public function __construct(?array $data) |
|
31 | { |
||
32 | 22 | $this->postcode = $data['postcode'] ?? null; |
|
33 | |||
34 | 22 | if (isset($data['city'])) { |
|
35 | 12 | $this->city = new City($data); |
|
36 | } |
||
37 | |||
38 | 22 | if (isset($data['country_code'])) { |
|
39 | 4 | $this->country = new Country($data); |
|
40 | } |
||
41 | |||
42 | 22 | if (! empty($this->postcode)) { |
|
43 | 15 | $this->sanitize(); |
|
44 | } |
||
45 | } |
||
46 | |||
47 | /** |
||
48 | * @return string |
||
49 | */ |
||
50 | 21 | public function value(): string |
|
51 | { |
||
52 | 21 | return $this->postcode ?? ''; |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * @return void |
||
57 | */ |
||
58 | 15 | protected function sanitize(): void |
|
59 | { |
||
60 | 15 | if (! is_null($this->postcode)) { |
|
61 | 15 | $this->postcode = Str::squish($this->postcode); |
|
62 | } |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Cast the object to string. |
||
67 | * |
||
68 | * @return string |
||
69 | */ |
||
70 | 21 | public function __toString(): string |
|
73 | } |
||
74 | } |
||
75 |