|
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 2.0 |
|
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
|
|
|
* Constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param \AliAlharthi\SaudiAddress\ConfigInterface $config |
|
50
|
|
|
* @param string $latitude |
|
51
|
|
|
* @param string $longitude |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct($config, $latitude, $longitude) |
|
54
|
|
|
{ |
|
55
|
|
|
|
|
56
|
|
|
parent::__construct($config); |
|
57
|
|
|
$cache = $this->file . $latitude . '_' . $longitude . '_' . strtolower($this->config->getLocale()) . '.data'; |
|
58
|
|
|
|
|
59
|
|
|
$this->response = $this->cacheValue($cache); |
|
60
|
|
|
|
|
61
|
|
|
if ($this->response == null) { |
|
62
|
|
|
$response = $this->_get( |
|
63
|
|
|
'v3.1/Address/address-geocode', |
|
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
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns a the response. |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
|
|
public function get() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->response['Addresses'][0]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Returns a the response. |
|
94
|
|
|
* |
|
95
|
|
|
* @return array |
|
96
|
|
|
*/ |
|
97
|
|
|
public function all() |
|
98
|
|
|
{ |
|
99
|
|
|
return $this->get(); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Returns a the city name of reversed address. |
|
104
|
|
|
* |
|
105
|
|
|
* @return array |
|
106
|
|
|
*/ |
|
107
|
|
|
public function getCity() |
|
108
|
|
|
{ |
|
109
|
|
|
return $this->response['Addresses'][0]['City']; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Returns a the city name of reversed address. |
|
114
|
|
|
* |
|
115
|
|
|
* @return array |
|
116
|
|
|
*/ |
|
117
|
|
|
public function city() |
|
118
|
|
|
{ |
|
119
|
|
|
return $this->getCity(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns a the address 1 of reversed address. |
|
124
|
|
|
* |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getAddressOne() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->response['Addresses'][0]['Address1']; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Returns a the address 1 of reversed address. |
|
134
|
|
|
* |
|
135
|
|
|
* @return array |
|
136
|
|
|
*/ |
|
137
|
|
|
public function addressOne() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->getAddressOne(); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns a the address 2 of reversed address. |
|
144
|
|
|
* |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
|
|
public function getAddressTwo() |
|
148
|
|
|
{ |
|
149
|
|
|
return $this->response['Addresses'][0]['Address2']; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Returns a the address 2 of reversed address. |
|
154
|
|
|
* |
|
155
|
|
|
* @return array |
|
156
|
|
|
*/ |
|
157
|
|
|
public function addressTwo() |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->getAddressTwo(); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Returns a the street name of reversed address. |
|
164
|
|
|
* |
|
165
|
|
|
* @return array |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getStreet() |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->response['Addresses'][0]['Street']; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Returns a the street name of reversed address. |
|
174
|
|
|
* |
|
175
|
|
|
* @return array |
|
176
|
|
|
*/ |
|
177
|
|
|
public function street() |
|
178
|
|
|
{ |
|
179
|
|
|
return $this->getStreet(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Returns a the region name of reversed address. |
|
184
|
|
|
* |
|
185
|
|
|
* @return array |
|
186
|
|
|
*/ |
|
187
|
|
|
public function getRegion() |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->response['Addresses'][0]['RegionName']; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Returns a the region name of reversed address. |
|
194
|
|
|
* |
|
195
|
|
|
* @return array |
|
196
|
|
|
*/ |
|
197
|
|
|
public function region() |
|
198
|
|
|
{ |
|
199
|
|
|
return $this->getRegion(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Returns a the district name of reversed address. |
|
204
|
|
|
* |
|
205
|
|
|
* @return array |
|
206
|
|
|
*/ |
|
207
|
|
|
public function getDistrict() |
|
208
|
|
|
{ |
|
209
|
|
|
return $this->response['Addresses'][0]['District']; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
/** |
|
213
|
|
|
* Returns a the district name of reversed address. |
|
214
|
|
|
* |
|
215
|
|
|
* @return array |
|
216
|
|
|
*/ |
|
217
|
|
|
public function district() |
|
218
|
|
|
{ |
|
219
|
|
|
return $this->getDistrict(); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Returns a the building number name of reversed address. |
|
224
|
|
|
* |
|
225
|
|
|
* @return array |
|
226
|
|
|
*/ |
|
227
|
|
|
public function getBuildingNumber() |
|
228
|
|
|
{ |
|
229
|
|
|
return $this->response['Addresses'][0]['BuildingNumber']; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Returns a the building number name of reversed address. |
|
234
|
|
|
* |
|
235
|
|
|
* @return array |
|
236
|
|
|
*/ |
|
237
|
|
|
public function buildingNumber() |
|
238
|
|
|
{ |
|
239
|
|
|
return $this->getBuildingNumber(); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Returns a the post code name of reversed address. |
|
244
|
|
|
* |
|
245
|
|
|
* @return array |
|
246
|
|
|
*/ |
|
247
|
|
|
public function getPostCode() |
|
248
|
|
|
{ |
|
249
|
|
|
return $this->response['Addresses'][0]['PostCode']; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* Returns a the post code name of reversed address. |
|
254
|
|
|
* |
|
255
|
|
|
* @return array |
|
256
|
|
|
*/ |
|
257
|
|
|
public function postCode() |
|
258
|
|
|
{ |
|
259
|
|
|
return $this->getPostCode(); |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* Returns a the post code name of reversed address. |
|
264
|
|
|
* |
|
265
|
|
|
* @return array |
|
266
|
|
|
*/ |
|
267
|
|
|
public function getZip() |
|
268
|
|
|
{ |
|
269
|
|
|
return $this->getPostCode(); |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* Returns a the post code name of reversed address. |
|
274
|
|
|
* |
|
275
|
|
|
* @return array |
|
276
|
|
|
*/ |
|
277
|
|
|
public function zip() |
|
278
|
|
|
{ |
|
279
|
|
|
return $this->getPostCode(); |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* Returns a the additional number name of reversed address. |
|
284
|
|
|
* |
|
285
|
|
|
* @return array |
|
286
|
|
|
*/ |
|
287
|
|
|
public function getAdditionalNumber() |
|
288
|
|
|
{ |
|
289
|
|
|
return $this->response['Addresses'][0]['AdditionalNumber']; |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Returns a the additional number name of reversed address. |
|
294
|
|
|
* |
|
295
|
|
|
* @return array |
|
296
|
|
|
*/ |
|
297
|
|
|
public function additionalNumber() |
|
298
|
|
|
{ |
|
299
|
|
|
return $this->getAdditionalNumber(); |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
} |
|
303
|
|
|
|