Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
24 changes: 20 additions & 4 deletions src/Endpoints/ShipmentLabels.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class ShipmentLabels extends BaseEndpoint
{
const SEPARATOR = ";";

/** @var \Mvdnbrk\MyParcel\Resources\Label */
protected $label;

Expand All @@ -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',
Expand All @@ -36,7 +36,6 @@ public function get($value): string
null,
['Accept' => 'application/pdf']
);

return $response;
}

Expand All @@ -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;
}
}
}
27 changes: 27 additions & 0 deletions tests/Feature/Endpoints/ShipmentLabelsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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()
{
Expand Down