1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Part of the Saudi Address API PHP package. |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* Licensed under the MIT. |
9
|
|
|
* |
10
|
|
|
* This source file is subject to the MIT License that is |
11
|
|
|
* bundled with this package in the LICENSE file. |
12
|
|
|
* |
13
|
|
|
* @package Saudi Address |
14
|
|
|
* @version 1.2 |
15
|
|
|
* @author Ali Alharthi |
16
|
|
|
* @license MIT |
17
|
|
|
* @copyright (c) 2020, Ali Alharthi |
18
|
|
|
* @link https://aalharthi.sa |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AliAlharthi\SaudiAddress\Api; |
22
|
|
|
|
23
|
|
|
class Geo extends Api |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The response array. |
27
|
|
|
* |
28
|
|
|
* @var array|null |
29
|
|
|
*/ |
30
|
|
|
protected $response = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The cache directory. |
34
|
|
|
* |
35
|
|
|
* @var string|null |
36
|
|
|
*/ |
37
|
|
|
protected $cacheDir = __DIR__ . '/cache/'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The cache file name. |
41
|
|
|
* |
42
|
|
|
* @var string|null |
43
|
|
|
*/ |
44
|
|
|
protected $file = __DIR__ . '/cache/' . 'geo_'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Address reverse geo. |
48
|
|
|
* |
49
|
|
|
* @param string $latitude |
50
|
|
|
* @param string $longitude |
51
|
|
|
* @param string $lang |
52
|
|
|
* @return Geo |
53
|
|
|
*/ |
54
|
|
|
public function coordinates($latitude, $longitude, $lang = 'A') |
55
|
|
|
{ |
56
|
|
|
$cache = $this->file . $latitude . '_' . $longitude . '_' . strtolower($lang) . '.data'; |
57
|
|
|
|
58
|
|
|
$this->response = $this->cacheValue($cache); |
|
|
|
|
59
|
|
|
|
60
|
|
View Code Duplication |
if ($this->response == null) { |
|
|
|
|
61
|
|
|
$response = $this->_get( |
62
|
|
|
'v3.1/Address/address-geocode', |
63
|
|
|
$lang, |
64
|
|
|
[ |
65
|
|
|
'lat' => $latitude, |
66
|
|
|
'long' => $longitude |
67
|
|
|
] |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
if($this->config->getCache()){ |
71
|
|
|
(!file_exists($this->cacheDir)) ? |
72
|
|
|
mkdir($this->cacheDir, 0755, false): |
73
|
|
|
((file_exists($cache)) ? unlink($cache):touch($cache)); |
74
|
|
|
file_put_contents($cache, serialize($response)); |
75
|
|
|
} |
76
|
|
|
$this->response = $response; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Address reverse geo. |
84
|
|
|
* |
85
|
|
|
* @param string $latitude |
86
|
|
|
* @param string $longitude |
87
|
|
|
* @param string $lang |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function coords($latitude, $longitude, $lang = 'A') |
91
|
|
|
{ |
92
|
|
|
return $this->coordinates($latitude, $longitude, $lang); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Address reverse geo. |
97
|
|
|
* |
98
|
|
|
* @param string $latitude |
99
|
|
|
* @param string $longitude |
100
|
|
|
* @param string $lang |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
public function location($latitude, $longitude, $lang = 'A') |
104
|
|
|
{ |
105
|
|
|
return $this->coordinates($latitude, $longitude, $lang); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns a the response. |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
|
|
public function get() |
114
|
|
|
{ |
115
|
|
|
$this->check(); |
116
|
|
|
|
117
|
|
|
return $this->response['Addresses'][0]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Returns a the city name of reversed address. |
122
|
|
|
* |
123
|
|
|
* @return array |
124
|
|
|
*/ |
125
|
|
|
public function getCity() |
126
|
|
|
{ |
127
|
|
|
$this->check(); |
128
|
|
|
|
129
|
|
|
return $this->response['Addresses'][0]['City']; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Returns a the city name of reversed address. |
134
|
|
|
* |
135
|
|
|
* @return array |
136
|
|
|
*/ |
137
|
|
|
public function city() |
138
|
|
|
{ |
139
|
|
|
return $this->getCity(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns a the address 1 of reversed address. |
144
|
|
|
* |
145
|
|
|
* @return array |
146
|
|
|
*/ |
147
|
|
|
public function getAddressOne() |
148
|
|
|
{ |
149
|
|
|
$this->check(); |
150
|
|
|
|
151
|
|
|
return $this->response['Addresses'][0]['Address1']; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Returns a the address 1 of reversed address. |
156
|
|
|
* |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
|
|
public function addressOne() |
160
|
|
|
{ |
161
|
|
|
return $this->getAddressOne(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Returns a the address 2 of reversed address. |
166
|
|
|
* |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
|
|
public function getAddressTwo() |
170
|
|
|
{ |
171
|
|
|
$this->check(); |
172
|
|
|
|
173
|
|
|
return $this->response['Addresses'][0]['Address2']; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns a the address 2 of reversed address. |
178
|
|
|
* |
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
|
|
public function addressTwo() |
182
|
|
|
{ |
183
|
|
|
return $this->getAddressTwo(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Returns a the street name of reversed address. |
188
|
|
|
* |
189
|
|
|
* @return array |
190
|
|
|
*/ |
191
|
|
|
public function getStreet() |
192
|
|
|
{ |
193
|
|
|
$this->check(); |
194
|
|
|
|
195
|
|
|
return $this->response['Addresses'][0]['Street']; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Returns a the street name of reversed address. |
200
|
|
|
* |
201
|
|
|
* @return array |
202
|
|
|
*/ |
203
|
|
|
public function street() |
204
|
|
|
{ |
205
|
|
|
return $this->getStreet(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Returns a the region name of reversed address. |
210
|
|
|
* |
211
|
|
|
* @return array |
212
|
|
|
*/ |
213
|
|
|
public function getRegion() |
214
|
|
|
{ |
215
|
|
|
$this->check(); |
216
|
|
|
|
217
|
|
|
return $this->response['Addresses'][0]['RegionName']; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Returns a the region name of reversed address. |
222
|
|
|
* |
223
|
|
|
* @return array |
224
|
|
|
*/ |
225
|
|
|
public function region() |
226
|
|
|
{ |
227
|
|
|
return $this->getRegion(); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Returns a the district name of reversed address. |
232
|
|
|
* |
233
|
|
|
* @return array |
234
|
|
|
*/ |
235
|
|
|
public function getDistrict() |
236
|
|
|
{ |
237
|
|
|
$this->check(); |
238
|
|
|
|
239
|
|
|
return $this->response['Addresses'][0]['District']; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Returns a the district name of reversed address. |
244
|
|
|
* |
245
|
|
|
* @return array |
246
|
|
|
*/ |
247
|
|
|
public function district() |
248
|
|
|
{ |
249
|
|
|
return $this->getDistrict(); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Returns a the building number name of reversed address. |
254
|
|
|
* |
255
|
|
|
* @return array |
256
|
|
|
*/ |
257
|
|
|
public function getBuildingNumber() |
258
|
|
|
{ |
259
|
|
|
$this->check(); |
260
|
|
|
|
261
|
|
|
return $this->response['Addresses'][0]['BuildingNumber']; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Returns a the building number name of reversed address. |
266
|
|
|
* |
267
|
|
|
* @return array |
268
|
|
|
*/ |
269
|
|
|
public function buildingNumber() |
270
|
|
|
{ |
271
|
|
|
return $this->getBuildingNumber(); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Returns a the post code name of reversed address. |
276
|
|
|
* |
277
|
|
|
* @return array |
278
|
|
|
*/ |
279
|
|
|
public function getPostCode() |
280
|
|
|
{ |
281
|
|
|
$this->check(); |
282
|
|
|
|
283
|
|
|
return $this->response['Addresses'][0]['PostCode']; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Returns a the post code name of reversed address. |
288
|
|
|
* |
289
|
|
|
* @return array |
290
|
|
|
*/ |
291
|
|
|
public function postCode() |
292
|
|
|
{ |
293
|
|
|
return $this->getPostCode(); |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Returns a the post code name of reversed address. |
298
|
|
|
* |
299
|
|
|
* @return array |
300
|
|
|
*/ |
301
|
|
|
public function getZip() |
302
|
|
|
{ |
303
|
|
|
return $this->getPostCode(); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
/** |
307
|
|
|
* Returns a the post code name of reversed address. |
308
|
|
|
* |
309
|
|
|
* @return array |
310
|
|
|
*/ |
311
|
|
|
public function zip() |
312
|
|
|
{ |
313
|
|
|
return $this->getPostCode(); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* Returns a the additional number name of reversed address. |
318
|
|
|
* |
319
|
|
|
* @return array |
320
|
|
|
*/ |
321
|
|
|
public function getAdditionalNumber() |
322
|
|
|
{ |
323
|
|
|
$this->check(); |
324
|
|
|
|
325
|
|
|
return $this->response['Addresses'][0]['AdditionalNumber']; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Returns a the additional number name of reversed address. |
330
|
|
|
* |
331
|
|
|
* @return array |
332
|
|
|
*/ |
333
|
|
|
public function additionalNumber() |
334
|
|
|
{ |
335
|
|
|
return $this->getAdditionalNumber(); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Check if coordinates() method was called first. |
340
|
|
|
* |
341
|
|
|
* @return void |
342
|
|
|
* @throws \BadMethodCallException |
343
|
|
|
*/ |
344
|
|
|
protected function check() |
345
|
|
|
{ |
346
|
|
|
if ($this->response == null) { |
347
|
|
|
throw new \BadMethodCallException("You need to call coordinates() method first."); |
348
|
|
|
} |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.