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.3 |
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 Address 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/' . 'address_'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns a list of all the addresses from a string. |
48
|
|
|
* |
49
|
|
|
* @param string $address |
50
|
|
|
* @param int $page |
51
|
|
|
* @param string $lang |
52
|
|
|
* @return Address |
53
|
|
|
*/ |
54
|
|
|
public function find($address, $page = 1, $lang = 'A') |
55
|
|
|
{ |
56
|
|
|
$cache = $this->file . preg_replace('/[^a-zA-Z0-9_]/', '_', $address) . '_' . strtolower($lang) . '.data'; |
57
|
|
|
|
58
|
|
|
$this->response = $this->cacheValue($cache); |
59
|
|
|
|
60
|
|
|
if ($this->response == null) { |
61
|
|
|
$response = $this->_get( |
62
|
|
|
'v4/Address/address-free-text', |
63
|
|
|
$lang, |
64
|
|
|
[ |
65
|
|
|
'addressstring' => $address, |
66
|
|
|
'page' => $page, |
67
|
|
|
], |
68
|
|
|
false |
69
|
|
|
); |
70
|
|
|
if ($response['success'] == false) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
$addresses = $response['Addresses']; |
74
|
|
|
$pages = (int) ceil($response['totalSearchResults'] / 20); |
75
|
|
|
if ($page == 1 && ($pages > 1)) { |
76
|
|
|
for ($i = 2; $i <= $pages; $i++) { |
77
|
|
|
// 1 call allowed per 5 seconds (on Development subscription) |
78
|
|
|
($this->config->getApiSubscription() == 'Development') ? sleep(5) : ''; |
79
|
|
|
$pageData = $this->_get( |
80
|
|
|
'v4/Address/address-free-text', |
81
|
|
|
$lang, |
82
|
|
|
[ |
83
|
|
|
'addressstring' => $address, |
84
|
|
|
'page' => $i, |
85
|
|
|
], |
86
|
|
|
false |
87
|
|
|
); |
88
|
|
|
$pageData = $pageData['Addresses']; |
89
|
|
|
$pageData = array_combine(range(($i * 10), count($pageData) + (($i * 10) - 1)), array_values($pageData)); |
90
|
|
|
$addresses += $pageData; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
if($this->config->getCache()){ |
94
|
|
|
(!file_exists($this->cacheDir)) ? |
95
|
|
|
mkdir($this->cacheDir, 0777, false) : ((file_exists($cache)) ? unlink($cache) : touch($cache)); |
96
|
|
|
file_put_contents($cache, serialize($addresses)); |
97
|
|
|
} |
98
|
|
|
$this->response = $addresses; |
99
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns all the addresses found. |
107
|
|
|
* |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
public function all() |
111
|
|
|
{ |
112
|
|
|
$this->check(); |
113
|
|
|
|
114
|
|
|
return $this->response; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Returns all the addresses found. |
119
|
|
|
* |
120
|
|
|
* @return array |
121
|
|
|
*/ |
122
|
|
|
public function get() |
123
|
|
|
{ |
124
|
|
|
return $this->all(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Verify an address. |
129
|
|
|
* |
130
|
|
|
* @param int $buildingNumber |
131
|
|
|
* @param int $zip |
132
|
|
|
* @param int $additionalNumber |
133
|
|
|
* @param string $lang |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function verify($buildingNumber, $zip, $additionalNumber, $lang = 'A') |
137
|
|
|
{ |
138
|
|
|
$response = $this->_get( |
139
|
|
|
'v3.1/Address/address-verify', |
140
|
|
|
$lang, |
141
|
|
|
[ |
142
|
|
|
'buildingnumber' => $buildingNumber, |
143
|
|
|
'zipcode' => $zip, |
144
|
|
|
'additionalnumber' => $additionalNumber, |
145
|
|
|
] |
146
|
|
|
); |
147
|
|
|
|
148
|
|
|
return (bool) $response['addressfound']; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Check if find() method was called first. |
153
|
|
|
* |
154
|
|
|
* @return void |
155
|
|
|
* @throws \BadMethodCallException |
156
|
|
|
*/ |
157
|
|
|
protected function check() |
158
|
|
|
{ |
159
|
|
|
if ($this->response == null) { |
160
|
|
|
throw new \BadMethodCallException("You need to call find() method first."); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|