From e313a9f867f504f1c6f2ade1cd60898b11373a54 Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Tue, 24 Nov 2020 11:13:19 +0100 Subject: [PATCH 1/3] Prep shipment label request to allow multiple labels request --- src/Endpoints/ShipmentLabels.php | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/src/Endpoints/ShipmentLabels.php b/src/Endpoints/ShipmentLabels.php index 5ac9e7e..de02b88 100644 --- a/src/Endpoints/ShipmentLabels.php +++ b/src/Endpoints/ShipmentLabels.php @@ -8,6 +8,8 @@ class ShipmentLabels extends BaseEndpoint { + const SEPARATOR = ";"; + /** @var \Mvdnbrk\MyParcel\Resources\Label */ protected $label; @@ -24,9 +26,7 @@ public function boot(): void */ public function get($value): string { - if ($value instanceof Shipment) { - $value = $value->id; - } + $value = $this->prepLabelRequest($value); $response = $this->performApiCall( 'GET', @@ -36,7 +36,6 @@ public function get($value): string null, ['Accept' => 'application/pdf'] ); - return $response; } @@ -53,4 +52,21 @@ public function setLabel(Label $label): self return $this; } + + private function prepLabelRequest($value): string + { + if (is_array($value)) { + $shipmentIds = []; + foreach ($value as $val) { + $shipmentIds[] = $this->prepLabelRequest($val); + } + return implode(static::SEPARATOR, $shipmentIds); + } + + if ($value instanceof Shipment) { + return $value->id; + } else { + return $value; + } + } } From cae96088a7b6d99a4887a890ac177f6b9ae6fc8b Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Tue, 24 Nov 2020 11:13:26 +0100 Subject: [PATCH 2/3] Updated labels unit test --- .../Feature/Endpoints/ShipmentLabelsTest.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/tests/Feature/Endpoints/ShipmentLabelsTest.php b/tests/Feature/Endpoints/ShipmentLabelsTest.php index 669f18b..602eac6 100644 --- a/tests/Feature/Endpoints/ShipmentLabelsTest.php +++ b/tests/Feature/Endpoints/ShipmentLabelsTest.php @@ -42,6 +42,19 @@ public function get_a_label_in_A6_size_by_shipment_id() $this->assertIsString('string', $pdf); } + /** @test */ + public function get_a_label_in_A6_size_by_multiple_shipment_id() + { + // create 2nd shipment + $parcel = new Parcel([ + 'recipient' => $this->validRecipient(), + 'reference_identifier' => 'shipment-2' + ]); + $shipment = $this->client->shipments->concept($parcel); + $pdf = $this->client->labels->get([$this->shipment->id, $shipment->id]); + $this->assertIsString('string', $pdf); + } + /** @test */ public function get_a_label_in_A6_size_by_shipment_object() { @@ -50,6 +63,20 @@ public function get_a_label_in_A6_size_by_shipment_object() $this->assertIsString('string', $pdf); } + /** @test */ + public function get_a_label_in_A6_size_by_multiple_shipment_object() + { + // create 2nd shipment + $parcel = new Parcel([ + 'recipient' => $this->validRecipient(), + 'reference_identifier' => 'shipment-2' + ]); + $shipment = $this->client->shipments->concept($parcel); + $pdf = $this->client->labels->get([$this->shipment, $shipment]); + + $this->assertIsString('string', $pdf); + } + /** @test */ public function it_can_set_a_label() { From bafc43f8f6461d4a48c452ae822e7876c0995fc6 Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Tue, 24 Nov 2020 11:20:21 +0100 Subject: [PATCH 3/3] Updated readme --- README.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/README.md b/README.md index af0f21b..b946539 100644 --- a/README.md +++ b/README.md @@ -80,12 +80,23 @@ Or you may pass the `Shipment` instance directly to this method: $myparcel->labels->get($shipment); ``` +Or you may pass an `array` with shipment ids or instances to retrieve multiple labels. \ +**note:** the labels will be merged to a single pdf. +```php +$myparcel->labels->get([ + $shipment->id, + $shipmentInstance +]); +``` + + The label format is A6 by default, you may change this by calling the `setFormatA4` method: ```php $myparcel->labels->setFormatA4()->get($shipment); ``` + ### Setting delivery options for a parcel You can set delivery options for a parcel by passing in the options directly when you create a parcel: