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
|
|
|
use AliAlharthi\SaudiAddress\ConfigInterface; |
24
|
|
|
|
25
|
|
|
class Address extends Api |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The response array. |
29
|
|
|
* |
30
|
|
|
* @var array|null |
31
|
|
|
*/ |
32
|
|
|
protected $response = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The cache directory. |
36
|
|
|
* |
37
|
|
|
* @var string|null |
38
|
|
|
*/ |
39
|
|
|
protected $cacheDir = __DIR__ . '/cache/'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The cache file name. |
43
|
|
|
* |
44
|
|
|
* @var string|null |
45
|
|
|
*/ |
46
|
|
|
protected $file = __DIR__ . '/cache/' . 'address_'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Constructor. |
50
|
|
|
* |
51
|
|
|
* @param \AliAlharthi\SaudiAddress\ConfigInterface $config |
52
|
|
|
*/ |
53
|
|
|
public function __construct($config) |
54
|
|
|
{ |
55
|
|
|
parent::__construct($config); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Free text address search. |
60
|
|
|
* |
61
|
|
|
* @param string $address |
62
|
|
|
* @param int $page |
63
|
|
|
* @return Address |
64
|
|
|
*/ |
65
|
|
|
public function search($address, $page = 1) |
66
|
|
|
{ |
67
|
|
|
$cache = $this->file . preg_replace('/[^a-zA-Z0-9_]/', '_', $address) . '_' . strtolower($this->config->getLocale()) . '.data'; |
68
|
|
|
|
69
|
|
|
$this->response = $this->cacheValue($cache); |
70
|
|
|
|
71
|
|
|
if ($this->response == null) { |
72
|
|
|
$response = $this->_get( |
73
|
|
|
'v4/Address/address-free-text', |
74
|
|
|
[ |
75
|
|
|
'addressstring' => $address, |
76
|
|
|
'page' => $page, |
77
|
|
|
], |
78
|
|
|
false |
79
|
|
|
); |
80
|
|
|
if ($response['success'] == false) { |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
$addresses = $response['Addresses']; |
84
|
|
|
$pages = (int) ceil($response['totalSearchResults'] / 20); |
85
|
|
|
if ($page == 1 && ($pages > 1)) { |
86
|
|
|
for ($i = 2; $i <= $pages; $i++) { |
87
|
|
|
// 1 call allowed per 5 seconds (on Development subscription) |
88
|
|
|
($this->config->getApiSubscription() == 'Development') ? sleep(5) : ''; |
89
|
|
|
$pageData = $this->_get( |
90
|
|
|
'v4/Address/address-free-text', |
91
|
|
|
[ |
92
|
|
|
'addressstring' => $address, |
93
|
|
|
'page' => $i, |
94
|
|
|
], |
95
|
|
|
false |
96
|
|
|
); |
97
|
|
|
|
98
|
|
|
$pageData = $pageData['Addresses']; |
99
|
|
|
$pageData = array_combine(range(($i * 10), count($pageData) + (($i * 10) - 1)), array_values($pageData)); |
100
|
|
|
$addresses += $pageData; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
if($this->config->getCache()){ |
104
|
|
|
(!file_exists($this->cacheDir)) ? |
|
|
|
|
105
|
|
|
mkdir($this->cacheDir, 0777, false) : ((file_exists($cache)) ? unlink($cache) : touch($cache)); |
|
|
|
|
106
|
|
|
file_put_contents($cache, serialize($addresses)); |
107
|
|
|
} |
108
|
|
|
$this->response = $addresses; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
/** |
114
|
|
|
* Constructor. |
115
|
|
|
* |
116
|
|
|
* @param string $address |
117
|
|
|
* @param int $page |
118
|
|
|
* @return Address |
119
|
|
|
*/ |
120
|
|
|
public function find($address = null, $page = 1) |
121
|
|
|
{ |
122
|
|
|
$this->search($address, $page); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Returns all the addresses found. |
127
|
|
|
* |
128
|
|
|
* @return array |
129
|
|
|
*/ |
130
|
|
|
public function all() |
131
|
|
|
{ |
132
|
|
|
$this->check(); |
133
|
|
|
|
134
|
|
|
return $this->response; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Returns all the addresses found. |
139
|
|
|
* |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public function get() |
143
|
|
|
{ |
144
|
|
|
return $this->all(); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Verify an address. |
149
|
|
|
* |
150
|
|
|
* @param int $buildingNumber |
151
|
|
|
* @param int $zip |
152
|
|
|
* @param int $additionalNumber |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
public function verify($buildingNumber, $zip, $additionalNumber) |
156
|
|
|
{ |
157
|
|
|
$cache = $this->file . $buildingNumber . '_' .$zip . '_' .$additionalNumber . '_' . strtolower($this->config->getLocale()) . '.data'; |
158
|
|
|
|
159
|
|
|
$response = $this->cacheValue($cache); |
160
|
|
|
|
161
|
|
|
if ($response == null) { |
162
|
|
|
|
163
|
|
|
$response = $this->_get( |
164
|
|
|
'v3.1/Address/address-verify', |
165
|
|
|
[ |
166
|
|
|
'buildingnumber' => $buildingNumber, |
167
|
|
|
'zipcode' => $zip, |
168
|
|
|
'additionalnumber' => $additionalNumber, |
169
|
|
|
] |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
if($this->config->getCache()){ |
174
|
|
|
(!file_exists($this->cacheDir)) ? |
|
|
|
|
175
|
|
|
mkdir($this->cacheDir, 0777, false) : ((file_exists($cache)) ? unlink($cache) : touch($cache)); |
|
|
|
|
176
|
|
|
file_put_contents($cache, serialize($response)); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
return (bool) $response['addressfound']; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Check if find() method was called first. |
184
|
|
|
* |
185
|
|
|
* @return void |
186
|
|
|
* @throws \BadMethodCallException |
187
|
|
|
*/ |
188
|
|
|
protected function check() |
189
|
|
|
{ |
190
|
|
|
if ($this->response == null) { |
191
|
|
|
throw new \BadMethodCallException("You need to call search() or find() methods first."); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|