1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace JustSteveKing\LaravelPostcodes\Service; |
6
|
|
|
|
7
|
|
|
use GuzzleHttp\Client; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use function GuzzleHttp\Psr7\build_query; |
10
|
|
|
|
11
|
|
|
class PostcodeService |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $url; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var Client |
20
|
|
|
*/ |
21
|
|
|
protected $http; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Postcode Service constructor. |
25
|
|
|
* |
26
|
|
|
* @param Client $client |
27
|
|
|
* |
28
|
|
|
* @return void |
29
|
|
|
*/ |
30
|
60 |
|
public function __construct(Client $client) |
31
|
|
|
{ |
32
|
60 |
|
$this->url = config('services.postcodes.url'); |
33
|
|
|
|
34
|
60 |
|
$this->http = $client; |
35
|
60 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Validate a postcode against the API |
39
|
|
|
* |
40
|
|
|
* @param string $postcode |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
9 |
|
public function validate(string $postcode, $pre_validate = TRUE): bool |
45
|
|
|
{ |
46
|
9 |
|
if($pre_validate == TRUE) |
47
|
|
|
if (preg_match('#^(GIR ?0AA|[A-PR-UWYZ]([0-9]{1,2}|([A-HK-Y][0-9]([0-9ABEHMNPRV-Y])?)|[0-9][A-HJKPS-UW]) ?[0-9][ABD-HJLNP-UW-Z]{2})$#', $postcode)) { |
48
|
|
|
return FALSE; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
return $this->getResponse("postcodes/$postcode/validate"); |
|
|
|
|
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the address details from a postcode |
56
|
3 |
|
* |
57
|
|
|
* @param string $postcode |
58
|
3 |
|
* |
59
|
|
|
* @return object |
60
|
|
|
*/ |
61
|
|
|
public function getPostcode(string $postcode): object |
62
|
|
|
{ |
63
|
|
|
return $this->getResponse("postcodes/$postcode"); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get the address details from a multiple postcodes at once |
68
|
|
|
* |
69
|
|
|
* @param array $postcodes |
70
|
|
|
* |
71
|
3 |
|
* @param array $filter - optional array of fields to return |
72
|
|
|
* @return Collection |
73
|
3 |
|
* |
74
|
3 |
|
* @throws \GuzzleHttp\Exception\GuzzleException |
75
|
|
|
*/ |
76
|
|
|
public function getPostcodes(array $postcodes, array $filter = []): Collection |
77
|
3 |
|
{ |
78
|
3 |
|
if (!empty($filter)) { |
79
|
3 |
|
$filter = build_query(['filter' => implode(',', $filter)]); |
80
|
3 |
|
} |
81
|
|
|
|
82
|
3 |
|
return collect($this->getResponse( |
83
|
3 |
|
'postcodes?' . $filter, |
84
|
|
|
'POST', |
85
|
|
|
['postcodes' => array_values($postcodes)] |
86
|
|
|
))->map(function ($item) { |
87
|
|
|
return $item->result; |
88
|
|
|
}); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get information based on outward code including geo data |
93
|
3 |
|
* |
94
|
|
|
* @param string $outwardcode |
95
|
3 |
|
* |
96
|
|
|
* @return object |
97
|
|
|
*/ |
98
|
|
|
public function getOutwardCode(string $outwardcode): object |
99
|
|
|
{ |
100
|
|
|
return $this->getResponse("outcodes/$outwardcode"); |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
/** |
104
|
|
|
* Get the address details from a random postcode |
105
|
3 |
|
* |
106
|
|
|
* @return object |
107
|
|
|
*/ |
108
|
|
|
public function getRandomPostcode() |
109
|
|
|
{ |
110
|
|
|
return $this->getResponse("random/postcodes"); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Query the API for a given string |
115
|
|
|
* |
116
|
|
|
* @param string $query |
117
|
3 |
|
* |
118
|
|
|
* @return Collection |
119
|
3 |
|
* |
120
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
121
|
3 |
|
*/ |
122
|
|
|
public function query(string $query): Collection |
123
|
|
|
{ |
124
|
|
|
$queryString = http_build_query(['q' => $query]); |
125
|
|
|
|
126
|
|
|
return collect($this->getResponse("postcodes?$queryString")); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get data for the postcodes nearest to the passed postcode |
131
|
|
|
* |
132
|
|
|
* @param string $postcode |
133
|
3 |
|
* |
134
|
|
|
* @return Collection |
135
|
3 |
|
* |
136
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
137
|
|
|
*/ |
138
|
|
|
public function nearest(string $postcode): Collection |
139
|
|
|
{ |
140
|
|
|
return collect($this->getResponse("postcodes/$postcode/nearest")); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Lookup a terminated postcode. Returns the postcode, year and month of termination. |
145
|
3 |
|
* |
146
|
|
|
* @param string $postcode |
147
|
3 |
|
* |
148
|
|
|
* @return object |
149
|
|
|
*/ |
150
|
|
|
public function getTerminatedPostcode($postcode) |
151
|
|
|
{ |
152
|
|
|
return $this->getResponse("terminated_postcodes/$postcode"); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Autocomplete a postcode partial. |
157
|
|
|
* |
158
|
|
|
* @param string $partialPostcode |
159
|
6 |
|
* |
160
|
|
|
* @return Collection |
161
|
6 |
|
* |
162
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
163
|
|
|
*/ |
164
|
|
|
public function autocomplete(string $partialPostcode): Collection |
165
|
|
|
{ |
166
|
|
|
return collect($this->getResponse("postcodes/$partialPostcode/autocomplete")); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get nearest outward codes for a given longitude & latitude |
171
|
|
|
* |
172
|
|
|
* @param float $longitude |
173
|
|
|
* |
174
|
6 |
|
* @param float $latitude |
175
|
|
|
* @return Collection |
176
|
6 |
|
* |
177
|
6 |
|
* @throws \GuzzleHttp\Exception\GuzzleException |
178
|
4 |
|
*/ |
179
|
4 |
|
public function nearestOutwardCodesForGivenLngAndLat(float $longitude, float $latitude): Collection |
180
|
|
|
{ |
181
|
|
|
return collect($this->getResponse(sprintf( |
182
|
|
|
'outcodes?lon=%s&lat=%s', |
183
|
|
|
$longitude, |
184
|
|
|
$latitude |
185
|
|
|
))); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Get information about nearest outcodes based on outward code |
190
|
|
|
* |
191
|
|
|
* @param string $outwardcode |
192
|
|
|
* |
193
|
|
|
* @param int $limit Needs to be less than 100 |
194
|
3 |
|
* @param int $radius Needs to be less than 25,000m |
195
|
|
|
* @return Collection |
196
|
|
|
* |
197
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
198
|
|
|
*/ |
199
|
3 |
|
public function getNearestOutwardCode( |
200
|
3 |
|
string $outwardcode, |
201
|
|
|
int $limit = 10, |
202
|
3 |
|
int $radius = 5000 |
203
|
3 |
|
): Collection { |
204
|
|
|
$limit = ($limit > 100) ? 100 : $limit; |
205
|
|
|
$radius = ($radius > 100) ? 25000 : $radius; |
206
|
|
|
|
207
|
|
|
return collect($this->getResponse( |
208
|
|
|
"outcodes/$outwardcode/nearest?limit=$limit&radius=$radius" |
209
|
|
|
)); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get nearest postcodes for a given longitude & latitude |
214
|
|
|
* |
215
|
|
|
* @param float $longitude |
216
|
|
|
* @param float $latitude |
217
|
6 |
|
* |
218
|
|
|
* @return Collection |
219
|
6 |
|
* |
220
|
6 |
|
* @throws \GuzzleHttp\Exception\GuzzleException |
221
|
4 |
|
*/ |
222
|
4 |
|
public function nearestPostcodesForGivenLngAndLat(float $longitude, float $latitude): Collection |
223
|
|
|
{ |
224
|
|
|
return collect($this->getResponse(sprintf( |
225
|
|
|
'postcodes?lon=%s&lat=%s', |
226
|
|
|
$longitude, |
227
|
|
|
$latitude |
228
|
|
|
))); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Get the response and return the result object |
233
|
|
|
* |
234
|
|
|
* @param string|null $uri |
235
|
|
|
* @param string $method |
236
|
51 |
|
* @param array $data - data to be sent in post/patch/put request |
237
|
|
|
* @param array $options - array of options to be passed to curl, if $data is passed 'json' will be overwritten |
238
|
|
|
* @return mixed |
239
|
|
|
* @throws \GuzzleHttp\Exception\GuzzleException |
240
|
|
|
*/ |
241
|
|
|
protected function getResponse( |
242
|
51 |
|
string $uri = null, |
243
|
|
|
string $method = 'GET', |
244
|
51 |
|
array $data = [], |
245
|
3 |
|
array $options = [] |
246
|
|
|
) { |
247
|
|
|
$url = $this->url . $uri; |
248
|
51 |
|
|
249
|
51 |
|
if (!empty($data)) { |
250
|
34 |
|
$options['json'] = $data; |
251
|
34 |
|
} |
252
|
|
|
|
253
|
|
|
$request = $this->http->request( |
254
|
51 |
|
$method, |
255
|
|
|
$url, |
256
|
|
|
$options |
257
|
|
|
); |
258
|
|
|
|
259
|
|
|
return json_decode($request->getBody()->getContents())->result; |
260
|
|
|
} |
261
|
|
|
} |
262
|
|
|
|