1
|
|
|
<?php |
2
|
|
|
namespace KamranAhmed\Geocode; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Location |
6
|
|
|
* |
7
|
|
|
* Represents the location details obtained from the Geocoding |
8
|
|
|
* Service |
9
|
|
|
*/ |
10
|
|
|
class Location |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string Address to which the detail belong |
14
|
|
|
*/ |
15
|
|
|
private $address = ''; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string Latitude of the location |
19
|
|
|
*/ |
20
|
|
|
private $latitude = ''; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string Longitude of the location |
24
|
|
|
*/ |
25
|
|
|
private $longitude = ''; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string Country of the location |
29
|
|
|
*/ |
30
|
|
|
private $country = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string Locality of the location |
34
|
|
|
*/ |
35
|
|
|
private $locality = ''; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string District of the location |
39
|
|
|
*/ |
40
|
|
|
private $district = ''; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string Postal code of the location |
44
|
|
|
*/ |
45
|
|
|
private $postcode = ''; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var string Town of the location |
49
|
|
|
*/ |
50
|
|
|
private $town = ''; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var string Street number |
54
|
|
|
*/ |
55
|
|
|
private $streetNumber = ''; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @var string Street address |
59
|
|
|
*/ |
60
|
|
|
private $streetAddress = ''; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @var string Neighborhood address |
64
|
|
|
*/ |
65
|
|
|
private $neighborhood = ''; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @var string Short Country of the location |
69
|
|
|
*/ |
70
|
|
|
private $shortCountry = ''; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @var string Short Locality of the location |
74
|
|
|
*/ |
75
|
|
|
private $shortLocality = ''; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @var string Short District of the location |
79
|
|
|
*/ |
80
|
|
|
private $shortDistrict = ''; |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @var string Short Town of the location |
84
|
|
|
*/ |
85
|
|
|
private $shortTown = ''; |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @var string Short Street address |
89
|
|
|
*/ |
90
|
|
|
private $shortStreetAddress = ''; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @var string Short Neighborhood address |
94
|
|
|
*/ |
95
|
|
|
private $shortNeighborhood = ''; |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @var boolean Whether the location is valid or not |
99
|
|
|
*/ |
100
|
|
|
private $isValid = true; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Create a new Location object |
104
|
|
|
* |
105
|
|
|
* @param string $address Address whose detail it is |
106
|
|
|
* @param \stdClass $dataFromService The data retrieved from the Geocoding service |
107
|
|
|
*/ |
108
|
3 |
|
public function __construct($address, \stdClass $dataFromService) |
109
|
|
|
{ |
110
|
3 |
|
$this->address = $address; |
111
|
3 |
|
$this->populateDetail($dataFromService); |
112
|
3 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Checks whether the data passed to the class was valid |
116
|
|
|
* |
117
|
|
|
* @return boolean True if the data is valid and false otherwise |
118
|
|
|
*/ |
119
|
3 |
|
public function isValid() |
120
|
|
|
{ |
121
|
3 |
|
return $this->isValid; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Populates the object with the detail from the service |
126
|
|
|
* |
127
|
|
|
* @param \stdClass $locationDetail The address detail i.e. which was retrieved from the API |
128
|
|
|
* |
129
|
|
|
* @return boolean True if successfully populated the detail and false otherwise |
130
|
|
|
*/ |
131
|
3 |
|
private function populateDetail(\stdClass $locationDetail) |
132
|
|
|
{ |
133
|
|
|
// The data from the API is returned under the `results` key |
134
|
3 |
|
if (!property_exists($locationDetail, 'results')) { |
135
|
2 |
|
$this->isValid = false; |
136
|
|
|
|
137
|
2 |
|
return false; |
138
|
|
|
} |
139
|
|
|
|
140
|
1 |
|
$this->latitude = $locationDetail->results[0]->geometry->location->lat; |
141
|
1 |
|
$this->longitude = $locationDetail->results[0]->geometry->location->lng; |
142
|
|
|
|
143
|
1 |
|
foreach ($locationDetail->results[0]->address_components as $component) { |
144
|
1 |
|
if (in_array('street_number', $component->types)) { |
145
|
1 |
|
$this->streetNumber = $component->long_name; |
146
|
1 |
|
} elseif (in_array('locality', $component->types)) { |
147
|
1 |
|
$this->locality = $component->long_name; |
148
|
1 |
|
$this->shortLocality = $component->short_name; |
149
|
1 |
|
} elseif (in_array('postal_town', $component->types)) { |
150
|
|
|
$this->town = $component->long_name; |
151
|
|
|
$this->shortTown = $component->short_name; |
152
|
1 |
|
} elseif (in_array('administrative_area_level_2', $component->types)) { |
153
|
1 |
|
$this->country = $component->long_name; |
154
|
1 |
|
$this->shortCountry = $component->short_name; |
155
|
1 |
|
} elseif (in_array('country', $component->types)) { |
156
|
1 |
|
$this->country = $component->long_name; |
157
|
1 |
|
$this->shortCountry = $component->short_name; |
158
|
1 |
|
} elseif (in_array('administrative_area_level_1', $component->types)) { |
159
|
1 |
|
$this->district = $component->long_name; |
160
|
1 |
|
$this->shortDistrict = $component->short_name; |
161
|
1 |
|
} elseif (in_array('postal_code', $component->types)) { |
162
|
1 |
|
$this->postcode = $component->long_name; |
163
|
1 |
|
} elseif (in_array('route', $component->types)) { |
164
|
1 |
|
$this->streetAddress = $component->long_name; |
165
|
1 |
|
$this->shortStreetAddress = $component->short_name; |
166
|
1 |
|
} elseif (in_array('political', $component->types)) { |
167
|
1 |
|
$this->neighborhood = $component->long_name; |
168
|
1 |
|
$this->shortNeighborhood = $component->short_name; |
169
|
1 |
|
} |
170
|
1 |
|
} |
171
|
|
|
|
172
|
1 |
|
return true; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Gets the address |
177
|
|
|
* |
178
|
|
|
* @param string $default |
179
|
|
|
* |
180
|
|
|
* @return string |
181
|
|
|
*/ |
182
|
1 |
|
public function getAddress($default = '') |
183
|
|
|
{ |
184
|
1 |
|
return $this->address ?: $default; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Gets the latitude of the location |
189
|
|
|
* |
190
|
|
|
* @param string $default |
191
|
|
|
* |
192
|
|
|
* @return string |
193
|
|
|
*/ |
194
|
|
|
public function getLatitude($default = '') |
195
|
|
|
{ |
196
|
|
|
return $this->latitude ?: $default; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Gets the longitude of the location |
201
|
|
|
* |
202
|
|
|
* @param string $default |
203
|
|
|
* |
204
|
|
|
* @return string |
205
|
|
|
*/ |
206
|
|
|
public function getLongitude($default = '') |
207
|
|
|
{ |
208
|
|
|
return $this->longitude ?: $default; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Gets the country of the location |
213
|
|
|
* |
214
|
|
|
* @param string $default |
215
|
|
|
* |
216
|
|
|
* @return string |
217
|
|
|
*/ |
218
|
1 |
|
public function getCountry($default = '') |
219
|
|
|
{ |
220
|
1 |
|
return $this->country ?: $default; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* Gets the short country of the location |
225
|
|
|
* |
226
|
|
|
* @param string $default |
227
|
|
|
* |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
1 |
|
public function getShortCountry($default = '') |
231
|
|
|
{ |
232
|
1 |
|
return $this->shortCountry ?: $default; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Gets the locality of the location |
237
|
|
|
* |
238
|
|
|
* @param string $default |
239
|
|
|
* |
240
|
|
|
* @return string |
241
|
|
|
*/ |
242
|
1 |
|
public function getLocality($default = '') |
243
|
|
|
{ |
244
|
1 |
|
return $this->locality ?: $default; |
245
|
|
|
} |
246
|
|
|
/** |
247
|
|
|
* Gets the short locality of the location |
248
|
|
|
* |
249
|
|
|
* @param string $default |
250
|
|
|
* |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
1 |
|
public function getShortLocality($default = '') |
254
|
|
|
{ |
255
|
1 |
|
return $this->shortLocality ?: $default; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Gets the district of the location |
260
|
|
|
* |
261
|
|
|
* @param string $default |
262
|
|
|
* |
263
|
|
|
* @return string |
264
|
|
|
*/ |
265
|
1 |
|
public function getDistrict($default = '') |
266
|
|
|
{ |
267
|
1 |
|
return $this->district ?: $default; |
268
|
|
|
} |
269
|
|
|
/** |
270
|
|
|
* Gets the short district of the location |
271
|
|
|
* |
272
|
|
|
* @param string $default |
273
|
|
|
* |
274
|
|
|
* @return string |
275
|
|
|
*/ |
276
|
1 |
|
public function getShortDistrict($default = '') |
277
|
|
|
{ |
278
|
1 |
|
return $this->shortDistrict ?: $default; |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
/** |
282
|
|
|
* Gets the post code for the location |
283
|
|
|
* |
284
|
|
|
* @param string $default |
285
|
|
|
* |
286
|
|
|
* @return string |
287
|
|
|
*/ |
288
|
1 |
|
public function getPostcode($default = '') |
289
|
|
|
{ |
290
|
1 |
|
return $this->postcode ?: $default; |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* Gets the town for the location |
295
|
|
|
* |
296
|
|
|
* @param string $default |
297
|
|
|
* |
298
|
|
|
* @return string |
299
|
|
|
*/ |
300
|
1 |
|
public function getTown($default = '') |
301
|
|
|
{ |
302
|
1 |
|
return $this->town ?: $default; |
303
|
|
|
} |
304
|
|
|
/** |
305
|
|
|
* Gets the short town of the location |
306
|
|
|
* |
307
|
|
|
* @param string $default |
308
|
|
|
* |
309
|
|
|
* @return string |
310
|
|
|
*/ |
311
|
|
|
public function getShortTown($default = '') |
312
|
|
|
{ |
313
|
|
|
return $this->shortTown ?: $default; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Gets the street number for the location |
318
|
|
|
* |
319
|
|
|
* @param string $default |
320
|
|
|
* |
321
|
|
|
* @return string |
322
|
|
|
*/ |
323
|
1 |
|
public function getStreetNumber($default = '') |
324
|
|
|
{ |
325
|
1 |
|
return $this->streetNumber ?: $default; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Gets the street address |
330
|
|
|
* |
331
|
|
|
* @param string $default |
332
|
|
|
* |
333
|
|
|
* @return string |
334
|
|
|
*/ |
335
|
|
|
public function getStreetAddress($default = '') |
336
|
|
|
{ |
337
|
|
|
return $this->streetAddress ?: $default; |
338
|
|
|
} |
339
|
|
|
/** |
340
|
|
|
* Gets the short street address of the location |
341
|
|
|
* |
342
|
|
|
* @param string $default |
343
|
|
|
* |
344
|
|
|
* @return string |
345
|
|
|
*/ |
346
|
|
|
public function getShortStreetAddress($default = '') |
347
|
|
|
{ |
348
|
|
|
return $this->shortStreetAddress ?: $default; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Gets the neighborhood address |
353
|
|
|
* |
354
|
|
|
* @param string $default |
355
|
|
|
* |
356
|
|
|
* @return string |
357
|
|
|
*/ |
358
|
|
|
public function getNeighborhood($default = '') |
359
|
|
|
{ |
360
|
|
|
return $this->neighborhood ?: $default; |
361
|
|
|
} |
362
|
|
|
/** |
363
|
|
|
* Gets the short neighborhood address of the location |
364
|
|
|
* |
365
|
|
|
* @param string $default |
366
|
|
|
* |
367
|
|
|
* @return string |
368
|
|
|
*/ |
369
|
|
|
public function getShortNeighborhood($default = '') |
370
|
|
|
{ |
371
|
|
|
return $this->shortNeighborhood ?: $default; |
372
|
|
|
} |
373
|
|
|
} |
374
|
|
|
|