From 9ee74184d59b3c432169642683b37fd865ac8424 Mon Sep 17 00:00:00 2001 From: Willem Turkstra Date: Wed, 10 May 2023 13:28:34 +0200 Subject: [PATCH 1/2] Added INS option to ShipmentOptions --- src/Resources/Parcel.php | 13 ++++++++++++ src/Resources/ShipmentOptions.php | 20 ++++++++++++++++++ tests/Unit/Resources/ShipmentOptionsTest.php | 22 ++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/src/Resources/Parcel.php b/src/Resources/Parcel.php index 4209296..e44c065 100644 --- a/src/Resources/Parcel.php +++ b/src/Resources/Parcel.php @@ -76,6 +76,19 @@ public function signature(): self return $this; } + /** + * Set the amount for "Insured" in EUR. + * + * @param int|float $value + * @return $this + */ + public function setInsured($value): self + { + $this->options->setInsured($value); + + return $this; + } + /** * Set the shipment options for this parcel. * diff --git a/src/Resources/ShipmentOptions.php b/src/Resources/ShipmentOptions.php index eb510ec..59a3738 100644 --- a/src/Resources/ShipmentOptions.php +++ b/src/Resources/ShipmentOptions.php @@ -30,6 +30,9 @@ class ShipmentOptions extends BaseResource /** @var bool */ public $signature; + /** @var int|float */ + protected $insured; + public function __construct(array $attributes = []) { $this->setDefaultOptions(); @@ -90,6 +93,17 @@ public function setCashOnDelivery($value): void $this->cash_on_delivery = $value; } + /** + * Set the amount for the option "INS" in EUR. + * + * @param int|float $value + * @return void + */ + public function setInsured($value): void + { + $this->insured = $value; + } + public function toArray(): array { return collect() @@ -136,6 +150,12 @@ public function toArray(): array 'key' => 'EVE', ]); }) + ->when($this->insured, function($collection) { + return $collection->push([ + 'key' => 'INS', + 'input' => $this->insured, + ]); + }) ->all(); } } diff --git a/tests/Unit/Resources/ShipmentOptionsTest.php b/tests/Unit/Resources/ShipmentOptionsTest.php index cd295c1..7458f0f 100644 --- a/tests/Unit/Resources/ShipmentOptionsTest.php +++ b/tests/Unit/Resources/ShipmentOptionsTest.php @@ -201,4 +201,26 @@ public function to_array_with_cash_on_delivery() ], ], $array); } + + /** @test */ + public function to_array_with_insured() + { + $options = new ShipmentOptions([ + 'insured' => 150, + ]); + + $array = $options->toArray(); + + $this->assertIsArray($array); + + $this->assertEquals([ + [ + 'key' => 'DOOR', + ], + [ + 'key' => 'INS', + 'input' => 150, + ], + ], $array); + } } From 8fc9c14f07fbb856e97907ec05cfe5d7e55c0f89 Mon Sep 17 00:00:00 2001 From: Willem Turkstra Date: Wed, 10 May 2023 13:33:53 +0200 Subject: [PATCH 2/2] Apply codestyle --- src/Resources/Parcel.php | 6 +++--- src/Resources/ShipmentOptions.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Resources/Parcel.php b/src/Resources/Parcel.php index e44c065..1c6e818 100644 --- a/src/Resources/Parcel.php +++ b/src/Resources/Parcel.php @@ -80,7 +80,7 @@ public function signature(): self * Set the amount for "Insured" in EUR. * * @param int|float $value - * @return $this + * @return $this */ public function setInsured($value): self { @@ -93,7 +93,7 @@ public function setInsured($value): self * Set the shipment options for this parcel. * * @param \Mvdnbrk\DhlParcel\Resources\ShipmentOptions|array $value - * @return void + * @return void */ public function setOptionsAttribute($value): void { @@ -128,7 +128,7 @@ public function servicePoint(string $value): self * Set the amount for "Cash On Delivery" in EUR. * * @param int|float $value - * @return $this + * @return $this */ public function cashOnDelivery($value): self { diff --git a/src/Resources/ShipmentOptions.php b/src/Resources/ShipmentOptions.php index 59a3738..055d588 100644 --- a/src/Resources/ShipmentOptions.php +++ b/src/Resources/ShipmentOptions.php @@ -150,7 +150,7 @@ public function toArray(): array 'key' => 'EVE', ]); }) - ->when($this->insured, function($collection) { + ->when($this->insured, function ($collection) { return $collection->push([ 'key' => 'INS', 'input' => $this->insured,