1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Kerox\Messenger\Model\Common; |
6
|
|
|
|
7
|
|
|
class Address implements \JsonSerializable |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var string|null |
11
|
|
|
*/ |
12
|
|
|
protected $name; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $street; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string|null |
21
|
|
|
*/ |
22
|
|
|
protected $additionalStreet; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $city; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $postalCode; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $state; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
protected $country; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var int|null |
46
|
|
|
*/ |
47
|
|
|
protected $id; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Address constructor. |
51
|
|
|
*/ |
52
|
10 |
|
public function __construct( |
53
|
|
|
string $street, |
54
|
|
|
string $city, |
55
|
|
|
string $postalCode, |
56
|
|
|
string $state, |
57
|
|
|
string $country |
58
|
|
|
) { |
59
|
10 |
|
$this->street = $street; |
60
|
10 |
|
$this->city = $city; |
61
|
10 |
|
$this->postalCode = $postalCode; |
62
|
10 |
|
$this->state = $state; |
63
|
10 |
|
$this->country = $country; |
64
|
10 |
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return \Kerox\Messenger\Model\Common\Address |
68
|
|
|
*/ |
69
|
6 |
|
public static function create( |
70
|
|
|
string $street, |
71
|
|
|
string $city, |
72
|
|
|
string $postalCode, |
73
|
|
|
string $state, |
74
|
|
|
string $country |
75
|
|
|
): self { |
76
|
6 |
|
return new self($street, $city, $postalCode, $state, $country); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return \Kerox\Messenger\Model\Common\Address |
81
|
|
|
*/ |
82
|
2 |
|
public function setName(string $name): self |
83
|
|
|
{ |
84
|
2 |
|
$this->name = $name; |
85
|
|
|
|
86
|
2 |
|
return $this; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
public function getName(): ?string |
90
|
|
|
{ |
91
|
1 |
|
return $this->name; |
92
|
|
|
} |
93
|
|
|
|
94
|
3 |
|
public function getStreet(): string |
95
|
|
|
{ |
96
|
3 |
|
return $this->street; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return Address |
101
|
|
|
*/ |
102
|
5 |
|
public function setAdditionalStreet(string $additionalStreet): self |
103
|
|
|
{ |
104
|
5 |
|
$this->additionalStreet = $additionalStreet; |
105
|
|
|
|
106
|
5 |
|
return $this; |
107
|
|
|
} |
108
|
|
|
|
109
|
3 |
|
public function getAdditionalStreet(): ?string |
110
|
|
|
{ |
111
|
3 |
|
return $this->additionalStreet; |
112
|
|
|
} |
113
|
|
|
|
114
|
3 |
|
public function getCity(): string |
115
|
|
|
{ |
116
|
3 |
|
return $this->city; |
117
|
|
|
} |
118
|
|
|
|
119
|
3 |
|
public function getPostalCode(): string |
120
|
|
|
{ |
121
|
3 |
|
return $this->postalCode; |
122
|
|
|
} |
123
|
|
|
|
124
|
3 |
|
public function getState(): string |
125
|
|
|
{ |
126
|
3 |
|
return $this->state; |
127
|
|
|
} |
128
|
|
|
|
129
|
3 |
|
public function getCountry(): string |
130
|
|
|
{ |
131
|
3 |
|
return $this->country; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @return Address |
136
|
|
|
*/ |
137
|
2 |
|
public function setId(int $id): self |
138
|
|
|
{ |
139
|
2 |
|
$this->id = $id; |
140
|
|
|
|
141
|
2 |
|
return $this; |
142
|
|
|
} |
143
|
|
|
|
144
|
1 |
|
public function getId(): ?int |
145
|
|
|
{ |
146
|
1 |
|
return $this->id; |
147
|
|
|
} |
148
|
|
|
|
149
|
6 |
|
public function toArray(): array |
150
|
|
|
{ |
151
|
|
|
$array = [ |
152
|
6 |
|
'name' => $this->name, |
153
|
6 |
|
'street_1' => $this->street, |
154
|
6 |
|
'street_2' => $this->additionalStreet, |
155
|
6 |
|
'city' => $this->city, |
156
|
6 |
|
'postal_code' => $this->postalCode, |
157
|
6 |
|
'state' => $this->state, |
158
|
6 |
|
'country' => $this->country, |
159
|
6 |
|
'id' => $this->id, |
160
|
|
|
]; |
161
|
|
|
|
162
|
6 |
|
return array_filter($array); |
163
|
|
|
} |
164
|
|
|
|
165
|
6 |
|
public function jsonSerialize(): array |
166
|
|
|
{ |
167
|
6 |
|
return $this->toArray(); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @return \Kerox\Messenger\Model\Common\Address |
172
|
|
|
*/ |
173
|
3 |
|
public static function fromPayload(array $payload): self |
174
|
|
|
{ |
175
|
3 |
|
$address = self::create( |
176
|
3 |
|
$payload['street_1'], |
177
|
3 |
|
$payload['city'], |
178
|
3 |
|
$payload['postal_code'], |
179
|
3 |
|
$payload['state'], |
180
|
3 |
|
$payload['country'] |
181
|
|
|
); |
182
|
|
|
|
183
|
3 |
|
if (isset($payload['street_2'])) { |
184
|
3 |
|
$address->setAdditionalStreet($payload['street_2']); |
185
|
|
|
} |
186
|
|
|
|
187
|
3 |
|
if (isset($payload['id'])) { |
188
|
1 |
|
$address->setId($payload['id']); |
189
|
|
|
} |
190
|
|
|
|
191
|
3 |
|
if (isset($payload['name'])) { |
192
|
1 |
|
$address->setName($payload['name']); |
193
|
|
|
} |
194
|
|
|
|
195
|
3 |
|
return $address; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|