|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Requests; |
|
6
|
|
|
|
|
7
|
|
|
use Appwilio\RussianPostSDK\Dispatching\DataAware; |
|
8
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Entities\Address; |
|
9
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Contracts\Arrayable; |
|
10
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\Order; |
|
11
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\OrderItem; |
|
12
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\Recipient; |
|
13
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\CustomsDeclaration; |
|
14
|
|
|
|
|
15
|
|
|
final class OrderRequest implements Arrayable |
|
16
|
|
|
{ |
|
17
|
|
|
use DataAware; |
|
18
|
|
|
|
|
19
|
|
|
private const RUSSIAN_POSTAL_CODE = '~\d{6}~'; |
|
20
|
|
|
|
|
21
|
|
|
/** @var Address */ |
|
22
|
|
|
private $address; |
|
23
|
|
|
|
|
24
|
|
|
/** @var OrderItem[] */ |
|
25
|
|
|
private $items = []; |
|
26
|
|
|
|
|
27
|
|
|
/** @var CustomsDeclaration|null */ |
|
28
|
|
|
private $declaration; |
|
29
|
|
|
|
|
30
|
|
|
/** @var Recipient */ |
|
31
|
|
|
private $recipient; |
|
32
|
|
|
|
|
33
|
|
|
public static function create( |
|
34
|
|
|
string $id, |
|
35
|
|
|
string $category, |
|
36
|
|
|
string $type, |
|
37
|
|
|
int $weight, |
|
38
|
|
|
Address $address, |
|
39
|
|
|
Recipient $recipient |
|
40
|
|
|
) { |
|
41
|
|
|
return new self(...\func_get_args()); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public static function fromOrder(Order $order) |
|
|
|
|
|
|
45
|
|
|
{ |
|
46
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function __construct( |
|
50
|
|
|
string $number, |
|
51
|
|
|
string $mailType, |
|
52
|
|
|
string $mailCategory, |
|
53
|
|
|
int $weight, |
|
54
|
|
|
Address $address, |
|
55
|
|
|
Recipient $recipient |
|
56
|
|
|
) { |
|
57
|
|
|
$this->data = [ |
|
58
|
|
|
'mass' => $weight, |
|
59
|
|
|
'order-num' => $number, |
|
60
|
|
|
'mail-type' => $mailType, |
|
61
|
|
|
'mail-category' => $mailCategory, |
|
62
|
|
|
]; |
|
63
|
|
|
|
|
64
|
|
|
$this->address = $address; |
|
65
|
|
|
$this->recipient = $recipient; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function dimensions(int $height, int $width, int $length) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->data['dimension'] = \compact('height', 'width', 'length'); |
|
71
|
|
|
|
|
72
|
|
|
return $this; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function fragile(bool $value = true) |
|
76
|
|
|
{ |
|
77
|
|
|
$this->data['fragile'] = $value; |
|
78
|
|
|
|
|
79
|
|
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function viaCourier(bool $value = true) |
|
83
|
|
|
{ |
|
84
|
|
|
$this->data['courier'] = $value; |
|
85
|
|
|
|
|
86
|
|
|
return $this; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
public function addItem(OrderItem $item): void |
|
90
|
|
|
{ |
|
91
|
|
|
$this->items[] = $item; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function withCustomsDeclaration(CustomsDeclaration $declaration) |
|
95
|
|
|
{ |
|
96
|
|
|
$this->declaration = $declaration; |
|
97
|
|
|
|
|
98
|
|
|
return $this; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function withInshurance(int $value) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->data['insr-value'] = $value; |
|
104
|
|
|
|
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function withInventory(bool $value) |
|
109
|
|
|
{ |
|
110
|
|
|
$this->data['inventory'] = $value; |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function withDeclaredValue(int $value) |
|
116
|
|
|
{ |
|
117
|
|
|
$this->data['payment'] = $value; |
|
118
|
|
|
|
|
119
|
|
|
return $this; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function withCompletenessChecking(bool $value = true) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->data['completeness-checking'] = $value; |
|
125
|
|
|
|
|
126
|
|
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function withVsd(bool $value = true) |
|
130
|
|
|
{ |
|
131
|
|
|
$this->data['vsd'] = $value; |
|
132
|
|
|
|
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function withElectronicNotice(bool $value = true) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->data['with-electronic-notice'] = $value; |
|
139
|
|
|
|
|
140
|
|
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
public function withSimpleNotice(bool $value = true) |
|
144
|
|
|
{ |
|
145
|
|
|
$this->data['with-simple-notice'] = $value; |
|
146
|
|
|
|
|
147
|
|
|
return $this; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function withRegisteredNotice(bool $value = true) |
|
151
|
|
|
{ |
|
152
|
|
|
$this->data['with-order-of-notice'] = $value; |
|
153
|
|
|
|
|
154
|
|
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function withoutMailRank(bool $value = true) |
|
158
|
|
|
{ |
|
159
|
|
|
$this->data['wo-mail-rank'] = $value; |
|
160
|
|
|
|
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
public function toArray(): array |
|
165
|
|
|
{ |
|
166
|
|
|
$this->data['custom-declaration'] = $this->declaration ? $this->declaration->toArray() : null; |
|
167
|
|
|
|
|
168
|
|
|
if (\count($this->items) > 0) { |
|
169
|
|
|
$this->data['goods'] = ['items']; |
|
170
|
|
|
|
|
171
|
|
|
foreach ($this->items as $item) { |
|
172
|
|
|
$this->data['goods']['items'][] = $item->toArray(); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
$this->data = \array_merge($this->data, $this->recipient); |
|
|
|
|
|
|
177
|
|
|
$this->data = \array_merge($this->data, \iterator_to_array($this->convertAddress($this->address))); |
|
178
|
|
|
|
|
179
|
|
|
return [$this->data]; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
private function convertAddress(Address $address): \Generator |
|
183
|
|
|
{ |
|
184
|
|
|
foreach ($address->toArray() as $key => $value) { |
|
185
|
|
|
if ($key === 'index' && !\preg_match(self::RUSSIAN_POSTAL_CODE, $value)) { |
|
186
|
|
|
yield 'str-index-to' => $value; |
|
187
|
|
|
} else if ($key === 'mail-direct') { |
|
188
|
|
|
yield $key => $value; |
|
189
|
|
|
} else { |
|
190
|
|
|
yield "{$key}-to" => $value; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.