|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Geocoder package. |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
* |
|
10
|
|
|
* @license MIT License |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace Geocoder\Model; |
|
14
|
|
|
|
|
15
|
|
|
use Geocoder\Location; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @author William Durand <[email protected]> |
|
19
|
|
|
*/ |
|
20
|
|
|
class Address implements Location |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @var Coordinates|null |
|
24
|
|
|
*/ |
|
25
|
|
|
private $coordinates; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Bounds|null |
|
29
|
|
|
*/ |
|
30
|
|
|
private $bounds; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string|int|null |
|
34
|
|
|
*/ |
|
35
|
|
|
private $streetNumber; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var string|null |
|
39
|
|
|
*/ |
|
40
|
|
|
private $streetName; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var string|null |
|
44
|
|
|
*/ |
|
45
|
|
|
private $subLocality; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string|null |
|
49
|
|
|
*/ |
|
50
|
|
|
private $locality; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var string|null |
|
54
|
|
|
*/ |
|
55
|
|
|
private $postalCode; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var AdminLevelCollection |
|
59
|
|
|
*/ |
|
60
|
|
|
private $adminLevels; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var Country|null |
|
64
|
|
|
*/ |
|
65
|
|
|
private $country; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var string|null |
|
69
|
|
|
*/ |
|
70
|
|
|
private $timezone; |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @var string |
|
74
|
|
|
*/ |
|
75
|
|
|
private $providedBy; |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $providedBy |
|
79
|
|
|
* @param AdminLevelCollection $adminLevels |
|
80
|
|
|
* @param Coordinates|null $coordinates |
|
81
|
|
|
* @param Bounds|null $bounds |
|
82
|
|
|
* @param string|null $streetNumber |
|
83
|
|
|
* @param string|null $streetName |
|
84
|
|
|
* @param string|null $postalCode |
|
85
|
|
|
* @param string|null $locality |
|
86
|
|
|
* @param string|null $subLocality |
|
87
|
|
|
* @param Country|null $country |
|
88
|
|
|
* @param string|null $timezone |
|
89
|
|
|
*/ |
|
90
|
35 |
|
public function __construct( |
|
91
|
|
|
string $providedBy, |
|
92
|
|
|
AdminLevelCollection $adminLevels, |
|
93
|
|
|
Coordinates $coordinates = null, |
|
94
|
|
|
Bounds $bounds = null, |
|
95
|
|
|
string $streetNumber = null, |
|
96
|
|
|
string $streetName = null, |
|
97
|
|
|
string $postalCode = null, |
|
98
|
|
|
string $locality = null, |
|
99
|
|
|
string $subLocality = null, |
|
100
|
|
|
Country $country = null, |
|
101
|
|
|
string $timezone = null |
|
102
|
|
|
) { |
|
103
|
35 |
|
$this->providedBy = $providedBy; |
|
104
|
35 |
|
$this->adminLevels = $adminLevels; |
|
105
|
35 |
|
$this->coordinates = $coordinates; |
|
106
|
35 |
|
$this->bounds = $bounds; |
|
107
|
35 |
|
$this->streetNumber = $streetNumber; |
|
108
|
35 |
|
$this->streetName = $streetName; |
|
109
|
35 |
|
$this->postalCode = $postalCode; |
|
110
|
35 |
|
$this->locality = $locality; |
|
111
|
35 |
|
$this->subLocality = $subLocality; |
|
112
|
35 |
|
$this->country = $country; |
|
113
|
35 |
|
$this->timezone = $timezone; |
|
114
|
35 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return string |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getProvidedBy(): string |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->providedBy; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* {@inheritdoc} |
|
126
|
|
|
*/ |
|
127
|
20 |
|
public function getCoordinates() |
|
128
|
|
|
{ |
|
129
|
20 |
|
return $this->coordinates; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* {@inheritdoc} |
|
134
|
|
|
*/ |
|
135
|
12 |
|
public function getBounds() |
|
136
|
|
|
{ |
|
137
|
12 |
|
return $this->bounds; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* {@inheritdoc} |
|
142
|
|
|
*/ |
|
143
|
15 |
|
public function getStreetNumber() |
|
144
|
|
|
{ |
|
145
|
15 |
|
return $this->streetNumber; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* {@inheritdoc} |
|
150
|
|
|
*/ |
|
151
|
15 |
|
public function getStreetName() |
|
152
|
|
|
{ |
|
153
|
15 |
|
return $this->streetName; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* {@inheritdoc} |
|
158
|
|
|
*/ |
|
159
|
15 |
|
public function getLocality() |
|
160
|
|
|
{ |
|
161
|
15 |
|
return $this->locality; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* {@inheritdoc} |
|
166
|
|
|
*/ |
|
167
|
15 |
|
public function getPostalCode() |
|
168
|
|
|
{ |
|
169
|
15 |
|
return $this->postalCode; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* {@inheritdoc} |
|
174
|
|
|
*/ |
|
175
|
15 |
|
public function getSubLocality() |
|
176
|
|
|
{ |
|
177
|
15 |
|
return $this->subLocality; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* {@inheritdoc} |
|
182
|
|
|
*/ |
|
183
|
15 |
|
public function getAdminLevels(): AdminLevelCollection |
|
184
|
|
|
{ |
|
185
|
15 |
|
return $this->adminLevels; |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* {@inheritdoc} |
|
190
|
|
|
*/ |
|
191
|
31 |
|
public function getCountry() |
|
192
|
|
|
{ |
|
193
|
31 |
|
return $this->country; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
/** |
|
197
|
|
|
* {@inheritdoc} |
|
198
|
|
|
*/ |
|
199
|
15 |
|
public function getTimezone() |
|
200
|
|
|
{ |
|
201
|
15 |
|
return $this->timezone; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Create an Address with an array. Useful for testing. |
|
206
|
|
|
* |
|
207
|
|
|
* @param array $data |
|
208
|
|
|
* |
|
209
|
|
|
* @return static |
|
210
|
|
|
*/ |
|
211
|
34 |
|
public static function createFromArray(array $data) |
|
212
|
|
|
{ |
|
213
|
|
|
$defaults = [ |
|
214
|
34 |
|
'providedBy' => 'n/a', |
|
215
|
|
|
'latitude' => null, |
|
216
|
|
|
'longitude' => null, |
|
217
|
|
|
'bounds' => [ |
|
218
|
|
|
'south' => null, |
|
219
|
|
|
'west' => null, |
|
220
|
|
|
'north' => null, |
|
221
|
|
|
'east' => null, |
|
222
|
|
|
], |
|
223
|
|
|
'streetNumber' => null, |
|
224
|
|
|
'streetName' => null, |
|
225
|
|
|
'locality' => null, |
|
226
|
|
|
'postalCode' => null, |
|
227
|
|
|
'subLocality' => null, |
|
228
|
|
|
'adminLevels' => [], |
|
229
|
|
|
'country' => null, |
|
230
|
|
|
'countryCode' => null, |
|
231
|
|
|
'timezone' => null, |
|
232
|
|
|
]; |
|
233
|
|
|
|
|
234
|
34 |
|
$data = array_merge($defaults, $data); |
|
235
|
|
|
|
|
236
|
34 |
|
$adminLevels = []; |
|
237
|
34 |
|
foreach ($data['adminLevels'] as $adminLevel) { |
|
238
|
6 |
|
if (empty($adminLevel['level'])) { |
|
239
|
|
|
continue; |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
6 |
|
$name = $adminLevel['name'] ?? $adminLevel['code'] ?? null; |
|
243
|
6 |
|
if (empty($name)) { |
|
244
|
|
|
continue; |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
6 |
|
$adminLevels[] = new AdminLevel( |
|
248
|
6 |
|
$adminLevel['level'], |
|
249
|
|
|
$name, |
|
250
|
6 |
|
$adminLevel['code'] ?? null |
|
251
|
|
|
); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
34 |
|
return new static( |
|
255
|
34 |
|
$data['providedBy'], |
|
256
|
34 |
|
new AdminLevelCollection($adminLevels), |
|
257
|
34 |
|
self::createCoordinates( |
|
258
|
34 |
|
$data['latitude'], |
|
259
|
34 |
|
$data['longitude'] |
|
260
|
|
|
), |
|
261
|
34 |
|
self::createBounds( |
|
262
|
34 |
|
$data['bounds']['south'], |
|
263
|
34 |
|
$data['bounds']['west'], |
|
264
|
34 |
|
$data['bounds']['north'], |
|
265
|
34 |
|
$data['bounds']['east'] |
|
266
|
|
|
), |
|
267
|
34 |
|
$data['streetNumber'], |
|
268
|
34 |
|
$data['streetName'], |
|
269
|
34 |
|
$data['postalCode'], |
|
270
|
34 |
|
$data['locality'], |
|
271
|
34 |
|
$data['subLocality'], |
|
272
|
34 |
|
new Country( |
|
273
|
34 |
|
$data['country'], |
|
274
|
34 |
|
$data['countryCode'] |
|
275
|
|
|
), |
|
276
|
34 |
|
$data['timezone'] |
|
277
|
|
|
); |
|
278
|
|
|
} |
|
279
|
|
|
|
|
280
|
|
|
/** |
|
281
|
|
|
* @param float $latitude |
|
282
|
|
|
* @param float $longitude |
|
283
|
|
|
* |
|
284
|
|
|
* @return Coordinates|null |
|
285
|
|
|
*/ |
|
286
|
34 |
|
private static function createCoordinates($latitude, $longitude) |
|
287
|
|
|
{ |
|
288
|
34 |
|
if (null === $latitude || null === $longitude) { |
|
289
|
21 |
|
return null; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
13 |
|
return new Coordinates($latitude, $longitude); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* @param float $south |
|
297
|
|
|
* @param float $west |
|
298
|
|
|
* @param float $north |
|
299
|
|
|
* |
|
300
|
|
|
* @return Bounds|null |
|
301
|
|
|
*/ |
|
302
|
34 |
|
private static function createBounds($south, $west, $north, $east) |
|
303
|
|
|
{ |
|
304
|
34 |
|
if (null === $south || null === $west || null === $north || null === $east) { |
|
305
|
27 |
|
return null; |
|
306
|
|
|
} |
|
307
|
|
|
|
|
308
|
7 |
|
return new Bounds($south, $west, $north, $east); |
|
309
|
|
|
} |
|
310
|
|
|
|
|
311
|
|
|
/** |
|
312
|
|
|
* {@inheritdoc} |
|
313
|
|
|
*/ |
|
314
|
16 |
|
public function toArray(): array |
|
315
|
|
|
{ |
|
316
|
16 |
|
$adminLevels = []; |
|
317
|
16 |
|
foreach ($this->adminLevels as $adminLevel) { |
|
318
|
2 |
|
$adminLevels[$adminLevel->getLevel()] = [ |
|
319
|
2 |
|
'name' => $adminLevel->getName(), |
|
320
|
2 |
|
'code' => $adminLevel->getCode(), |
|
321
|
2 |
|
'level' => $adminLevel->getLevel(), |
|
322
|
|
|
]; |
|
323
|
|
|
} |
|
324
|
|
|
|
|
325
|
16 |
|
$lat = null; |
|
326
|
16 |
|
$lon = null; |
|
327
|
16 |
|
if (null !== $coordinates = $this->getCoordinates()) { |
|
328
|
11 |
|
$lat = $coordinates->getLatitude(); |
|
329
|
11 |
|
$lon = $coordinates->getLongitude(); |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
16 |
|
$countryName = null; |
|
333
|
16 |
|
$countryCode = null; |
|
334
|
16 |
|
if (null !== $country = $this->getCountry()) { |
|
335
|
15 |
|
$countryName = $country->getName(); |
|
336
|
15 |
|
$countryCode = $country->getCode(); |
|
337
|
|
|
} |
|
338
|
|
|
|
|
339
|
|
|
$noBounds = [ |
|
340
|
16 |
|
'south' => null, |
|
341
|
|
|
'west' => null, |
|
342
|
|
|
'north' => null, |
|
343
|
|
|
'east' => null, |
|
344
|
|
|
]; |
|
345
|
|
|
|
|
346
|
|
|
return [ |
|
347
|
16 |
|
'providedBy' => $this->providedBy, |
|
348
|
16 |
|
'latitude' => $lat, |
|
349
|
16 |
|
'longitude' => $lon, |
|
350
|
16 |
|
'bounds' => null !== $this->bounds ? $this->bounds->toArray() : $noBounds, |
|
351
|
16 |
|
'streetNumber' => $this->streetNumber, |
|
352
|
16 |
|
'streetName' => $this->streetName, |
|
353
|
16 |
|
'postalCode' => $this->postalCode, |
|
354
|
16 |
|
'locality' => $this->locality, |
|
355
|
16 |
|
'subLocality' => $this->subLocality, |
|
356
|
16 |
|
'adminLevels' => $adminLevels, |
|
357
|
16 |
|
'country' => $countryName, |
|
358
|
16 |
|
'countryCode' => $countryCode, |
|
359
|
16 |
|
'timezone' => $this->timezone, |
|
360
|
|
|
]; |
|
361
|
|
|
} |
|
362
|
|
|
} |
|
363
|
|
|
|