From 1410a89f2e87eb02464c33c7e9ec1fbbc9886463 Mon Sep 17 00:00:00 2001 From: Martin Boer Date: Fri, 11 Dec 2020 11:22:57 +0100 Subject: [PATCH 1/2] :sparkles: label mime types for carriers and shipments --- src/Resources/Carrier.php | 21 +++++++++++++++++++ src/Resources/Interfaces/FileInterface.php | 1 + src/Resources/Proxy/CarrierProxy.php | 19 +++++++++++++++++ src/Resources/Proxy/ShipmentProxy.php | 8 +++++++ src/Resources/Shipment.php | 14 ++++++++++++- tests/Feature/MyParcelComApiTest.php | 1 + ...-eef00b32-177e-43d3-9b26-715365e4ce46.json | 5 ++++- .../page-number--1/page-size--30.json | 10 +++++++-- tests/Unit/ShipmentTest.php | 12 +++++++++++ 9 files changed, 87 insertions(+), 4 deletions(-) diff --git a/src/Resources/Carrier.php b/src/Resources/Carrier.php index f4983a33..943b516c 100644 --- a/src/Resources/Carrier.php +++ b/src/Resources/Carrier.php @@ -15,6 +15,7 @@ class Carrier implements CarrierInterface const ATTRIBUTE_NAME = 'name'; const ATTRIBUTE_CODE = 'code'; const ATTRIBUTE_CREDENTIALS_FORMAT = 'credentials_format'; + const ATTRIBUTE_LABEL_MIME_TYPES = 'label_mime_types'; /** @var string */ private $id; @@ -27,6 +28,7 @@ class Carrier implements CarrierInterface self::ATTRIBUTE_NAME => null, self::ATTRIBUTE_CODE => null, self::ATTRIBUTE_CREDENTIALS_FORMAT => [], + self::ATTRIBUTE_LABEL_MIME_TYPES => [], ]; /** @@ -113,6 +115,25 @@ public function getCredentialsFormat() return $this->attributes[self::ATTRIBUTE_CREDENTIALS_FORMAT]; } + /** + * @param array $labelMimeTypes + * @return $this + */ + public function setLabelMimeTypes(array $labelMimeTypes) + { + $this->attributes[self::ATTRIBUTE_LABEL_MIME_TYPES] = $labelMimeTypes; + + return $this; + } + + /** + * @return string + */ + public function getLabelMimeTypes() + { + return $this->attributes[self::ATTRIBUTE_LABEL_MIME_TYPES]; + } + /** * This function puts all object properties in an array and returns it. * diff --git a/src/Resources/Interfaces/FileInterface.php b/src/Resources/Interfaces/FileInterface.php index 27964cc5..414fa057 100644 --- a/src/Resources/Interfaces/FileInterface.php +++ b/src/Resources/Interfaces/FileInterface.php @@ -19,6 +19,7 @@ interface FileInterface extends ResourceInterface const MIME_TYPE_PNG = 'image/png'; const MIME_TYPE_JPG = 'image/jpeg'; const MIME_TYPE_PDF = 'application/pdf'; + const MIME_TYPE_ZPL = 'application/zpl'; const FORMAT_MIME_TYPE = 'mime_type'; const FORMAT_EXTENSION = 'extension'; diff --git a/src/Resources/Proxy/CarrierProxy.php b/src/Resources/Proxy/CarrierProxy.php index d3cd8127..47c62d5a 100644 --- a/src/Resources/Proxy/CarrierProxy.php +++ b/src/Resources/Proxy/CarrierProxy.php @@ -105,6 +105,25 @@ public function getCredentialsFormat() return $this->getResource()->getCredentialsFormat(); } + /** + * @param array $labelMimeTypes + * @return $this + */ + public function setLabelMimeTypes(array $labelMimeTypes) + { + $this->getResource()->setLabelMimeTypes($labelMimeTypes); + + return $this; + } + + /** + * @return array + */ + public function getLabelMimeTypes() + { + return $this->getResource()->getLabelMimeTypes(); + } + /** * This function puts all object properties in an array and returns it. * diff --git a/src/Resources/Proxy/ShipmentProxy.php b/src/Resources/Proxy/ShipmentProxy.php index a0b78e4f..74b11575 100644 --- a/src/Resources/Proxy/ShipmentProxy.php +++ b/src/Resources/Proxy/ShipmentProxy.php @@ -711,6 +711,14 @@ public function clearTags() return $this->getResource()->clearTags(); } + /** + * {@inheritDoc} + */ + public function setLabelMimeType($labelMimeType) + { + return $this->getResource()->setLabelMimeType($labelMimeType); + } + /** * This function puts all object properties in an array and returns it. * diff --git a/src/Resources/Shipment.php b/src/Resources/Shipment.php index 51d4eba6..ec6c19de 100644 --- a/src/Resources/Shipment.php +++ b/src/Resources/Shipment.php @@ -118,7 +118,7 @@ class Shipment implements ShipmentInterface /** @var array */ private $meta = [ - self::META_LABEL_MIME_TYPE => 'application/pdf', + self::META_LABEL_MIME_TYPE => FileInterface::MIME_TYPE_PDF, self::META_SERVICE_CODE => null, ]; @@ -834,4 +834,16 @@ public function clearTags() return $this; } + + /** + * Supported values are FileInterface::MIME_TYPE_PDF or FileInterface::MIME_TYPE_ZPL + * @param $labelMimeType + * @return $this + */ + public function setLabelMimeType($labelMimeType) + { + $this->meta[self::META_LABEL_MIME_TYPE] = $labelMimeType; + + return $this; + } } diff --git a/tests/Feature/MyParcelComApiTest.php b/tests/Feature/MyParcelComApiTest.php index a66bf9c3..062ce668 100644 --- a/tests/Feature/MyParcelComApiTest.php +++ b/tests/Feature/MyParcelComApiTest.php @@ -219,6 +219,7 @@ public function testGetCarriers() $this->assertNotEmpty($carrier->getName()); $this->assertNotEmpty($carrier->getCode()); $this->assertNotEmpty($carrier->getCredentialsFormat()); + $this->assertNotEmpty($carrier->getLabelMimeTypes()); } } diff --git a/tests/Stubs/get/https---api-carriers-eef00b32-177e-43d3-9b26-715365e4ce46.json b/tests/Stubs/get/https---api-carriers-eef00b32-177e-43d3-9b26-715365e4ce46.json index b0448509..2ca90a9a 100644 --- a/tests/Stubs/get/https---api-carriers-eef00b32-177e-43d3-9b26-715365e4ce46.json +++ b/tests/Stubs/get/https---api-carriers-eef00b32-177e-43d3-9b26-715365e4ce46.json @@ -19,7 +19,10 @@ "type": "string" } } - } + }, + "label_mime_types": [ + "application/pdf" + ] }, "links": { "self": "https://api/carriers/eef00b32-177e-43d3-9b26-715365e4ce46" diff --git a/tests/Stubs/get/https---api-carriers/page-number--1/page-size--30.json b/tests/Stubs/get/https---api-carriers/page-number--1/page-size--30.json index 2fef4800..d022ab34 100644 --- a/tests/Stubs/get/https---api-carriers/page-number--1/page-size--30.json +++ b/tests/Stubs/get/https---api-carriers/page-number--1/page-size--30.json @@ -20,7 +20,10 @@ "type": "string" } } - } + }, + "label_mime_types": [ + "application/pdf" + ] }, "links": { "self": "https://api/carriers/eef00b32-177e-43d3-9b26-715365e4ce46" @@ -46,7 +49,10 @@ "type": "string" } } - } + }, + "label_mime_types": [ + "application/pdf" + ] }, "links": { "self": "https://api/carriers/4a78637a-5d81-4e71-9b18-c338968f72fa" diff --git a/tests/Unit/ShipmentTest.php b/tests/Unit/ShipmentTest.php index 74215257..5c511ada 100644 --- a/tests/Unit/ShipmentTest.php +++ b/tests/Unit/ShipmentTest.php @@ -440,6 +440,18 @@ public function testTags() $this->assertNull($shipment->getTags()); } + /** @test */ + public function testLabelMimeType() + { + $shipment = new Shipment(); + + $this->assertEquals(FileInterface::MIME_TYPE_PDF, $shipment->jsonSerialize()['meta']['label_mime_type']); + + $shipment->setLabelMimeType(FileInterface::MIME_TYPE_ZPL); + + $this->assertEquals(FileInterface::MIME_TYPE_ZPL, $shipment->jsonSerialize()['meta']['label_mime_type']); + } + /** @test */ public function testJsonSerialize() { From dfd4d3ef24b2689b22b1663bbd7b27ef405950ba Mon Sep 17 00:00:00 2001 From: Martin Boer Date: Tue, 15 Dec 2020 12:02:20 +0100 Subject: [PATCH 2/2] :bug: shipment meta should be outside of data --- src/MyParcelComApi.php | 22 ++++++++++++++-------- src/Resources/Proxy/ShipmentProxy.php | 8 ++++++++ src/Resources/Shipment.php | 8 ++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/MyParcelComApi.php b/src/MyParcelComApi.php index 86f2302d..b5141748 100644 --- a/src/MyParcelComApi.php +++ b/src/MyParcelComApi.php @@ -473,7 +473,7 @@ public function createShipment(ShipmentInterface $shipment) throw $exception; } - return $this->postResource($shipment); + return $this->postResource($shipment, $shipment->getMeta()); } /** @@ -492,7 +492,7 @@ public function updateShipment(ShipmentInterface $shipment) throw $exception; } - return $this->patchResource($shipment); + return $this->patchResource($shipment, $shipment->getMeta()); } /** @@ -764,24 +764,26 @@ public function getResourcesFromUri($uri) * Patch given resource and return the resource that was returned by the request. * * @param ResourceInterface $resource + * @param array $meta * @return ResourceInterface|null * @throws RequestException */ - protected function patchResource(ResourceInterface $resource) + protected function patchResource(ResourceInterface $resource, $meta = []) { - return $this->sendResource($resource, 'patch'); + return $this->sendResource($resource, 'patch', $meta); } /** * Post given resource and return the resource that was returned by the request. * * @param ResourceInterface $resource + * @param array $meta * @return ResourceInterface|null * @throws RequestException */ - protected function postResource(ResourceInterface $resource) + protected function postResource(ResourceInterface $resource, $meta = []) { - return $this->sendResource($resource); + return $this->sendResource($resource, 'post', $meta); } /** @@ -789,15 +791,19 @@ protected function postResource(ResourceInterface $resource) * * @param ResourceInterface $resource * @param string $method + * @param array $meta * @return ResourceInterface|null * @throws RequestException */ - protected function sendResource(ResourceInterface $resource, $method = 'post') + protected function sendResource(ResourceInterface $resource, $method = 'post', $meta = []) { $response = $this->doRequest( $this->getResourceUri($resource->getType(), $resource->getId()), $method, - ['data' => $resource], + array_filter([ + 'data' => $resource, + 'meta' => array_filter($meta), + ]), $this->authenticator->getAuthorizationHeader() + [ AuthenticatorInterface::HEADER_ACCEPT => AuthenticatorInterface::MIME_TYPE_JSONAPI, ] diff --git a/src/Resources/Proxy/ShipmentProxy.php b/src/Resources/Proxy/ShipmentProxy.php index 74b11575..7dc54307 100644 --- a/src/Resources/Proxy/ShipmentProxy.php +++ b/src/Resources/Proxy/ShipmentProxy.php @@ -59,6 +59,14 @@ public function getType() return $this->type; } + /** + * @return array + */ + public function getMeta() + { + return $this->getResource()->getMeta(); + } + /** * @param AddressInterface $recipientAddress * @return $this diff --git a/src/Resources/Shipment.php b/src/Resources/Shipment.php index ec6c19de..c070459a 100644 --- a/src/Resources/Shipment.php +++ b/src/Resources/Shipment.php @@ -148,6 +148,14 @@ public function getType() return $this->type; } + /** + * @return array + */ + public function getMeta() + { + return $this->meta; + } + /** * {@inheritdoc} */