1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Inspirum\Balikobot\Services; |
6
|
|
|
|
7
|
|
|
use DateTime; |
8
|
|
|
use Inspirum\Balikobot\Contracts\RequesterInterface; |
9
|
|
|
use Inspirum\Balikobot\Definitions\API; |
10
|
|
|
use Inspirum\Balikobot\Definitions\Request; |
11
|
|
|
use Inspirum\Balikobot\Exceptions\BadRequestException; |
12
|
|
|
use function array_filter; |
13
|
|
|
use function array_key_exists; |
14
|
|
|
use function count; |
15
|
|
|
|
16
|
|
|
class Client |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* API requester |
20
|
|
|
* |
21
|
|
|
* @var \Inspirum\Balikobot\Contracts\RequesterInterface |
22
|
|
|
*/ |
23
|
|
|
private RequesterInterface $requester; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Request and Response formatter |
27
|
|
|
* |
28
|
|
|
* @var \Inspirum\Balikobot\Services\Formatter |
29
|
|
|
*/ |
30
|
|
|
private Formatter $formatter; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Response validator |
34
|
|
|
* |
35
|
|
|
* @var \Inspirum\Balikobot\Services\Validator |
36
|
|
|
*/ |
37
|
|
|
private Validator $validator; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Balikobot API client |
41
|
|
|
* |
42
|
|
|
* @param \Inspirum\Balikobot\Contracts\RequesterInterface $requester |
43
|
|
|
*/ |
44
|
366 |
|
public function __construct(RequesterInterface $requester) |
45
|
|
|
{ |
46
|
366 |
|
$this->requester = $requester; |
47
|
366 |
|
$this->validator = new Validator(); |
48
|
366 |
|
$this->formatter = new Formatter($this->validator); |
49
|
366 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Add package(s) to the Balikobot |
53
|
|
|
* |
54
|
|
|
* @param string $shipper |
55
|
|
|
* @param array<array<string,mixed>> $packages |
56
|
|
|
* @param mixed|null $labelsUrl |
57
|
|
|
* |
58
|
|
|
* @return array<array<string,mixed>> |
59
|
|
|
* |
60
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
61
|
|
|
*/ |
62
|
30 |
|
public function addPackages(string $shipper, array $packages, mixed &$labelsUrl = null): array |
63
|
|
|
{ |
64
|
30 |
|
$response = $this->requester->call(API::V2V1, $shipper, Request::ADD, ['packages' => $packages]); |
65
|
|
|
|
66
|
16 |
|
if (isset($response['labels_url'])) { |
67
|
11 |
|
$labelsUrl = $response['labels_url']; |
68
|
|
|
} |
69
|
|
|
|
70
|
16 |
|
$response = $response['packages'] ?? []; |
71
|
|
|
|
72
|
16 |
|
$this->validator->validateIndexes($response, count($packages)); |
73
|
|
|
|
74
|
15 |
|
$this->validator->validateResponseItemHasAttribute($response, 'package_id', $response); |
75
|
|
|
|
76
|
13 |
|
return $response; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Drops a package from the Balikobot – the package must be not ordered |
81
|
|
|
* |
82
|
|
|
* @param string $shipper |
83
|
|
|
* @param string $packageId |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
* |
87
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
88
|
|
|
*/ |
89
|
2 |
|
public function dropPackage(string $shipper, string $packageId): void |
90
|
|
|
{ |
91
|
2 |
|
$this->dropPackages($shipper, [$packageId]); |
92
|
2 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Drops a package from the Balikobot – the package must be not ordered |
96
|
|
|
* |
97
|
|
|
* @param string $shipper |
98
|
|
|
* @param array<string> $packageIds |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
* |
102
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
103
|
|
|
*/ |
104
|
9 |
|
public function dropPackages(string $shipper, array $packageIds): void |
105
|
|
|
{ |
106
|
9 |
|
if (count($packageIds) > 0) { |
107
|
8 |
|
$this->requester->call(API::V2V1, $shipper, Request::DROP, ['package_ids' => $packageIds]); |
108
|
|
|
} |
109
|
5 |
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Tracks a package |
113
|
|
|
* |
114
|
|
|
* @param string $shipper |
115
|
|
|
* @param string $carrierId |
116
|
|
|
* |
117
|
|
|
* @return array<array<string,float|string>> |
118
|
|
|
* |
119
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
120
|
|
|
*/ |
121
|
7 |
|
public function trackPackage(string $shipper, string $carrierId): array |
122
|
|
|
{ |
123
|
7 |
|
return $this->trackPackages($shipper, [$carrierId])[0]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Tracks a packages |
128
|
|
|
* |
129
|
|
|
* @param string $shipper |
130
|
|
|
* @param array<string> $carrierIds |
131
|
|
|
* |
132
|
|
|
* @return array<array<array<string,float|string>>> |
133
|
|
|
* |
134
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
135
|
|
|
*/ |
136
|
23 |
|
public function trackPackages(string $shipper, array $carrierIds): array |
137
|
|
|
{ |
138
|
23 |
|
$response = $this->requester->call(API::V2V2, $shipper, Request::TRACK, ['carrier_ids' => $carrierIds], shouldHaveStatus: false); |
139
|
|
|
|
140
|
19 |
|
$response = $response['packages'] ?? []; |
141
|
|
|
|
142
|
19 |
|
$this->validator->validateIndexes($response, count($carrierIds)); |
143
|
|
|
|
144
|
14 |
|
return $this->formatter->normalizeTrackPackagesResponse($response); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Tracks a package, get the last info |
149
|
|
|
* |
150
|
|
|
* @param string $shipper |
151
|
|
|
* @param string $carrierId |
152
|
|
|
* |
153
|
|
|
* @return array<string,float|string|null> |
154
|
|
|
* |
155
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
156
|
|
|
*/ |
157
|
7 |
|
public function trackPackageLastStatus(string $shipper, string $carrierId): array |
158
|
|
|
{ |
159
|
7 |
|
return $this->trackPackagesLastStatus($shipper, [$carrierId])[0]; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Tracks a package, get the last info |
164
|
|
|
* |
165
|
|
|
* @param string $shipper |
166
|
|
|
* @param array<string> $carrierIds |
167
|
|
|
* |
168
|
|
|
* @return array<array<string,float|string|null>> |
169
|
|
|
* |
170
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
171
|
|
|
*/ |
172
|
20 |
|
public function trackPackagesLastStatus(string $shipper, array $carrierIds): array |
173
|
|
|
{ |
174
|
20 |
|
$response = $this->requester->call( |
175
|
20 |
|
API::V2V2, |
176
|
|
|
$shipper, |
177
|
20 |
|
Request::TRACK_STATUS, |
178
|
20 |
|
['carrier_ids' => $carrierIds], |
179
|
20 |
|
shouldHaveStatus: false, |
180
|
|
|
); |
181
|
|
|
|
182
|
16 |
|
$response = $response['packages'] ?? []; |
183
|
|
|
|
184
|
16 |
|
$this->validator->validateIndexes($response, count($carrierIds)); |
185
|
|
|
|
186
|
13 |
|
return $this->formatter->normalizeTrackPackagesLastStatusResponse($response); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Returns packages from the front (not ordered) for given shipper |
191
|
|
|
* |
192
|
|
|
* @param string $shipper |
193
|
|
|
* |
194
|
|
|
* @return array<array<string,int|string>> |
195
|
|
|
* |
196
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
197
|
|
|
*/ |
198
|
7 |
|
public function getOverview(string $shipper): array |
199
|
|
|
{ |
200
|
7 |
|
return $this->requester->call(API::V2V1, $shipper, Request::OVERVIEW, shouldHaveStatus: false); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Gets labels |
205
|
|
|
* |
206
|
|
|
* @param string $shipper |
207
|
|
|
* @param array<string> $packageIds |
208
|
|
|
* |
209
|
|
|
* @return string |
210
|
|
|
* |
211
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
212
|
|
|
*/ |
213
|
9 |
|
public function getLabels(string $shipper, array $packageIds): string |
214
|
|
|
{ |
215
|
9 |
|
$response = $this->requester->call(API::V2V1, $shipper, Request::LABELS, ['package_ids' => $packageIds]); |
216
|
|
|
|
217
|
5 |
|
return $response['labels_url']; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Gets complete information about a package by its package ID |
222
|
|
|
* |
223
|
|
|
* @param string $shipper |
224
|
|
|
* @param string $packageId |
225
|
|
|
* |
226
|
|
|
* @return array<string,int|string> |
227
|
|
|
* |
228
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
229
|
|
|
*/ |
230
|
4 |
|
public function getPackageInfo(string $shipper, string $packageId): array |
231
|
|
|
{ |
232
|
4 |
|
return $this->requester->call(API::V2V1, $shipper, Request::PACKAGE . '/' . $packageId, shouldHaveStatus: false); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Gets complete information about a package by its carrier ID |
237
|
|
|
* |
238
|
|
|
* @param string $shipper |
239
|
|
|
* @param string $carrierId |
240
|
|
|
* |
241
|
|
|
* @return array<string,int|string> |
242
|
|
|
* |
243
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
244
|
|
|
*/ |
245
|
8 |
|
public function getPackageInfoByCarrierId(string $shipper, string $carrierId): array |
246
|
|
|
{ |
247
|
8 |
|
return $this->requester->call( |
248
|
8 |
|
API::V2V1, |
249
|
|
|
$shipper, |
250
|
8 |
|
Request::PACKAGE . '/carrier_id/' . $carrierId, |
251
|
8 |
|
shouldHaveStatus: false, |
252
|
|
|
); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Order shipment for packages |
257
|
|
|
* |
258
|
|
|
* @param string $shipper |
259
|
|
|
* @param array<string> $packageIds |
260
|
|
|
* |
261
|
|
|
* @return array<string,int|string> |
262
|
|
|
* |
263
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
264
|
|
|
*/ |
265
|
10 |
|
public function orderShipment(string $shipper, array $packageIds): array |
266
|
|
|
{ |
267
|
10 |
|
$response = $this->requester->call(API::V2V1, $shipper, Request::ORDER, ['package_ids' => $packageIds]); |
268
|
|
|
|
269
|
6 |
|
return $this->formatter->withoutStatus($response); |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Get order details |
274
|
|
|
* |
275
|
|
|
* @param string $shipper |
276
|
|
|
* @param string $orderId |
277
|
|
|
* |
278
|
|
|
* @return array<string,int|string|array<mixed,mixed>> |
279
|
|
|
* |
280
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
281
|
|
|
*/ |
282
|
9 |
|
public function getOrder(string $shipper, string $orderId): array |
283
|
|
|
{ |
284
|
9 |
|
$response = $this->requester->call(API::V2V1, $shipper, Request::ORDER_VIEW . '/' . $orderId, shouldHaveStatus: false); |
285
|
|
|
|
286
|
6 |
|
return $this->formatter->withoutStatus($response); |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* Order pickup for packages |
291
|
|
|
* |
292
|
|
|
* @param string $shipper |
293
|
|
|
* @param \DateTime $dateFrom |
294
|
|
|
* @param \DateTime $dateTo |
295
|
|
|
* @param float $weight |
296
|
|
|
* @param int $packageCount |
297
|
|
|
* @param string|null $message |
298
|
|
|
* |
299
|
|
|
* @return void |
300
|
|
|
* |
301
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
302
|
|
|
*/ |
303
|
5 |
|
public function orderPickup( |
304
|
|
|
string $shipper, |
305
|
|
|
DateTime $dateFrom, |
306
|
|
|
DateTime $dateTo, |
307
|
|
|
float $weight, |
308
|
|
|
int $packageCount, |
309
|
|
|
?string $message = null, |
310
|
|
|
): void { |
311
|
5 |
|
$response = $this->requester->call(API::V2V1, $shipper, Request::ORDER_PICKUP, [ |
312
|
5 |
|
'date' => $dateFrom->format('Y-m-d'), |
313
|
5 |
|
'time_from' => $dateFrom->format('H:s'), |
314
|
5 |
|
'time_to' => $dateTo->format('H:s'), |
315
|
5 |
|
'weight' => $weight, |
316
|
5 |
|
'package_count' => $packageCount, |
317
|
5 |
|
'message' => $message, |
318
|
|
|
]); |
319
|
|
|
|
320
|
2 |
|
if (array_key_exists('message', $response)) { |
321
|
1 |
|
throw new BadRequestException($response, 400, null, $response['message']); |
322
|
|
|
} |
323
|
1 |
|
} |
324
|
|
|
|
325
|
|
|
/** |
326
|
|
|
* Returns available services for the given shipper |
327
|
|
|
* |
328
|
|
|
* @param string $shipper |
329
|
|
|
* |
330
|
|
|
* @return array<string,string> |
331
|
|
|
* |
332
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
333
|
|
|
*/ |
334
|
13 |
|
public function getServices(string $shipper): array |
335
|
|
|
{ |
336
|
13 |
|
$response = $this->requester->call(API::V2V1, $shipper, Request::SERVICES); |
337
|
|
|
|
338
|
9 |
|
return $this->formatter->normalizeResponseItems($response['service_types'] ?? [], 'service_type', 'name'); |
339
|
|
|
} |
340
|
|
|
|
341
|
|
|
/** |
342
|
|
|
* Returns available B2A services for the given shipper |
343
|
|
|
* |
344
|
|
|
* @param string $shipper |
345
|
|
|
* |
346
|
|
|
* @return array<string,string> |
347
|
|
|
* |
348
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
349
|
|
|
*/ |
350
|
9 |
|
public function getB2AServices(string $shipper): array |
351
|
|
|
{ |
352
|
9 |
|
$response = $this->requester->call(API::V2V1, $shipper, Request::B2A . '/' . Request::SERVICES); |
353
|
|
|
|
354
|
6 |
|
return $this->formatter->normalizeResponseItems($response['service_types'] ?? [], 'service_type', 'name'); |
355
|
|
|
} |
356
|
|
|
|
357
|
|
|
/** |
358
|
|
|
* Returns all manipulation units for the given shipper |
359
|
|
|
* |
360
|
|
|
* @param string $shipper |
361
|
|
|
* @param bool $fullData |
362
|
|
|
* |
363
|
|
|
* @return array<string,string|array<mixed,mixed>> |
364
|
|
|
* |
365
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
366
|
|
|
*/ |
367
|
12 |
|
public function getManipulationUnits(string $shipper, bool $fullData = false): array |
368
|
|
|
{ |
369
|
12 |
|
$response = $this->requester->call(API::V2V1, $shipper, Request::MANIPULATION_UNITS); |
370
|
|
|
|
371
|
9 |
|
return $this->formatter->normalizeResponseItems( |
372
|
9 |
|
$response['units'] ?? [], |
373
|
9 |
|
'code', |
374
|
9 |
|
$fullData === false ? 'name' : null, |
375
|
|
|
); |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
/** |
379
|
|
|
* Returns available manipulation units for the given shipper |
380
|
|
|
* |
381
|
|
|
* @param string $shipper |
382
|
|
|
* @param bool $fullData |
383
|
|
|
* |
384
|
|
|
* @return array<string,string|array<mixed,mixed>> |
385
|
|
|
* |
386
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
387
|
|
|
*/ |
388
|
12 |
|
public function getActivatedManipulationUnits(string $shipper, bool $fullData = false): array |
389
|
|
|
{ |
390
|
12 |
|
$response = $this->requester->call(API::V2V1, $shipper, Request::ACTIVATED_MANIPULATION_UNITS); |
391
|
|
|
|
392
|
9 |
|
return $this->formatter->normalizeResponseItems( |
393
|
9 |
|
$response['units'] ?? [], |
394
|
9 |
|
'code', |
395
|
9 |
|
$fullData === false ? 'name' : null, |
396
|
|
|
); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Returns available branches for the given shipper and its service |
401
|
|
|
* Full branches instead branches request |
402
|
|
|
* |
403
|
|
|
* @param string $shipper |
404
|
|
|
* @param string|null $service |
405
|
|
|
* @param string|null $country |
406
|
|
|
* @param bool $fullBranchesRequest |
407
|
|
|
* @param bool $gzip |
408
|
|
|
* |
409
|
|
|
* @return array<array<string,mixed>> |
410
|
|
|
* |
411
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
412
|
|
|
*/ |
413
|
23 |
|
public function getBranches( |
414
|
|
|
string $shipper, |
415
|
|
|
?string $service, |
416
|
|
|
?string $country = null, |
417
|
|
|
bool $fullBranchesRequest = false, |
418
|
|
|
bool $gzip = false, |
419
|
|
|
): array { |
420
|
23 |
|
$usedRequest = $fullBranchesRequest ? Request::FULL_BRANCHES : Request::BRANCHES; |
421
|
|
|
|
422
|
23 |
|
if ($service !== null) { |
423
|
21 |
|
$usedRequest .= '/service/' . $service; |
424
|
|
|
} |
425
|
|
|
|
426
|
23 |
|
if ($country !== null) { |
427
|
7 |
|
$usedRequest .= '/country/' . $country; |
428
|
|
|
} |
429
|
|
|
|
430
|
23 |
|
$response = $this->requester->call(API::V2V1, $shipper, $usedRequest, gzip: $gzip); |
431
|
|
|
|
432
|
20 |
|
return $response['branches'] ?? []; |
433
|
|
|
} |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Returns available branches for the given shipper in given location |
437
|
|
|
* |
438
|
|
|
* @param string $shipper |
439
|
|
|
* @param string $country |
440
|
|
|
* @param string $city |
441
|
|
|
* @param string|null $postcode |
442
|
|
|
* @param string|null $street |
443
|
|
|
* @param int|null $maxResults |
444
|
|
|
* @param float|null $radius |
445
|
|
|
* @param string|null $type |
446
|
|
|
* |
447
|
|
|
* @return array<array<string,mixed>> |
448
|
|
|
* |
449
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
450
|
|
|
*/ |
451
|
11 |
|
public function getBranchesForLocation( |
452
|
|
|
string $shipper, |
453
|
|
|
string $country, |
454
|
|
|
string $city, |
455
|
|
|
?string $postcode = null, |
456
|
|
|
?string $street = null, |
457
|
|
|
?int $maxResults = null, |
458
|
|
|
?float $radius = null, |
459
|
|
|
?string $type = null, |
460
|
|
|
): array { |
461
|
11 |
|
$response = $this->requester->call( |
462
|
11 |
|
API::V2V1, |
463
|
|
|
$shipper, |
464
|
11 |
|
Request::BRANCH_LOCATOR, |
465
|
11 |
|
array_filter( |
466
|
|
|
[ |
467
|
11 |
|
'country' => $country, |
468
|
11 |
|
'city' => $city, |
469
|
11 |
|
'zip' => $postcode, |
470
|
11 |
|
'street' => $street, |
471
|
11 |
|
'max_results' => $maxResults, |
472
|
11 |
|
'radius' => $radius, |
473
|
11 |
|
'type' => $type, |
474
|
|
|
] |
475
|
|
|
) |
476
|
|
|
); |
477
|
|
|
|
478
|
7 |
|
return $response['branches'] ?? []; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* Returns list of countries where service with cash-on-delivery payment type is available in |
483
|
|
|
* |
484
|
|
|
* @param string $shipper |
485
|
|
|
* |
486
|
|
|
* @return array<array<int|string,array<string,array<string,mixed>>>> |
487
|
|
|
* |
488
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
489
|
|
|
*/ |
490
|
9 |
|
public function getCodCountries(string $shipper): array |
491
|
|
|
{ |
492
|
9 |
|
$response = $this->requester->call(API::V2V1, $shipper, Request::CASH_ON_DELIVERY_COUNTRIES); |
493
|
|
|
|
494
|
6 |
|
return $this->formatter->normalizeResponseItems( |
495
|
6 |
|
$response['service_types'] ?? [], |
496
|
6 |
|
'service_type', |
497
|
6 |
|
'cod_countries', |
498
|
|
|
); |
499
|
|
|
} |
500
|
|
|
|
501
|
|
|
/** |
502
|
|
|
* Returns list of countries where service is available in |
503
|
|
|
* |
504
|
|
|
* @param string $shipper |
505
|
|
|
* |
506
|
|
|
* @return array<array<int|string,string>> |
507
|
|
|
* |
508
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
509
|
|
|
*/ |
510
|
9 |
|
public function getCountries(string $shipper): array |
511
|
|
|
{ |
512
|
9 |
|
$response = $this->requester->call(API::V2V1, $shipper, Request::COUNTRIES); |
513
|
|
|
|
514
|
6 |
|
return $this->formatter->normalizeResponseItems( |
515
|
6 |
|
$response['service_types'] ?? [], |
516
|
6 |
|
'service_type', |
517
|
6 |
|
'countries', |
518
|
|
|
); |
519
|
|
|
} |
520
|
|
|
|
521
|
|
|
/** |
522
|
|
|
* Returns available branches for the given shipper and its service |
523
|
|
|
* |
524
|
|
|
* @param string $shipper |
525
|
|
|
* @param string $service |
526
|
|
|
* @param string|null $country |
527
|
|
|
* |
528
|
|
|
* @return array<array<string,mixed>> |
529
|
|
|
* |
530
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
531
|
|
|
*/ |
532
|
14 |
|
public function getPostCodes(string $shipper, string $service, ?string $country = null): array |
533
|
|
|
{ |
534
|
14 |
|
$response = $this->requester->call(API::V2V1, $shipper, Request::ZIP_CODES . '/' . $service . '/' . $country); |
535
|
|
|
|
536
|
11 |
|
return $this->formatter->normalizePostCodesResponse($response, $country); |
537
|
|
|
} |
538
|
|
|
|
539
|
|
|
/** |
540
|
|
|
* Check package(s) data |
541
|
|
|
* |
542
|
|
|
* @param string $shipper |
543
|
|
|
* @param array<array<string,mixed>> $packages |
544
|
|
|
* |
545
|
|
|
* @return void |
546
|
|
|
* |
547
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
548
|
|
|
*/ |
549
|
7 |
|
public function checkPackages(string $shipper, array $packages): void |
550
|
|
|
{ |
551
|
7 |
|
$this->requester->call(API::V2V1, $shipper, Request::CHECK, ['packages' => $packages]); |
552
|
3 |
|
} |
553
|
|
|
|
554
|
|
|
/** |
555
|
|
|
* Returns available manipulation units for the given shipper |
556
|
|
|
* |
557
|
|
|
* @param string $shipper |
558
|
|
|
* @param bool $fullData |
559
|
|
|
* |
560
|
|
|
* @return array<string> |
561
|
|
|
* |
562
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
563
|
|
|
*/ |
564
|
12 |
|
public function getAdrUnits(string $shipper, bool $fullData = false): array |
565
|
|
|
{ |
566
|
12 |
|
$response = $this->requester->call(API::V2V1, $shipper, Request::ADR_UNITS); |
567
|
|
|
|
568
|
9 |
|
return $this->formatter->normalizeResponseItems( |
569
|
9 |
|
$response['units'] ?? [], |
570
|
9 |
|
'code', |
571
|
9 |
|
$fullData === false ? 'name' : null, |
572
|
|
|
); |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* Returns available activated services for the given shipper |
577
|
|
|
* |
578
|
|
|
* @param string $shipper |
579
|
|
|
* |
580
|
|
|
* @return array<string,mixed> |
581
|
|
|
* |
582
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
583
|
|
|
*/ |
584
|
7 |
|
public function getActivatedServices(string $shipper): array |
585
|
|
|
{ |
586
|
7 |
|
$response = $this->requester->call(API::V2V1, $shipper, Request::ACTIVATED_SERVICES); |
587
|
|
|
|
588
|
5 |
|
return $this->formatter->withoutStatus($response); |
589
|
|
|
} |
590
|
|
|
|
591
|
|
|
/** |
592
|
|
|
* Order shipments from place B (typically supplier / previous consignee) to place A (shipping point) |
593
|
|
|
* |
594
|
|
|
* @param string $shipper |
595
|
|
|
* @param array<array<string,mixed>> $packages |
596
|
|
|
* |
597
|
|
|
* @return array<array<string,mixed>> |
598
|
|
|
* |
599
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
600
|
|
|
*/ |
601
|
13 |
|
public function orderB2AShipment(string $shipper, array $packages): array |
602
|
|
|
{ |
603
|
13 |
|
$response = $this->requester->call(API::V1, $shipper, Request::B2A, $packages); |
604
|
|
|
|
605
|
9 |
|
$response = $this->formatter->withoutStatus($response); |
606
|
|
|
|
607
|
9 |
|
$this->validator->validateIndexes($response, count($packages)); |
608
|
|
|
|
609
|
8 |
|
$this->validator->validateResponseItemHasAttribute($response, 'package_id', $response); |
610
|
|
|
|
611
|
6 |
|
return $response; |
612
|
|
|
} |
613
|
|
|
|
614
|
|
|
/** |
615
|
|
|
* Get PDF link with signed consignment delivery document by the recipient |
616
|
|
|
* |
617
|
|
|
* @param string $shipper |
618
|
|
|
* @param string $carrierId |
619
|
|
|
* |
620
|
|
|
* @return string |
621
|
|
|
* |
622
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
623
|
|
|
*/ |
624
|
7 |
|
public function getProofOfDelivery(string $shipper, string $carrierId): string |
625
|
|
|
{ |
626
|
7 |
|
return $this->getProofOfDeliveries($shipper, [$carrierId])[0]; |
627
|
|
|
} |
628
|
|
|
|
629
|
|
|
/** |
630
|
|
|
* Get array of PDF links with signed consignment delivery document by the recipient |
631
|
|
|
* |
632
|
|
|
* @param string $shipper |
633
|
|
|
* @param array<string> $carrierIds |
634
|
|
|
* |
635
|
|
|
* @return array<string> |
636
|
|
|
* |
637
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
638
|
|
|
*/ |
639
|
21 |
|
public function getProofOfDeliveries(string $shipper, array $carrierIds): array |
640
|
|
|
{ |
641
|
21 |
|
$response = $this->requester->call( |
642
|
21 |
|
API::V1, |
643
|
|
|
$shipper, |
644
|
21 |
|
Request::PROOF_OF_DELIVERY, |
645
|
21 |
|
$this->formatter->encapsulateIds($carrierIds, 'id'), |
646
|
21 |
|
shouldHaveStatus: false, |
647
|
|
|
); |
648
|
|
|
|
649
|
17 |
|
$response = $this->formatter->withoutStatus($response); |
650
|
|
|
|
651
|
17 |
|
$this->validator->validateIndexes($response, count($carrierIds)); |
652
|
|
|
|
653
|
13 |
|
return $this->formatter->normalizeProofOfDeliveriesResponse($response); |
654
|
|
|
} |
655
|
|
|
|
656
|
|
|
/** |
657
|
|
|
* Obtain the price of carriage at consignment level |
658
|
|
|
* |
659
|
|
|
* @param string $shipper |
660
|
|
|
* @param array<array<string,mixed>> $packages |
661
|
|
|
* |
662
|
|
|
* @return array<array<string,mixed>> |
663
|
|
|
* |
664
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
665
|
|
|
*/ |
666
|
12 |
|
public function getTransportCosts(string $shipper, array $packages): array |
667
|
|
|
{ |
668
|
12 |
|
$response = $this->requester->call(API::V1, $shipper, Request::TRANSPORT_COSTS, $packages); |
669
|
|
|
|
670
|
8 |
|
unset($response['status']); |
671
|
|
|
|
672
|
8 |
|
$this->validator->validateIndexes($response, count($packages)); |
673
|
|
|
|
674
|
7 |
|
$this->validator->validateResponseItemHasAttribute($response, 'eid', $response); |
675
|
|
|
|
676
|
5 |
|
return $response; |
677
|
|
|
} |
678
|
|
|
|
679
|
|
|
/** |
680
|
|
|
* Get information on individual countries of the world |
681
|
|
|
* |
682
|
|
|
* @return array<array<string,mixed>> |
683
|
|
|
* |
684
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
685
|
|
|
*/ |
686
|
9 |
|
public function getCountriesData(): array |
687
|
|
|
{ |
688
|
9 |
|
$response = $this->requester->call(API::V2V1, '', Request::GET_COUNTRIES_DATA); |
689
|
|
|
|
690
|
6 |
|
return $this->formatter->normalizeResponseItems($response['countries'] ?? [], 'iso_code', null); |
691
|
|
|
} |
692
|
|
|
|
693
|
|
|
/** |
694
|
|
|
* Method for obtaining news in the Balikobot API |
695
|
|
|
* |
696
|
|
|
* @return array<string,mixed> |
697
|
|
|
* |
698
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
699
|
|
|
*/ |
700
|
6 |
|
public function getChangelog(): array |
701
|
|
|
{ |
702
|
6 |
|
$response = $this->requester->call(API::V2V1, '', Request::CHANGELOG); |
703
|
|
|
|
704
|
3 |
|
return $this->formatter->withoutStatus($response); |
705
|
|
|
} |
706
|
|
|
|
707
|
|
|
/** |
708
|
|
|
* Method for easier carrier integration, obtaining list of available input attributes for the ADD method |
709
|
|
|
* |
710
|
|
|
* @param string $shipper |
711
|
|
|
* |
712
|
|
|
* @return array<string,array<string,mixed>> |
713
|
|
|
* |
714
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
715
|
|
|
*/ |
716
|
9 |
|
public function getAddAttributes(string $shipper): array |
717
|
|
|
{ |
718
|
9 |
|
$response = $this->requester->call(API::V1, $shipper, Request::ADD_ATTRIBUTES); |
719
|
|
|
|
720
|
6 |
|
return $this->formatter->normalizeResponseItems( |
721
|
6 |
|
$response['attributes'] ?? [], |
722
|
6 |
|
'name', |
723
|
6 |
|
null, |
724
|
|
|
); |
725
|
|
|
} |
726
|
|
|
|
727
|
|
|
/** |
728
|
|
|
* Method for obtaining a list of additional services by individual transport services |
729
|
|
|
* |
730
|
|
|
* @param string $shipper |
731
|
|
|
* @param string|null $service |
732
|
|
|
* @param bool $fullData |
733
|
|
|
* |
734
|
|
|
* @return array<string,string|array<string,string|array<mixed,mixed>>> |
735
|
|
|
* |
736
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
737
|
|
|
*/ |
738
|
18 |
|
public function getAddServiceOptions(string $shipper, ?string $service = null, bool $fullData = false): array |
739
|
|
|
{ |
740
|
18 |
|
$response = $this->requester->call(API::V1, $shipper, Request::ADD_SERVICE_OPTIONS . '/' . $service); |
741
|
|
|
|
742
|
15 |
|
if ($service === null) { |
743
|
8 |
|
return $this->formatter->normalizeResponseIndexedItems( |
744
|
8 |
|
$response['service_types'] ?? [], |
745
|
8 |
|
'service_type', |
746
|
8 |
|
'services', |
747
|
8 |
|
'code', |
748
|
8 |
|
$fullData === false ? 'name' : null, |
749
|
|
|
); |
750
|
|
|
} |
751
|
|
|
|
752
|
7 |
|
return $this->formatter->normalizeResponseItems( |
753
|
7 |
|
$response['services'] ?? [], |
754
|
7 |
|
'code', |
755
|
7 |
|
$fullData === false ? 'name' : null, |
756
|
|
|
); |
757
|
|
|
} |
758
|
|
|
|
759
|
|
|
/** |
760
|
|
|
* Method for obtaining info about used API keys |
761
|
|
|
* |
762
|
|
|
* @return array<string,mixed> |
763
|
|
|
* |
764
|
|
|
* @throws \Inspirum\Balikobot\Contracts\ExceptionInterface |
765
|
|
|
*/ |
766
|
5 |
|
public function getAccountInfo(): array |
767
|
|
|
{ |
768
|
5 |
|
$response = $this->requester->call(API::V2V1, '', Request::INFO_WHO_AM_I); |
769
|
|
|
|
770
|
2 |
|
return $this->formatter->withoutStatus($response); |
771
|
|
|
} |
772
|
|
|
} |
773
|
|
|
|