|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Temidaio\ValueObjects\Formatters; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Str; |
|
8
|
|
|
use MichaelRubel\Formatters\Formatter; |
|
9
|
|
|
|
|
10
|
|
|
class AddressFormatter implements Formatter |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param string|null $prefix |
|
14
|
|
|
* @param string|null $street |
|
15
|
|
|
* @param string|null $number |
|
16
|
|
|
* @param string|null $local |
|
17
|
|
|
* @param string|null $postcode |
|
18
|
|
|
* @param string|null $city |
|
19
|
|
|
* @param string|null $country |
|
20
|
|
|
* @param string|null $iso_code |
|
21
|
|
|
*/ |
|
22
|
28 |
|
public function __construct( |
|
23
|
|
|
public ?string $prefix = null, |
|
24
|
|
|
public ?string $street = null, |
|
25
|
|
|
public ?string $number = null, |
|
26
|
|
|
public ?string $local = null, |
|
27
|
|
|
public ?string $postcode = null, |
|
28
|
|
|
public ?string $city = null, |
|
29
|
|
|
public ?string $country = null, |
|
30
|
|
|
public ?string $iso_code = null |
|
31
|
|
|
) { |
|
32
|
28 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* String builder. |
|
36
|
|
|
* |
|
37
|
|
|
* @var string |
|
38
|
|
|
*/ |
|
39
|
|
|
private string $builder = ''; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Run the formatter. |
|
43
|
|
|
* |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
28 |
|
public function format(): string |
|
47
|
|
|
{ |
|
48
|
28 |
|
$this->formatProperties(); |
|
49
|
|
|
|
|
50
|
28 |
|
return $this->buildString(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Initialize the internal properties. |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
28 |
|
protected function formatProperties(): void |
|
59
|
|
|
{ |
|
60
|
28 |
|
$this->street = format( |
|
61
|
28 |
|
StreetFormatter::class, |
|
62
|
28 |
|
prefix: $this->prefix, |
|
|
|
|
|
|
63
|
28 |
|
street: $this->street, |
|
64
|
28 |
|
number: $this->number, |
|
65
|
28 |
|
local: $this->local, |
|
66
|
28 |
|
); |
|
67
|
|
|
|
|
68
|
28 |
|
$this->country = format( |
|
69
|
28 |
|
CountryFormatter::class, |
|
70
|
28 |
|
country: $this->country, |
|
71
|
28 |
|
iso_code: $this->iso_code |
|
72
|
28 |
|
); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Execute the formatter logic. |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
28 |
|
protected function buildString(): string |
|
81
|
|
|
{ |
|
82
|
28 |
|
if (! empty($this->street)) { |
|
83
|
17 |
|
$this->appendStreet(); |
|
84
|
|
|
|
|
85
|
17 |
|
if (! empty($this->postcode) || ! empty($this->city) || ! empty($this->country)) { |
|
86
|
15 |
|
$this->appendComma(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
28 |
|
empty($this->postcode) ?: $this->appendPostcode(); |
|
91
|
|
|
|
|
92
|
28 |
|
empty($this->city) ?: $this->appendCity(); |
|
93
|
|
|
|
|
94
|
28 |
|
$this->appendCountry(); |
|
95
|
|
|
|
|
96
|
28 |
|
return $this->builder(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Sanitize the string builder. |
|
101
|
|
|
* |
|
102
|
|
|
* @return string |
|
103
|
|
|
*/ |
|
104
|
28 |
|
protected function builder(): string |
|
105
|
|
|
{ |
|
106
|
28 |
|
return Str::squish($this->builder); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Append the street to the string builder. |
|
111
|
|
|
* |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
17 |
|
protected function appendStreet(): void |
|
115
|
|
|
{ |
|
116
|
17 |
|
$this->builder .= $this->street; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Append the street to the string builder. |
|
121
|
|
|
* |
|
122
|
|
|
* @return void |
|
123
|
|
|
*/ |
|
124
|
15 |
|
protected function appendComma(): void |
|
125
|
|
|
{ |
|
126
|
15 |
|
$this->builder .= ', '; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Append the post code to the string builder. |
|
131
|
|
|
* |
|
132
|
|
|
* @return void |
|
133
|
|
|
*/ |
|
134
|
15 |
|
protected function appendPostcode(): void |
|
135
|
|
|
{ |
|
136
|
15 |
|
$this->builder .= $this->postcode . ' '; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Append the city to the string builder. |
|
141
|
|
|
* |
|
142
|
|
|
* @return void |
|
143
|
|
|
*/ |
|
144
|
15 |
|
protected function appendCity(): void |
|
145
|
|
|
{ |
|
146
|
15 |
|
$this->builder .= $this->city . ' '; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Append the country to the string builder. |
|
151
|
|
|
* |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
28 |
|
protected function appendCountry(): void |
|
155
|
|
|
{ |
|
156
|
28 |
|
$this->builder .= $this->country; |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|