1
|
|
|
<?php |
2
|
|
|
namespace Bpost\BpostApiClient; |
3
|
|
|
|
4
|
|
|
use Bpost\BpostApiClient\ApiCaller\ApiCaller; |
5
|
|
|
use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostCurlException; |
6
|
|
|
use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostInvalidXmlResponseException; |
7
|
|
|
use Bpost\BpostApiClient\Exception\BpostApiResponseException\BpostTaxipostLocatorException; |
8
|
|
|
use Bpost\BpostApiClient\Geo6\Poi; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Geo6 class |
12
|
|
|
* |
13
|
|
|
* @author Tijs Verkoyen <[email protected]> |
14
|
|
|
* @version 3.0.0 |
15
|
|
|
* @copyright Copyright (c), Tijs Verkoyen. All rights reserved. |
16
|
|
|
* @license BSD License |
17
|
|
|
*/ |
18
|
|
|
class Geo6 |
19
|
|
|
{ |
20
|
|
|
// URL for the api |
21
|
|
|
const API_URL = 'https://taxipost.geo6.be/Locator'; |
22
|
|
|
|
23
|
|
|
// current version |
24
|
|
|
const VERSION = '3'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @see getPointType |
28
|
|
|
* @see getServicePointPageUrl |
29
|
|
|
*/ |
30
|
|
|
const POINT_TYPE_POST_OFFICE = 1; |
31
|
|
|
const POINT_TYPE_POST_POINT = 2; |
32
|
|
|
const POINT_TYPE_BPACK_247 = 4; |
33
|
|
|
const POINT_TYPE_CLICK_COLLECT_SHOP = 8; |
34
|
|
|
|
35
|
|
|
/** @var ApiCaller */ |
36
|
|
|
private $apiCaller; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
private $appId; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
private $partner; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* The timeout |
50
|
|
|
* |
51
|
|
|
* @var int |
52
|
|
|
*/ |
53
|
|
|
private $timeOut = 10; |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* The user agent |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
private $userAgent; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Constructor |
64
|
|
|
* @param string $partner Static parameter used for protection/statistics |
65
|
|
|
* @param string $appId Static parameter used for protection/statistics |
66
|
|
|
*/ |
67
|
|
|
public function __construct($partner, $appId) |
68
|
|
|
{ |
69
|
|
|
$this->setPartner((string)$partner); |
70
|
|
|
$this->setAppId((string)$appId); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return ApiCaller |
75
|
|
|
*/ |
76
|
|
View Code Duplication |
public function getApiCaller() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
if ($this->apiCaller === null) { |
79
|
|
|
$this->apiCaller = new ApiCaller(new Logger()); |
80
|
|
|
} |
81
|
|
|
return $this->apiCaller; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param ApiCaller $apiCaller |
86
|
|
|
*/ |
87
|
|
|
public function setApiCaller(ApiCaller $apiCaller) |
88
|
|
|
{ |
89
|
|
|
$this->apiCaller = $apiCaller; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Build the url to be called |
94
|
|
|
* |
95
|
|
|
* @param string $method |
96
|
|
|
* @param array $parameters |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
private function buildUrl($method, array $parameters = array()) |
100
|
|
|
{ |
101
|
|
|
return self::API_URL . '?' . $this->buildParameters($method, $parameters); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Build the parameters to send (URL-encoded string) |
106
|
|
|
* |
107
|
|
|
* @param string $method |
108
|
|
|
* @param array $parameters |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
|
|
private function buildParameters($method, array $parameters = array()) |
112
|
|
|
{ |
113
|
|
|
// add credentials |
114
|
|
|
$parameters['Function'] = $method; |
115
|
|
|
$parameters['Partner'] = $this->getPartner(); |
116
|
|
|
$parameters['AppId'] = $this->getAppId(); |
117
|
|
|
$parameters['Format'] = 'xml'; |
118
|
|
|
|
119
|
|
|
return http_build_query($parameters); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Make the real call |
124
|
|
|
* |
125
|
|
|
* @param string $method |
126
|
|
|
* @param array $parameters |
127
|
|
|
* @return \SimpleXMLElement |
128
|
|
|
* @throws BpostCurlException |
129
|
|
|
* @throws BpostInvalidXmlResponseException |
130
|
|
|
* @throws BpostTaxipostLocatorException |
131
|
|
|
*/ |
132
|
|
|
private function doCall($method, array $parameters = array()) |
133
|
|
|
{ |
134
|
|
|
$options = array( |
135
|
|
|
CURLOPT_URL => self::API_URL, |
136
|
|
|
CURLOPT_USERAGENT => $this->getUserAgent(), |
137
|
|
|
CURLOPT_FOLLOWLOCATION => true, |
138
|
|
|
CURLOPT_SSL_VERIFYPEER => false, |
139
|
|
|
CURLOPT_SSL_VERIFYHOST => false, |
140
|
|
|
CURLOPT_RETURNTRANSFER => true, |
141
|
|
|
CURLOPT_TIMEOUT => (int)$this->getTimeOut(), |
142
|
|
|
|
143
|
|
|
CURLOPT_POST => true, |
144
|
|
|
CURLOPT_POSTFIELDS => $this->buildParameters($method, $parameters), |
145
|
|
|
); |
146
|
|
|
|
147
|
|
|
$this->getApiCaller()->doCall($options); |
148
|
|
|
|
149
|
|
|
// we expect XML so decode it |
150
|
|
|
$xml = @simplexml_load_string($this->getApiCaller()->getResponseBody()); |
151
|
|
|
|
152
|
|
|
// validate xml |
153
|
|
|
if ($xml === false || (isset($xml->head) && isset($xml->body))) { |
154
|
|
|
throw new BpostInvalidXmlResponseException(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// catch generic errors |
158
|
|
|
if (isset($xml['type']) && (string)$xml['type'] == 'TaxipostLocatorError') { |
159
|
|
|
throw new BpostTaxipostLocatorException((string)$xml->txt, (int)$xml->status); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
// return |
163
|
|
|
return $xml; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @param string $appId |
168
|
|
|
*/ |
169
|
|
|
public function setAppId($appId) |
170
|
|
|
{ |
171
|
|
|
$this->appId = $appId; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* @return string |
176
|
|
|
*/ |
177
|
|
|
public function getAppId() |
178
|
|
|
{ |
179
|
|
|
return $this->appId; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* @param string $partner |
184
|
|
|
*/ |
185
|
|
|
public function setPartner($partner) |
186
|
|
|
{ |
187
|
|
|
$this->partner = $partner; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @return string |
192
|
|
|
*/ |
193
|
|
|
public function getPartner() |
194
|
|
|
{ |
195
|
|
|
return $this->partner; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Set the timeout |
200
|
|
|
* After this time the request will stop. You should handle any errors triggered by this. |
201
|
|
|
* |
202
|
|
|
* @param int $seconds The timeout in seconds. |
203
|
|
|
*/ |
204
|
|
|
public function setTimeOut($seconds) |
205
|
|
|
{ |
206
|
|
|
$this->timeOut = (int)$seconds; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Get the timeout that will be used |
211
|
|
|
* |
212
|
|
|
* @return int |
213
|
|
|
*/ |
214
|
|
|
public function getTimeOut() |
215
|
|
|
{ |
216
|
|
|
return (int)$this->timeOut; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get the useragent that will be used. |
221
|
|
|
* Our version will be prepended to yours. |
222
|
|
|
* It will look like: "PHP Bpost/<version> <your-user-agent>" |
223
|
|
|
* |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
|
|
public function getUserAgent() |
227
|
|
|
{ |
228
|
|
|
return (string)'PHP Bpost Geo6/' . self::VERSION . ' ' . $this->userAgent; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Set the user-agent for you application |
233
|
|
|
* It will be appended to ours, the result will look like: "PHP Bpost/<version> <your-user-agent>" |
234
|
|
|
* |
235
|
|
|
* @param string $userAgent Your user-agent, it should look like <app-name>/<app-version>. |
236
|
|
|
*/ |
237
|
|
|
public function setUserAgent($userAgent) |
238
|
|
|
{ |
239
|
|
|
$this->userAgent = (string)$userAgent; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
// webservice methods |
243
|
|
|
/** |
244
|
|
|
* The GetNearestServicePoints web service delivers the nearest bpost pick-up points to a location |
245
|
|
|
* |
246
|
|
|
* @param string $street Street name |
247
|
|
|
* @param string $number Street number |
248
|
|
|
* @param string $zone Postal code and/or city |
249
|
|
|
* @param string $language Language, possible values are: nl, fr |
250
|
|
|
* @param int $type Requested point type, possible values are: |
251
|
|
|
* - 1: Post Office |
252
|
|
|
* - 2: Post Point |
253
|
|
|
* - 3: (1+2, Post Office + Post Point) |
254
|
|
|
* - 4: bpack 24/7 |
255
|
|
|
* - 7: (1+2+4, Post Office + Post Point + bpack 24/7) |
256
|
|
|
* @param int $limit |
257
|
|
|
* @return array |
258
|
|
|
* @throws BpostCurlException |
259
|
|
|
* @throws BpostInvalidXmlResponseException |
260
|
|
|
* @throws BpostTaxipostLocatorException |
261
|
|
|
*/ |
262
|
|
|
public function getNearestServicePoint($street, $number, $zone, $language = 'nl', $type = 3, $limit = 10) |
263
|
|
|
{ |
264
|
|
|
$parameters = array( |
265
|
|
|
'Street' => (string)$street, |
266
|
|
|
'Number' => (string)$number, |
267
|
|
|
'Zone' => (string)$zone, |
268
|
|
|
'Language' => (string)$language, |
269
|
|
|
'Type' => (int)$type, |
270
|
|
|
'Limit' => (int)$limit |
271
|
|
|
); |
272
|
|
|
|
273
|
|
|
$xml = $this->doCall('search', $parameters); |
274
|
|
|
|
275
|
|
|
if (!isset($xml->PoiList->Poi)) { |
276
|
|
|
throw new BpostInvalidXmlResponseException(); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
$pois = array(); |
280
|
|
|
foreach ($xml->PoiList->Poi as $poi) { |
281
|
|
|
$pois[] = array( |
282
|
|
|
'poi' => Poi::createFromXML($poi), |
283
|
|
|
'distance' => (float)$poi->Distance, |
284
|
|
|
); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return $pois; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* The GetServicePointDetails web service delivers the details for a bpost |
292
|
|
|
* pick up point referred to by its identifier. |
293
|
|
|
* |
294
|
|
|
* @param string $id Requested point identifier |
295
|
|
|
* @param string $language Language, possible values: nl, fr |
296
|
|
|
* @param int $type Requested point type, possible values are: |
297
|
|
|
* - 1: Post Office |
298
|
|
|
* - 2: Post Point |
299
|
|
|
* - 4: bpack 24/7 |
300
|
|
|
* |
301
|
|
|
* @return Poi |
302
|
|
|
* @throws BpostCurlException |
303
|
|
|
* @throws BpostInvalidXmlResponseException |
304
|
|
|
* @throws BpostTaxipostLocatorException |
305
|
|
|
*/ |
306
|
|
|
public function getServicePointDetails($id, $language = 'nl', $type = 3) |
307
|
|
|
{ |
308
|
|
|
$parameters = array( |
309
|
|
|
'Id' => (string)$id, |
310
|
|
|
'Language' => (string)$language, |
311
|
|
|
'Type' => (int)$type, |
312
|
|
|
); |
313
|
|
|
|
314
|
|
|
$xml = $this->doCall('info', $parameters); |
315
|
|
|
|
316
|
|
|
if (!isset($xml->Poi)) { |
317
|
|
|
throw new BpostInvalidXmlResponseException(); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
return Poi::createFromXML($xml->Poi); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
/** |
324
|
|
|
* @param int $id |
325
|
|
|
* @param string $language |
326
|
|
|
* @param int $type |
327
|
|
|
* @return string |
328
|
|
|
* |
329
|
|
|
* @see getPointType to feed the param $type |
330
|
|
|
*/ |
331
|
|
|
public function getServicePointPageUrl($id, $language = 'nl', $type = 3) |
332
|
|
|
{ |
333
|
|
|
$parameters = array( |
334
|
|
|
'Id' => (string)$id, |
335
|
|
|
'Language' => (string)$language, |
336
|
|
|
'Type' => (int)$type, |
337
|
|
|
); |
338
|
|
|
|
339
|
|
|
return $this->buildUrl('page', $parameters); |
340
|
|
|
} |
341
|
|
|
|
342
|
|
|
/** |
343
|
|
|
* @param int $id |
344
|
|
|
* @param string $language |
345
|
|
|
* @param int $type |
346
|
|
|
* @return string |
347
|
|
|
* |
348
|
|
|
* @deprecated Renamed |
349
|
|
|
* @see getServicePointPageUrl |
350
|
|
|
*/ |
351
|
|
|
public function getServicePointPage($id, $language = 'nl', $type = 3) |
352
|
|
|
{ |
353
|
|
|
return $this->getServicePointPageUrl($id, $language, $type); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* @param bool $withPostOffice |
358
|
|
|
* @param bool $withPostPoint |
359
|
|
|
* @param bool $withBpack247 |
360
|
|
|
* @param bool $withClickAndCollectShop |
361
|
|
|
* @return int |
362
|
|
|
*/ |
363
|
|
|
public function getPointType( |
364
|
|
|
$withPostOffice = true, |
365
|
|
|
$withPostPoint = true, |
366
|
|
|
$withBpack247 = false, |
367
|
|
|
$withClickAndCollectShop = false |
368
|
|
|
) { |
369
|
|
|
return |
370
|
|
|
($withPostOffice ? self::POINT_TYPE_POST_OFFICE : 0) |
371
|
|
|
+ ($withPostPoint ? self::POINT_TYPE_POST_POINT : 0) |
372
|
|
|
+ ($withBpack247 ? self::POINT_TYPE_BPACK_247 : 0) |
373
|
|
|
+ ($withClickAndCollectShop ? self::POINT_TYPE_CLICK_COLLECT_SHOP : 0); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.