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: 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; + } + } } 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() {