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 StreetFormatter implements Formatter |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* String builder. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private string $builder = ''; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param string|null $prefix |
21
|
|
|
* @param string|null $street |
22
|
|
|
* @param string|null $number |
23
|
|
|
* @param string|null $local |
24
|
|
|
*/ |
25
|
45 |
|
public function __construct( |
26
|
|
|
public ?string $prefix = null, |
27
|
|
|
public ?string $street = null, |
28
|
|
|
public ?string $number = null, |
29
|
|
|
public ?string $local = null |
30
|
|
|
) { |
31
|
45 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Execute the formatter logic. |
35
|
|
|
* |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
45 |
|
public function format(): string |
39
|
|
|
{ |
40
|
45 |
|
if (! empty($this->prefix) && ! empty($this->street)) { |
41
|
18 |
|
$this->appendPrefix(); |
42
|
|
|
} |
43
|
|
|
|
44
|
45 |
|
$this->appendStreet(); |
45
|
|
|
|
46
|
45 |
|
if (! empty($this->local)) { |
47
|
21 |
|
if (! empty($this->number)) { |
48
|
18 |
|
$this->appendSlash(); |
49
|
|
|
} |
50
|
|
|
|
51
|
21 |
|
$this->appendLocal(); |
52
|
|
|
} |
53
|
|
|
|
54
|
45 |
|
return $this->builder(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Sanitize the string builder. |
59
|
|
|
* |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
45 |
|
protected function builder(): string |
63
|
|
|
{ |
64
|
45 |
|
return Str::squish($this->builder); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Append the prefix to the string builder. |
69
|
|
|
* |
70
|
|
|
* @return void |
71
|
|
|
*/ |
72
|
18 |
|
protected function appendPrefix(): void |
73
|
|
|
{ |
74
|
18 |
|
$this->builder .= $this->prefix . ' '; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Append the street & number to the string builder. |
79
|
|
|
* |
80
|
|
|
* @return void |
81
|
|
|
*/ |
82
|
45 |
|
protected function appendStreet(): void |
83
|
|
|
{ |
84
|
45 |
|
$this->builder .= $this->street . ' ' . $this->number; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Append slash to the string builder. |
89
|
|
|
* |
90
|
|
|
* @return void |
91
|
|
|
*/ |
92
|
18 |
|
protected function appendSlash(): void |
93
|
|
|
{ |
94
|
18 |
|
$this->builder .= '/'; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Append slash to the string builder. |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
21 |
|
protected function appendLocal(): void |
103
|
|
|
{ |
104
|
21 |
|
$this->builder .= $this->local; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|