diff --git a/src/Resources/Parcel.php b/src/Resources/Parcel.php index 4209296..1c6e818 100644 --- a/src/Resources/Parcel.php +++ b/src/Resources/Parcel.php @@ -76,11 +76,24 @@ 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. * * @param \Mvdnbrk\DhlParcel\Resources\ShipmentOptions|array $value - * @return void + * @return void */ public function setOptionsAttribute($value): void { @@ -115,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 eb510ec..055d588 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); + } }