|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Inspirum\Balikobot\Model\Package; |
|
6
|
|
|
|
|
7
|
|
|
use Inspirum\Balikobot\Client\Response\Validator; |
|
8
|
|
|
use Inspirum\Balikobot\Definitions\Attribute; |
|
9
|
|
|
use function count; |
|
10
|
|
|
|
|
11
|
|
|
final class DefaultPackageFactory implements PackageFactory |
|
12
|
|
|
{ |
|
13
|
34 |
|
public function __construct( |
|
14
|
|
|
private readonly Validator $validator, |
|
15
|
|
|
) { |
|
16
|
34 |
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** @inheritDoc */ |
|
19
|
12 |
|
public function create(string $carrier, array $data): Package |
|
20
|
|
|
{ |
|
21
|
12 |
|
return new DefaultPackage( |
|
22
|
12 |
|
$carrier, |
|
23
|
12 |
|
(string) $data['package_id'], |
|
24
|
12 |
|
$data['eid'], |
|
25
|
12 |
|
(string) ($data['carrier_id'] ?? ''), |
|
26
|
12 |
|
$data['track_url'] ?? null, |
|
27
|
12 |
|
$data['label_url'] ?? null, |
|
28
|
12 |
|
$data['carrier_id_swap'] ?? null, |
|
29
|
12 |
|
$data['pieces'] ?? [], |
|
30
|
12 |
|
$data['carrier_id_final'] ?? null, |
|
31
|
12 |
|
$data['track_url_final'] ?? null, |
|
32
|
12 |
|
$data['barcode'] ?? null, |
|
33
|
12 |
|
); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** @inheritDoc */ |
|
37
|
15 |
|
public function createCollection(string $carrier, ?array $packages, array $data): PackageCollection |
|
38
|
|
|
{ |
|
39
|
15 |
|
$packagesResponse = $data['packages']; |
|
40
|
15 |
|
if ($packages !== null) { |
|
|
|
|
|
|
41
|
14 |
|
$this->validator->validateIndexes($packagesResponse, count($packages)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
13 |
|
$orderedPackages = new DefaultPackageCollection( |
|
45
|
13 |
|
$carrier, |
|
46
|
13 |
|
labelsUrl: $data['labels_url'] ?? null, |
|
47
|
13 |
|
); |
|
48
|
|
|
|
|
49
|
13 |
|
foreach ($packagesResponse as $i => $package) { |
|
50
|
13 |
|
$this->validator->validateResponseStatus($package, $data, false); |
|
51
|
|
|
|
|
52
|
12 |
|
$package[Attribute::EID] ??= $packages[$i][Attribute::EID] ?? null; |
|
53
|
|
|
|
|
54
|
12 |
|
$orderedPackages->add($this->create($carrier, $package)); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
12 |
|
return $orderedPackages; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|