1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Bpost\BpostApiClient\Bpost\Order\Box\CustomsInfo; |
5
|
|
|
|
6
|
|
|
use Bpost\BpostApiClient\Common\XmlHelper; |
7
|
|
|
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidLengthException; |
8
|
|
|
use Bpost\BpostApiClient\Exception\BpostLogicException\BpostInvalidValueException; |
9
|
|
|
use DOMDocument; |
10
|
|
|
use DOMElement; |
11
|
|
|
use DOMException; |
12
|
|
|
use SimpleXMLElement; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* bPost CustomsInfo class |
16
|
|
|
* |
17
|
|
|
* @author Tijs Verkoyen <[email protected]> |
18
|
|
|
* |
19
|
|
|
* @version 3.0.0 |
20
|
|
|
* |
21
|
|
|
* @copyright Copyright (c), Tijs Verkoyen. All rights reserved. |
22
|
|
|
* @license BSD License |
23
|
|
|
*/ |
24
|
|
|
class CustomsInfo |
25
|
|
|
{ |
26
|
|
|
public const CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_RTA = 'RTA'; |
27
|
|
|
public const CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_RTS = 'RTS'; |
28
|
|
|
public const CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_ABANDONED = 'ABANDONED'; |
29
|
|
|
|
30
|
|
|
public const CUSTOM_INFO_SHIPMENT_TYPE_SAMPLE = 'SAMPLE'; |
31
|
|
|
public const CUSTOM_INFO_SHIPMENT_TYPE_GIFT = 'GIFT'; |
32
|
|
|
public const CUSTOM_INFO_SHIPMENT_TYPE_GOODS = 'GOODS'; |
33
|
|
|
public const CUSTOM_INFO_SHIPMENT_TYPE_DOCUMENTS = 'DOCUMENTS'; |
34
|
|
|
public const CUSTOM_INFO_SHIPMENT_TYPE_OTHER = 'OTHER'; |
35
|
|
|
|
36
|
|
|
public const CUSTOM_INFO_CURRENCY_EUR = 'EUR'; |
37
|
|
|
public const CUSTOM_INFO_CURRENCY_GBP = 'GBP'; |
38
|
|
|
public const CUSTOM_INFO_CURRENCY_USD = 'USD'; |
39
|
|
|
public const CUSTOM_INFO_CURRENCY_CNY = 'CNY'; |
40
|
|
|
|
41
|
|
|
private ?int $parcelValue = null; |
42
|
|
|
private ?string $contentDescription = null; |
43
|
|
|
private ?string $shipmentType = null; |
44
|
|
|
private ?string $parcelReturnInstructions = null; |
45
|
|
|
private ?bool $privateAddress = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* this is the currency used for field parcelValue.In case of shipment to non-European country, |
49
|
|
|
* this is also the currency used for all parcel contents value (field valueOfitem) in 3 letters format. |
50
|
|
|
* |
51
|
|
|
* Possible values are: EUR=Euro GBP=Pound Sterling USD=US Dollar CNY=Yuan Renminbi |
52
|
|
|
* |
53
|
|
|
*/ |
54
|
|
|
private ?string $currency = null; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Amount paid by the sender for the sending of this shipment. See contract pricing with bpost. |
58
|
|
|
* Decimal format field (3.2) |
59
|
|
|
* Minimum value : 0 |
60
|
|
|
* Maximum value : 999.99 |
61
|
|
|
* Currency for field amtPostagePaidByAddresse is always EUR ! |
62
|
|
|
* |
63
|
|
|
*/ |
64
|
|
|
private ?float $amtPostagePaidByAddresse = null; |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @throws BpostInvalidLengthException |
69
|
|
|
*/ |
70
|
|
|
public function setContentDescription(?string $contentDescription): void |
71
|
|
|
{ |
72
|
|
|
if ($contentDescription === null) { |
73
|
|
|
$this->contentDescription = null; |
74
|
|
|
return; |
75
|
|
|
} |
76
|
|
|
$length = 50; |
77
|
|
|
if (mb_strlen($contentDescription) > $length) { |
78
|
|
|
throw new BpostInvalidLengthException('contentDescription', mb_strlen($contentDescription), $length); |
79
|
|
|
} |
80
|
|
|
$this->contentDescription = $contentDescription; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function getContentDescription(): ?string |
84
|
|
|
{ |
85
|
|
|
return $this->contentDescription; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @throws BpostInvalidValueException |
90
|
|
|
*/ |
91
|
|
|
public function setParcelReturnInstructions(?string $parcelReturnInstructions): void |
92
|
|
|
{ |
93
|
|
|
if ($parcelReturnInstructions === null) { |
94
|
|
|
$this->parcelReturnInstructions = null; |
95
|
|
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$normalized = strtoupper($parcelReturnInstructions); |
99
|
|
|
if (!in_array($normalized, self::getPossibleParcelReturnInstructionValues(), true)) { |
100
|
|
|
throw new BpostInvalidValueException( |
101
|
|
|
'parcelReturnInstructions', |
102
|
|
|
$normalized, |
103
|
|
|
self::getPossibleParcelReturnInstructionValues() |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
$this->parcelReturnInstructions = $normalized; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function getParcelReturnInstructions(): ?string |
110
|
|
|
{ |
111
|
|
|
return $this->parcelReturnInstructions; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
public static function getPossibleParcelReturnInstructionValues(): array |
115
|
|
|
{ |
116
|
|
|
return [ |
117
|
|
|
self::CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_RTA, |
118
|
|
|
self::CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_RTS, |
119
|
|
|
self::CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_ABANDONED, |
120
|
|
|
]; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function setParcelValue(?int $parcelValue): void |
124
|
|
|
{ |
125
|
|
|
$this->parcelValue = $parcelValue; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getParcelValue(): ?int |
129
|
|
|
{ |
130
|
|
|
return $this->parcelValue; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function setPrivateAddress(?bool $privateAddress): void |
134
|
|
|
{ |
135
|
|
|
$this->privateAddress = $privateAddress; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getPrivateAddress(): ?bool |
139
|
|
|
{ |
140
|
|
|
return $this->privateAddress; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @throws BpostInvalidValueException |
145
|
|
|
*/ |
146
|
|
|
public function setShipmentType(?string $shipmentType): void |
147
|
|
|
{ |
148
|
|
|
if ($shipmentType === null) { |
149
|
|
|
$this->shipmentType = null; |
150
|
|
|
return; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
$normalized = strtoupper($shipmentType); |
154
|
|
|
if (!in_array($normalized, self::getPossibleShipmentTypeValues(), true)) { |
155
|
|
|
throw new BpostInvalidValueException('shipmentType', $normalized, self::getPossibleShipmentTypeValues()); |
156
|
|
|
} |
157
|
|
|
$this->shipmentType = $normalized; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function getShipmentType(): ?string |
161
|
|
|
{ |
162
|
|
|
return $this->shipmentType; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return array |
168
|
|
|
*/ |
169
|
|
|
public static function getPossibleShipmentTypeValues(): array |
170
|
|
|
{ |
171
|
|
|
return [ |
172
|
|
|
self::CUSTOM_INFO_SHIPMENT_TYPE_SAMPLE, |
173
|
|
|
self::CUSTOM_INFO_SHIPMENT_TYPE_GIFT, |
174
|
|
|
self::CUSTOM_INFO_SHIPMENT_TYPE_GOODS, |
175
|
|
|
self::CUSTOM_INFO_SHIPMENT_TYPE_DOCUMENTS, |
176
|
|
|
self::CUSTOM_INFO_SHIPMENT_TYPE_OTHER, |
177
|
|
|
]; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function getAmtPostagePaidByAddresse(): ?float |
181
|
|
|
{ |
182
|
|
|
return $this->amtPostagePaidByAddresse; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function setAmtPostagePaidByAddresse(?float $amtPostagePaidByAddresse): void |
186
|
|
|
{ |
187
|
|
|
$this->amtPostagePaidByAddresse = $amtPostagePaidByAddresse; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function getCurrency(): ?string |
191
|
|
|
{ |
192
|
|
|
return $this->currency; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @throws BpostInvalidValueException |
198
|
|
|
*/ |
199
|
|
|
public function setCurrency(?string $currency): void |
200
|
|
|
{ |
201
|
|
|
if ($currency === null) { |
202
|
|
|
$this->currency = null; |
203
|
|
|
return; |
204
|
|
|
} |
205
|
|
|
if (!in_array($currency, self::getPossibleCurrencyValues(), true)) { |
206
|
|
|
throw new BpostInvalidValueException('currency', $currency, self::getPossibleCurrencyValues()); |
207
|
|
|
} |
208
|
|
|
$this->currency = $currency; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public static function getPossibleCurrencyValues(): array |
212
|
|
|
{ |
213
|
|
|
return [ |
214
|
|
|
self::CUSTOM_INFO_CURRENCY_EUR, |
215
|
|
|
self::CUSTOM_INFO_CURRENCY_GBP, |
216
|
|
|
self::CUSTOM_INFO_CURRENCY_USD, |
217
|
|
|
self::CUSTOM_INFO_CURRENCY_CNY, |
218
|
|
|
]; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @throws DOMException |
223
|
|
|
*/ |
224
|
|
|
public function toXML(DOMDocument $document, ?string $prefix = null): DOMElement |
225
|
|
|
{ |
226
|
|
|
$customsInfo = $document->createElement(XmlHelper::getPrefixedTagName('customsInfo', $prefix)); |
227
|
|
|
|
228
|
|
|
$this->parcelValueToXML($document, $prefix, $customsInfo); |
229
|
|
|
$this->contentDescriptionToXML($document, $prefix, $customsInfo); |
230
|
|
|
$this->shipmentTypeToXML($document, $prefix, $customsInfo); |
231
|
|
|
$this->parcelReturnInstructionValuesToXML($document, $prefix, $customsInfo); |
232
|
|
|
$this->privateAddressToXML($document, $prefix, $customsInfo); |
233
|
|
|
$this->currencyToXML($document, $prefix, $customsInfo); |
234
|
|
|
$this->amtPostagePaidByAddresseToXML($document, $prefix, $customsInfo); |
235
|
|
|
|
236
|
|
|
return $customsInfo; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* @throws BpostInvalidLengthException |
241
|
|
|
* @throws BpostInvalidValueException |
242
|
|
|
*/ |
243
|
|
|
public static function createFromXML(SimpleXMLElement $xml): self |
244
|
|
|
{ |
245
|
|
|
$customsInfo = new self(); |
246
|
|
|
|
247
|
|
|
if (isset($xml->parcelValue) && (string)$xml->parcelValue !== '') { |
248
|
|
|
$customsInfo->setParcelValue((int)$xml->parcelValue); |
249
|
|
|
} |
250
|
|
|
if (isset($xml->contentDescription) && (string)$xml->contentDescription !== '') { |
251
|
|
|
$customsInfo->setContentDescription((string)$xml->contentDescription); |
252
|
|
|
} |
253
|
|
|
if (isset($xml->shipmentType) && (string)$xml->shipmentType !== '') { |
254
|
|
|
$customsInfo->setShipmentType((string)$xml->shipmentType); |
255
|
|
|
} |
256
|
|
|
if (isset($xml->parcelReturnInstructions) && (string)$xml->parcelReturnInstructions !== '') { |
257
|
|
|
$customsInfo->setParcelReturnInstructions((string)$xml->parcelReturnInstructions); |
258
|
|
|
} |
259
|
|
|
if (isset($xml->privateAddress) && (string)$xml->privateAddress !== '') { |
260
|
|
|
$customsInfo->setPrivateAddress(in_array((string)$xml->privateAddress, ['true', '1'], true)); |
261
|
|
|
} |
262
|
|
|
if (isset($xml->currency) && (string)$xml->currency !== '') { |
263
|
|
|
$customsInfo->setCurrency((string)$xml->currency); |
264
|
|
|
} |
265
|
|
|
if (isset($xml->amtPostagePaidByAddresse) && (string)$xml->amtPostagePaidByAddresse !== '') { |
266
|
|
|
$customsInfo->setAmtPostagePaidByAddresse((float)$xml->amtPostagePaidByAddresse); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
return $customsInfo; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** @throws DOMException */ |
273
|
|
|
private function parcelValueToXML(DOMDocument $document, ?string $prefix, DOMElement $customsInfo): void |
274
|
|
|
{ |
275
|
|
|
if ($this->getParcelValue() !== null) { |
276
|
|
|
$customsInfo->appendChild( |
277
|
|
|
$document->createElement( |
278
|
|
|
XmlHelper::getPrefixedTagName('parcelValue', $prefix), |
279
|
|
|
(string)$this->getParcelValue() |
280
|
|
|
) |
281
|
|
|
); |
282
|
|
|
} |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** @throws DOMException */ |
286
|
|
|
private function currencyToXML(DOMDocument $document, ?string $prefix, DOMElement $customsInfo): void |
287
|
|
|
{ |
288
|
|
|
if ($this->getCurrency() !== null) { |
289
|
|
|
$customsInfo->appendChild( |
290
|
|
|
$document->createElement( |
291
|
|
|
XmlHelper::getPrefixedTagName('currency', $prefix), |
292
|
|
|
$this->getCurrency() |
293
|
|
|
) |
294
|
|
|
); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** @throws DOMException */ |
299
|
|
|
private function amtPostagePaidByAddresseToXML(DOMDocument $document, ?string $prefix, DOMElement $customsInfo): void |
300
|
|
|
{ |
301
|
|
|
if ($this->getAmtPostagePaidByAddresse() !== null) { |
302
|
|
|
$customsInfo->appendChild( |
303
|
|
|
$document->createElement( |
304
|
|
|
XmlHelper::getPrefixedTagName('amtPostagePaidByAddresse', $prefix), |
305
|
|
|
sprintf('%0.2f', $this->getAmtPostagePaidByAddresse()) |
306
|
|
|
) |
307
|
|
|
); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** @throws DOMException */ |
312
|
|
|
private function contentDescriptionToXML(DOMDocument $document, ?string $prefix, DOMElement $customsInfo): void |
313
|
|
|
{ |
314
|
|
|
if ($this->getContentDescription() !== null) { |
315
|
|
|
$customsInfo->appendChild( |
316
|
|
|
$document->createElement( |
317
|
|
|
XmlHelper::getPrefixedTagName('contentDescription', $prefix), |
318
|
|
|
$this->getContentDescription() |
319
|
|
|
) |
320
|
|
|
); |
321
|
|
|
} |
322
|
|
|
} |
323
|
|
|
|
324
|
|
|
/** @throws DOMException */ |
325
|
|
|
private function shipmentTypeToXML(DOMDocument $document, ?string $prefix, DOMElement $customsInfo): void |
326
|
|
|
{ |
327
|
|
|
if ($this->getShipmentType() !== null) { |
328
|
|
|
$customsInfo->appendChild( |
329
|
|
|
$document->createElement( |
330
|
|
|
XmlHelper::getPrefixedTagName('shipmentType', $prefix), |
331
|
|
|
$this->getShipmentType() |
332
|
|
|
) |
333
|
|
|
); |
334
|
|
|
} |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
/** @throws DOMException */ |
338
|
|
|
private function parcelReturnInstructionValuesToXML(DOMDocument $document, ?string $prefix, DOMElement $customsInfo): void |
339
|
|
|
{ |
340
|
|
|
if ($this->getParcelReturnInstructions() !== null) { |
341
|
|
|
$customsInfo->appendChild( |
342
|
|
|
$document->createElement( |
343
|
|
|
XmlHelper::getPrefixedTagName('parcelReturnInstructions', $prefix), |
344
|
|
|
$this->getParcelReturnInstructions() |
345
|
|
|
) |
346
|
|
|
); |
347
|
|
|
} |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** @throws DOMException */ |
351
|
|
|
private function privateAddressToXML(DOMDocument $document, ?string $prefix, DOMElement $customsInfo): void |
352
|
|
|
{ |
353
|
|
|
if ($this->getPrivateAddress() !== null) { |
354
|
|
|
$value = $this->getPrivateAddress() ? 'true' : 'false'; |
355
|
|
|
$customsInfo->appendChild( |
356
|
|
|
$document->createElement( |
357
|
|
|
XmlHelper::getPrefixedTagName('privateAddress', $prefix), |
358
|
|
|
$value |
359
|
|
|
) |
360
|
|
|
); |
361
|
|
|
} |
362
|
|
|
} |
363
|
|
|
} |
364
|
|
|
|