/
ShipmentTransformer.php
130 lines (120 loc) · 5.44 KB
/
ShipmentTransformer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
namespace MyParcelCom\Microservice\Shipments;
use MyParcelCom\JsonApi\Transformers\AbstractTransformer;
use MyParcelCom\JsonApi\Transformers\TransformerException;
use MyParcelCom\Microservice\PickUpDropOffLocations\Address;
class ShipmentTransformer extends AbstractTransformer
{
/** @var string */
protected $type = 'shipments';
/**
* @param Shipment $shipment
* @return string
*/
public function getId($shipment): string
{
$this->validateModel($shipment);
return $shipment->getId();
}
/**
* @param Shipment $shipment
* @return array
*/
public function getAttributes($shipment): array
{
$this->validateModel($shipment);
return array_filter([
'recipient_address' => $this->transformAddress($shipment->getRecipientAddress()),
'sender_address' => $this->transformAddress($shipment->getSenderAddress()),
'return_address' => $this->transformAddress($shipment->getReturnAddress()),
'pickup_location' => $shipment->getPickupLocationCode() === null ? null : [
'code' => $shipment->getPickupLocationCode(),
'address' => $this->transformAddress($shipment->getPickupLocationAddress()),
],
'description' => $shipment->getDescription(),
'barcode' => $shipment->getBarcode(),
'tracking_code' => $shipment->getTrackingCode(),
'tracking_url' => $shipment->getTrackingUrl(),
'service' => [
'code' => $shipment->getService()->getCode(),
'name' => $shipment->getService()->getName(),
],
'options' => array_map(function (Option $option) {
return [
'code' => $option->getCode(),
'name' => $option->getName(),
];
}, $shipment->getOptions()),
'physical_properties' => $shipment->getPhysicalProperties() === null ? null : [
'height' => $shipment->getPhysicalProperties()->getHeight(),
'width' => $shipment->getPhysicalProperties()->getWidth(),
'length' => $shipment->getPhysicalProperties()->getLength(),
'volume' => $shipment->getPhysicalProperties()->getVolume(),
'weight' => $shipment->getPhysicalProperties()->getWeight(),
],
'files' => array_map(function (File $file) {
return [
'resource_type' => $file->getType(),
'mime_type' => $file->getMimeType(),
'extension' => $file->getExtension(),
'data' => $file->getData(),
];
}, $shipment->getFiles()),
'items' => array_map(function (ShipmentItem $item) {
return [
'sku' => $item->getSku(),
'description' => $item->getDescription(),
'quantity' => $item->getQuantity(),
'hs_code' => $item->getHsCode(),
'origin_country_code' => $item->getOriginCountryCode(),
'item_value' => [
'amount' => $item->getItemValueAmount(),
'currency' => $item->getItemValueCurrency(),
],
];
}, $shipment->getItems()),
'customs' => $shipment->getCustoms() === null ? null : [
'content_type' => $shipment->getCustoms()->getContentType(),
'invoice_number' => $shipment->getCustoms()->getInvoiceNumber(),
'non_delivery' => $shipment->getCustoms()->getNonDelivery(),
'incoterm' => $shipment->getCustoms()->getIncoterm(),
],
'myparcelcom_shipment_id' => $shipment->getMyparcelcomShipmentId(),
]);
}
/**
* @param Address $address
* @return array
*/
private function transformAddress(Address $address): array
{
return array_filter([
'street_1' => $address->getStreet1(),
'street_2' => $address->getStreet2(),
'street_number' => $address->getStreetNumber(),
'street_number_suffix' => $address->getStreetNumberSuffix(),
'postal_code' => $address->getPostalCode(),
'city' => $address->getCity(),
'region_code' => $address->getRegionCode(),
'country_code' => $address->getCountryCode(),
'first_name' => $address->getFirstName(),
'last_name' => $address->getLastName(),
'company' => $address->getCompany(),
'email' => $address->getEmail(),
'phone_number' => $address->getPhoneNumber(),
]);
}
/**
* @param Shipment $shipment
* @throws TransformerException
*/
protected function validateModel($shipment): void
{
if (!$shipment instanceof Shipment) {
throw new TransformerException(
'Invalid model supplied, expected instance of `Shipment`, got: ' . get_class($shipment)
);
}
}
}