1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Inspirum\Balikobot\Model\Branch; |
6
|
|
|
|
7
|
|
|
use Generator; |
8
|
|
|
use Inspirum\Balikobot\Definitions\Carrier; |
9
|
|
|
use Inspirum\Balikobot\Definitions\Service; |
10
|
|
|
use Traversable; |
11
|
|
|
use function sprintf; |
12
|
|
|
use function str_replace; |
13
|
|
|
use function trim; |
14
|
|
|
|
15
|
|
|
final class DefaultBranchFactory implements BranchFactory |
16
|
|
|
{ |
17
|
|
|
/** @inheritDoc */ |
18
|
161 |
|
public function create(string $carrier, ?string $service, array $data): Branch |
19
|
|
|
{ |
20
|
161 |
|
if ($carrier === Carrier::CP && $service === Service::CP_NP) { |
21
|
8 |
|
$data['country'] ??= 'CZ'; |
22
|
|
|
} |
23
|
|
|
|
24
|
161 |
|
if (isset($data['street']) && (isset($data['house_number']) || isset($data['orientation_number']))) { |
25
|
4 |
|
$houseNumber = (int) ($data['house_number'] ?? 0); |
26
|
4 |
|
$orientationNumber = (int) ($data['orientation_number'] ?? 0); |
27
|
4 |
|
$streetNumber = trim( |
28
|
4 |
|
sprintf( |
29
|
|
|
'%s/%s', |
30
|
4 |
|
$houseNumber > 0 ? $houseNumber : '', |
31
|
4 |
|
$orientationNumber > 0 ? $orientationNumber : '' |
32
|
|
|
), |
33
|
|
|
'/' |
34
|
|
|
); |
35
|
|
|
|
36
|
4 |
|
$data['street'] = trim(sprintf('%s %s', $data['street'] ?: ($data['city'] ?? ''), $streetNumber)); |
37
|
|
|
} |
38
|
|
|
|
39
|
161 |
|
$id = $data['branch_id'] ?? (isset($data['id']) ? (string) $data['id'] : null); |
40
|
161 |
|
$zip = $data['zip'] ?? '00000'; |
41
|
161 |
|
$name = $data['name'] ?? $zip; |
42
|
|
|
|
43
|
161 |
|
return new DefaultBranch( |
44
|
|
|
$carrier, |
45
|
|
|
$service, |
46
|
161 |
|
$this->resolveBranchId($carrier, $service, [ |
47
|
|
|
'id' => $id, |
48
|
|
|
'zip' => $zip, |
49
|
|
|
'name' => $name, |
50
|
|
|
]), |
51
|
|
|
$id, |
52
|
161 |
|
$data['branch_uid'] ?? null, |
53
|
161 |
|
$data['type'] ?? 'branch', |
54
|
|
|
$name, |
55
|
161 |
|
$data['city'] ?? '', |
56
|
161 |
|
$data['street'] ?? ($data['address'] ?? ''), |
57
|
|
|
$zip, |
58
|
161 |
|
$data['country'] ?? null, |
59
|
161 |
|
$data['city_part'] ?? null, |
60
|
161 |
|
$data['district'] ?? null, |
61
|
161 |
|
$data['region'] ?? null, |
62
|
161 |
|
$data['currency'] ?? null, |
63
|
161 |
|
$data['photo_small'] ?? null, |
64
|
161 |
|
$data['photo_big'] ?? null, |
65
|
161 |
|
$data['url'] ?? null, |
66
|
161 |
|
(isset($data['latitude']) ? (float) trim((string) $data['latitude']) : null) ?: null, |
67
|
161 |
|
(isset($data['longitude']) ? (float) trim((string) $data['longitude']) : null) ?: null, |
68
|
161 |
|
$data['directions_global'] ?? null, |
69
|
161 |
|
$data['directions_car'] ?? null, |
70
|
161 |
|
$data['directions_public'] ?? null, |
71
|
161 |
|
isset($data['wheelchair_accessible']) ? (bool) $data['wheelchair_accessible'] : null, |
72
|
161 |
|
isset($data['claim_assistant']) ? (bool) $data['claim_assistant'] : null, |
73
|
161 |
|
isset($data['dressing_room']) ? (bool) $data['dressing_room'] : null, |
74
|
161 |
|
$data['opening_monday'] ?? null, |
75
|
161 |
|
$data['opening_tuesday'] ?? null, |
76
|
161 |
|
$data['opening_wednesday'] ?? null, |
77
|
161 |
|
$data['opening_thursday'] ?? null, |
78
|
161 |
|
$data['opening_friday'] ?? null, |
79
|
161 |
|
$data['opening_saturday'] ?? null, |
80
|
161 |
|
$data['opening_sunday'] ?? null, |
81
|
161 |
|
isset($data['max_weight']) ? (float) $data['max_weight'] : null |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** @inheritDoc */ |
86
|
154 |
|
public function createIterator(string $carrier, ?string $service, ?array $countries, array $data): BranchIterator |
87
|
|
|
{ |
88
|
154 |
|
return new DefaultBranchIterator($carrier, $service, $countries, $this->generate($carrier, $service, $data)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** @inheritDoc */ |
92
|
82 |
|
public function wrapIterator(?string $carrier, ?string $service, ?array $countries, Traversable $iterator): BranchIterator |
93
|
|
|
{ |
94
|
82 |
|
return new DefaultBranchIterator($carrier, $service, $countries, $iterator); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param array<string,mixed> $data |
99
|
|
|
* |
100
|
|
|
* @return \Generator<int,\Inspirum\Balikobot\Model\Branch\Branch> |
101
|
|
|
*/ |
102
|
154 |
|
private function generate(string $carrier, ?string $service, array $data): Generator |
103
|
|
|
{ |
104
|
154 |
|
foreach ($data['branches'] ?? [] as $branch) { |
105
|
154 |
|
yield $this->create($carrier, $service, $branch); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @param array<string,mixed> $data |
111
|
|
|
*/ |
112
|
161 |
|
private function resolveBranchId(string $carrier, ?string $service, array $data): string |
113
|
|
|
{ |
114
|
|
|
// get key used in branch_id when calling add request |
115
|
|
|
if ( |
116
|
161 |
|
$carrier === Carrier::CP |
117
|
151 |
|
|| $carrier === Carrier::SP |
118
|
161 |
|
|| ($carrier === Carrier::ULOZENKA && $service === Service::ULOZENKA_CP_NP) |
119
|
|
|
) { |
120
|
26 |
|
return str_replace(' ', '', $data['zip']); |
121
|
|
|
} |
122
|
|
|
|
123
|
137 |
|
if ($carrier === Carrier::PPL) { |
124
|
12 |
|
return str_replace('KM', '', (string) $data['id']); |
125
|
|
|
} |
126
|
|
|
|
127
|
127 |
|
if ($carrier === Carrier::INTIME) { |
128
|
8 |
|
return $data['name']; |
129
|
|
|
} |
130
|
|
|
|
131
|
121 |
|
return (string) $data['id']; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|