1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Requests; |
6
|
|
|
|
7
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Enum\MailType; |
8
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Entities\Address; |
9
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Enum\MailCategory; |
10
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Contracts\Arrayable; |
11
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Entities\CommonOrder; |
12
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\Order; |
13
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\EcomData; |
14
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\OrderItem; |
15
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\Recipient; |
16
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\FiscalData; |
17
|
|
|
use Appwilio\RussianPostSDK\Dispatching\Endpoints\Orders\Entites\CustomsDeclaration; |
18
|
|
|
|
19
|
|
|
final class OrderRequest implements Arrayable |
20
|
|
|
{ |
21
|
|
|
use CommonOrder; |
22
|
|
|
|
23
|
|
|
private const RUSSIAN_POSTAL_CODE = '~\d{6}~'; |
24
|
|
|
|
25
|
|
|
private $data; |
26
|
|
|
|
27
|
|
|
/** @var Address */ |
28
|
|
|
private $address; |
29
|
|
|
|
30
|
|
|
/** @var OrderItem[] */ |
31
|
|
|
private $items = []; |
32
|
|
|
|
33
|
|
|
/** @var CustomsDeclaration|null */ |
34
|
|
|
private $declaration; |
35
|
|
|
|
36
|
|
|
/** @var EcomData */ |
37
|
|
|
private $ecomData; |
38
|
|
|
|
39
|
|
|
/** @var FiscalData */ |
40
|
|
|
private $fiscalData; |
41
|
|
|
|
42
|
|
|
/** @var Recipient */ |
43
|
|
|
private $recipient; |
44
|
|
|
|
45
|
|
|
public static function create( |
46
|
|
|
string $id, |
47
|
|
|
string $type, |
48
|
|
|
string $category, |
49
|
|
|
int $weight, |
50
|
|
|
Address $address, |
51
|
|
|
Recipient $recipient |
52
|
|
|
) { |
53
|
|
|
return new self(...\func_get_args()); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public static function fromOrder(Order $order): self |
57
|
|
|
{ |
58
|
|
|
$request = new self( |
59
|
|
|
$order->getNumber(), MailType::BANDEROL(), MailCategory::ORDINARY(), |
60
|
|
|
$order->getWeight(), $order->getAddress(), $order->getRecipient() |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
if ($order->hasCustomsDeclaration()) { |
64
|
|
|
$request->withCustomsDeclaration($order->getCustomsDeclaration()); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
foreach ($order->getItems() as $item) { |
68
|
|
|
$request->addItem($item); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $request; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function __construct( |
75
|
|
|
string $number, |
76
|
|
|
MailType $mailType, |
77
|
|
|
MailCategory $mailCategory, |
78
|
|
|
int $weight, |
79
|
|
|
Address $address, |
80
|
|
|
Recipient $recipient |
81
|
|
|
) { |
82
|
|
|
$this->data = [ |
83
|
|
|
'mass' => $weight, |
84
|
|
|
'order-num' => $number, |
85
|
|
|
'mail-type' => $mailType, |
86
|
|
|
'mail-category' => $mailCategory, |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
$this->address = $address; |
90
|
|
|
$this->recipient = $recipient; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
public function addItem(OrderItem $item): void |
94
|
|
|
{ |
95
|
|
|
$this->items[] = $item; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function withCustomsDeclaration(CustomsDeclaration $declaration) |
99
|
|
|
{ |
100
|
|
|
$this->declaration = $declaration; |
101
|
|
|
|
102
|
|
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function withEcomData(EcomData $data) |
106
|
|
|
{ |
107
|
|
|
$this->ecomData = $data; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function withFiscalData(FiscalData $data) |
113
|
|
|
{ |
114
|
|
|
$this->fiscalData = $data; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function withInshurance(int $value) |
120
|
|
|
{ |
121
|
|
|
$this->data['insr-value'] = $value; |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function withDeclaredValue(int $value) |
127
|
|
|
{ |
128
|
|
|
$this->data['payment'] = $value; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function withoutMailRank(bool $value = true) |
134
|
|
|
{ |
135
|
|
|
$this->data['wo-mail-rank'] = $value; |
136
|
|
|
|
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function toArray(): array |
141
|
|
|
{ |
142
|
|
|
$this->data['ecom-data'] = $this->ecomData ? $this->ecomData->toArray() : null; |
143
|
|
|
$this->data['fiscal-data'] = $this->fiscalData ? $this->fiscalData->toArray() : null; |
144
|
|
|
$this->data['custom-declaration'] = $this->declaration ? $this->declaration->toArray() : null; |
145
|
|
|
|
146
|
|
|
if (\count($this->items) > 0) { |
147
|
|
|
$this->data['goods'] = ['items']; |
148
|
|
|
|
149
|
|
|
foreach ($this->items as $item) { |
150
|
|
|
$this->data['goods']['items'][] = $item->toArray(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
$this->data = \array_merge( |
155
|
|
|
$this->data, |
156
|
|
|
$this->recipient->toArray(), |
157
|
|
|
\iterator_to_array($this->convertAddress($this->address)) |
158
|
|
|
); |
159
|
|
|
|
160
|
|
|
return [$this->data]; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
private function convertAddress(Address $address): \Generator |
164
|
|
|
{ |
165
|
|
|
foreach ($address->toArray() as $key => $value) { |
166
|
|
|
if ($key === 'index' && !\preg_match(self::RUSSIAN_POSTAL_CODE, $value)) { |
167
|
|
|
yield 'str-index-to' => $value; |
168
|
|
|
} elseif ($key === 'mail-direct') { |
169
|
|
|
yield $key => $value; |
170
|
|
|
} else { |
171
|
|
|
yield "{$key}-to" => $value; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|