From bea87fd404e1fae9e3c5215100f4b483d88319eb Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Wed, 18 Nov 2020 14:57:44 +0100 Subject: [PATCH 01/22] Added insurance --- src/Resources/Parcel.php | 11 +++++++++++ src/Resources/ShipmentOptions.php | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/Resources/Parcel.php b/src/Resources/Parcel.php index 39844b6..b2f768b 100644 --- a/src/Resources/Parcel.php +++ b/src/Resources/Parcel.php @@ -78,6 +78,17 @@ public function signature(): self return $this; } + /** + * Set Insurance for parcel (only applicable for package_type 1) + * @param int $amount + * @return $this + */ + public function insurance(int $amount) + { + $this->options->insurance = $amount; + return $this; + } + /** * @param array|object $value */ diff --git a/src/Resources/ShipmentOptions.php b/src/Resources/ShipmentOptions.php index 61f9cf2..609f1b8 100644 --- a/src/Resources/ShipmentOptions.php +++ b/src/Resources/ShipmentOptions.php @@ -25,6 +25,9 @@ class ShipmentOptions extends BaseResource /** @var bool */ public $signature; + /** @var int */ + public $insurance; + public function __construct(array $attributes = []) { $this->setDefaultOptions(); @@ -54,6 +57,19 @@ public function setDescriptionAttribute(string $value): void $this->label_description = $value; } + /** + * Set Insurance amount. Only applicable for package_type = 1 (package) + * @param int $value + * @return $this + */ + public function setInsuranceAttribute(int $value) + { + if ($this->package_type === 1) { + $this->insurance = $value; + } + return $this; + } + public function toArray(): array { return collect(parent::toArray()) From 76124d6041c896b546993abb67275804722df31e Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Wed, 18 Nov 2020 15:13:27 +0100 Subject: [PATCH 02/22] Set currency for insurance --- src/Resources/Parcel.php | 5 +++-- src/Resources/ShipmentOptions.php | 10 ++++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Resources/Parcel.php b/src/Resources/Parcel.php index b2f768b..0ae45cc 100644 --- a/src/Resources/Parcel.php +++ b/src/Resources/Parcel.php @@ -81,11 +81,12 @@ public function signature(): self /** * Set Insurance for parcel (only applicable for package_type 1) * @param int $amount + * @param string $currency * @return $this */ - public function insurance(int $amount) + public function insurance(int $amount, string $currency = 'EUR') { - $this->options->insurance = $amount; + $this->options->setInsurance($amount, $currency); return $this; } diff --git a/src/Resources/ShipmentOptions.php b/src/Resources/ShipmentOptions.php index 609f1b8..7cacac0 100644 --- a/src/Resources/ShipmentOptions.php +++ b/src/Resources/ShipmentOptions.php @@ -25,7 +25,7 @@ class ShipmentOptions extends BaseResource /** @var bool */ public $signature; - /** @var int */ + /** @var array|null */ public $insurance; public function __construct(array $attributes = []) @@ -62,10 +62,13 @@ public function setDescriptionAttribute(string $value): void * @param int $value * @return $this */ - public function setInsuranceAttribute(int $value) + public function setInsurance(int $value, string $currency = 'EUR') { if ($this->package_type === 1) { - $this->insurance = $value; + $this->insurance = [ + 'amount' => $value, + 'currency' => $currency + ]; } return $this; } @@ -77,7 +80,6 @@ public function toArray(): array if (is_bool($value)) { return (int) $value; } - return $value; }) ->all(); From 0823fd21764f969129ae2a01bdce9f7fda7bff36 Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Wed, 18 Nov 2020 15:13:34 +0100 Subject: [PATCH 03/22] Added unit test for insurance --- tests/Feature/Endpoints/ShipmentsTest.php | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/Feature/Endpoints/ShipmentsTest.php b/tests/Feature/Endpoints/ShipmentsTest.php index 1b86103..7a45f26 100644 --- a/tests/Feature/Endpoints/ShipmentsTest.php +++ b/tests/Feature/Endpoints/ShipmentsTest.php @@ -80,6 +80,49 @@ public function create_a_new_shipment_concept_for_a_parcel() $this->assertTrue($this->cleanUp($shipment)); } + /** @test */ + public function create_a_new_shipment_concept_for_a_parcel_with_insurance() + { + $array = [ + 'reference_identifier' => 'test-123', + 'recipient' => [ + 'company' => 'Test Company B.V.', + 'first_name' => 'John', + 'last_name' => 'Doe', + 'email' => 'john@example.com', + 'phone' => '0101111111', + 'street' => 'Poststraat', + 'number' => '1', + 'number_suffix' => 'A', + 'postal_code' => '1234AA', + 'city' => 'Amsterdam', + 'region' => 'Noord-Holland', + 'cc' => 'NL', + ], + 'options' => [ + 'label_description' => 'Test label description', + 'large_format' => false, + 'only_recipient' => false, + 'package_type' => PackageType::PACKAGE, + 'return' => false, + 'signature' => true, + ], + ]; + + $parcel = new Parcel($array); + $parcel->insurance(500); + $shipment = $this->client->shipments->create($parcel); + + $this->assertInstanceOf(Shipment::class, $shipment); + $this->assertInstanceOf(ShipmentOptions::class, $shipment->options); + $this->assertNotNull($shipment->id); + $this->assertEquals(ShipmentStatus::CONCEPT, $shipment->status); + $this->assertEquals('John', $shipment->recipient->first_name); + $this->assertEquals('Doe', $shipment->recipient->last_name); + + $this->assertTrue($this->cleanUp($shipment)); + } + /** @test */ public function create_a_shipment_with_invalid_data() { From 29bed4572b7ca0739c5076faeeaca346ff0ef1fa Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Wed, 18 Nov 2020 15:14:21 +0100 Subject: [PATCH 04/22] Updated readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2e3949b..e56c230 100644 --- a/README.md +++ b/README.md @@ -104,12 +104,13 @@ $parcel = new \Mvdnbrk\MyParcel\Resources\Parcel([ ]); ``` -Or you may use a method like `signature`, `onlyRecipient`, `returnToSender` and `labelDescription`. +Or you may use a method like `signature`, `onlyRecipient`, `returnToSender`, `insurance` and `labelDescription`. You may call any of these after constructing the parcel. ``` php $parcel->onlyRecipient() ->returnToSender() ->signature() + ->insurance(250) ->labelDescription('Some description.'); ``` From 83db9f0beab9cc23c25fe9afa51ca79242cdabac Mon Sep 17 00:00:00 2001 From: Mark van den Broek Date: Wed, 18 Nov 2020 16:31:46 +0100 Subject: [PATCH 05/22] Formatting --- src/Resources/ShipmentOptions.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Resources/ShipmentOptions.php b/src/Resources/ShipmentOptions.php index f51c428..7c67d65 100644 --- a/src/Resources/ShipmentOptions.php +++ b/src/Resources/ShipmentOptions.php @@ -63,10 +63,11 @@ public function setDescriptionAttribute(string $value): void /** * Set Insurance amount. Only applicable for package_type = 1 (package) - * @param int $value + * @param int $value + * @param string $currency * @return $this */ - public function setInsurance(int $value, string $currency = 'EUR') + public function setInsurance(int $value, string $currency = 'EUR'): self { if ($this->package_type === 1) { $this->insurance = [ @@ -74,6 +75,7 @@ public function setInsurance(int $value, string $currency = 'EUR') 'currency' => $currency ]; } + return $this; } @@ -84,6 +86,7 @@ public function toArray(): array if (is_bool($value)) { return (int) $value; } + return $value; }) ->all(); From 75434e3b5e6a215f759f5ef610b1a382522f7513 Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Wed, 18 Nov 2020 18:27:08 +0100 Subject: [PATCH 06/22] Review changes --- src/Resources/Parcel.php | 5 +++-- tests/Feature/Endpoints/ShipmentsTest.php | 3 ++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Resources/Parcel.php b/src/Resources/Parcel.php index 0ae45cc..44b3ea1 100644 --- a/src/Resources/Parcel.php +++ b/src/Resources/Parcel.php @@ -80,13 +80,14 @@ public function signature(): self /** * Set Insurance for parcel (only applicable for package_type 1) - * @param int $amount + * @param int $amount - in cents * @param string $currency * @return $this */ - public function insurance(int $amount, string $currency = 'EUR') + public function insurance(int $amount, string $currency = 'EUR'): self { $this->options->setInsurance($amount, $currency); + return $this; } diff --git a/tests/Feature/Endpoints/ShipmentsTest.php b/tests/Feature/Endpoints/ShipmentsTest.php index 7a45f26..1dfe1f3 100644 --- a/tests/Feature/Endpoints/ShipmentsTest.php +++ b/tests/Feature/Endpoints/ShipmentsTest.php @@ -119,7 +119,8 @@ public function create_a_new_shipment_concept_for_a_parcel_with_insurance() $this->assertEquals(ShipmentStatus::CONCEPT, $shipment->status); $this->assertEquals('John', $shipment->recipient->first_name); $this->assertEquals('Doe', $shipment->recipient->last_name); - + $this->assertEquals(500, $shipment->options->insurance['amount']); + $this->assertEquals('EUR', $shipment->options->insurance['currency']); $this->assertTrue($this->cleanUp($shipment)); } From b013f0518dda307e1027f3677121b9d7aa04173b Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Wed, 18 Nov 2020 18:28:38 +0100 Subject: [PATCH 07/22] Updated readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index e56c230..3a7402f 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,8 @@ $parcel->onlyRecipient() ->insurance(250) ->labelDescription('Some description.'); ``` +**Note:** The insurance value should be in cents + **Mailbox package** From 072cef88340c5bd52193e1a8d64274e7684e2e98 Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Wed, 18 Nov 2020 20:28:46 +0100 Subject: [PATCH 08/22] Update insurance attribute argument to array, updated default options --- src/Resources/Parcel.php | 18 +++++++++--------- src/Resources/ShipmentOptions.php | 23 +++++++++-------------- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/Resources/Parcel.php b/src/Resources/Parcel.php index 80d8dc4..91617fe 100644 --- a/src/Resources/Parcel.php +++ b/src/Resources/Parcel.php @@ -85,16 +85,16 @@ public function ageCheck(): self return $this; } - /** - * Set Insurance for parcel (only applicable for package_type 1) - * @param int $amount - in cents - * @param string $currency - * @return $this - */ - public function insurance(int $amount, string $currency = 'EUR'): self + public function insurance(int $cents, string $currency = 'EUR'): self { - $this->options->setInsurance($amount, $currency); - + $this->options->setInsuranceAttribute([ + 'amount' => $cents, + 'currency' => $currency + ]); + if ($this->recipient->cc === 'NL') { + $this->onlyRecipient(); + $this->signature(); + } return $this; } diff --git a/src/Resources/ShipmentOptions.php b/src/Resources/ShipmentOptions.php index 5a6ea85..727031b 100644 --- a/src/Resources/ShipmentOptions.php +++ b/src/Resources/ShipmentOptions.php @@ -31,8 +31,8 @@ class ShipmentOptions extends BaseResource /** @var bool */ public $signature; - /** @var array|null */ - public $insurance; + /** @var array */ + public $insurance = []; public function __construct(array $attributes = []) { @@ -50,6 +50,10 @@ public function setDefaultOptions(): self $this->package_type = PackageType::PACKAGE; $this->delivery_type = DeliveryType::STANDARD; $this->only_recipient = false; + $this->insurance = [ + 'amount' => 0, + 'currency' => 'EUR' + ]; return $this; } @@ -64,19 +68,10 @@ public function setDescriptionAttribute(string $value): void $this->label_description = $value; } - /** - * Set Insurance amount. Only applicable for package_type = 1 (package) - * @param int $value - * @param string $currency - * @return $this - */ - public function setInsurance(int $value, string $currency = 'EUR'): self + public function setInsuranceAttribute(array $insurance = []): self { - if ($this->package_type === 1) { - $this->insurance = [ - 'amount' => $value, - 'currency' => $currency - ]; + if ($this->package_type === PackageType::PACKAGE) { + $this->insurance = $insurance; } return $this; From b41f41abdc0fe2f6f12decad140abef955183923 Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Wed, 18 Nov 2020 20:39:56 +0100 Subject: [PATCH 09/22] Set insurance empty array, refactored toArray to remove empty arrays --- src/Resources/ShipmentOptions.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Resources/ShipmentOptions.php b/src/Resources/ShipmentOptions.php index 727031b..ef2d0d6 100644 --- a/src/Resources/ShipmentOptions.php +++ b/src/Resources/ShipmentOptions.php @@ -50,10 +50,7 @@ public function setDefaultOptions(): self $this->package_type = PackageType::PACKAGE; $this->delivery_type = DeliveryType::STANDARD; $this->only_recipient = false; - $this->insurance = [ - 'amount' => 0, - 'currency' => 'EUR' - ]; + $this->insurance = []; return $this; } @@ -87,6 +84,9 @@ public function toArray(): array return $value; }) + ->reject(function ($value) { + return (is_array($value) && !count($value)); + }) ->all(); } } From acef31837da48f92ffb3d90983d468e0cca6f978 Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Thu, 19 Nov 2020 09:43:10 +0100 Subject: [PATCH 10/22] Cast response to array --- src/Endpoints/Shipments.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Endpoints/Shipments.php b/src/Endpoints/Shipments.php index 62c509c..6018c87 100644 --- a/src/Endpoints/Shipments.php +++ b/src/Endpoints/Shipments.php @@ -70,7 +70,11 @@ protected function getShipmentsResource(string $apiMethod, string $message = '') $apiMethod ); - $shipment = collect($response->data->shipments)->first(); + $shipment = collect($response->data->shipments) + ->map(function ($shipment) { + return json_decode(json_encode($shipment), true); + }) + ->first(); if ($shipment === null) { throw new MyParcelException($message); From 20702a46c060a374a3a4871b092fc35bde8e3d8b Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Thu, 19 Nov 2020 09:47:47 +0100 Subject: [PATCH 11/22] Added test for retrieval of shipment with insurance --- tests/Feature/Endpoints/ShipmentsTest.php | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/Feature/Endpoints/ShipmentsTest.php b/tests/Feature/Endpoints/ShipmentsTest.php index 1dfe1f3..b54bac0 100644 --- a/tests/Feature/Endpoints/ShipmentsTest.php +++ b/tests/Feature/Endpoints/ShipmentsTest.php @@ -223,6 +223,31 @@ public function get_a_shipment_by_its_id() $this->assertTrue($this->cleanUp($shipment)); } + /** + * @test + */ + public function get_a_shipment_by_its_id_with_insurance() + { + $array = [ + 'recipient' => $this->validRecipient(), + ]; + + $parcel = new Parcel($array); + $parcel->insurance(25000); + $concept = $this->client->shipments->concept($parcel); + + $shipment = $this->client->shipments->get($concept->id); + + $this->assertInstanceOf(Shipment::class, $shipment); + $this->assertNotNull($shipment->id); + $this->assertNotNull($shipment->created); + $this->assertNotNull($shipment->status); + $this->assertEquals(25000, $shipment->options->insurance['amount']); + $this->assertEquals('EUR', $shipment->options->insurance['currency']); + + $this->assertTrue($this->cleanUp($shipment)); + } + /** @test */ public function getting_a_shipment_with_an_invalid_id_should_throw_an_error() { From 0aa325137abd956abffc39b2b2e273b3a1bf7898 Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Thu, 19 Nov 2020 09:48:06 +0100 Subject: [PATCH 12/22] Updated readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f233b12..68edf03 100644 --- a/README.md +++ b/README.md @@ -110,7 +110,7 @@ You may call any of these methods after constructing the parcel: ``` php $parcel->labelDescription('Your description.') ->ageCheck() - ->insurance(250) + ->insurance(25000) ->onlyRecipient() ->returnToSender() ->signature(); From 573e7ab83c96c766add4c95bb7c026d666af4ddd Mon Sep 17 00:00:00 2001 From: Mark van den Broek Date: Thu, 19 Nov 2020 17:55:16 +0100 Subject: [PATCH 13/22] Formatting --- src/Resources/Parcel.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Resources/Parcel.php b/src/Resources/Parcel.php index 91617fe..9ca34b7 100644 --- a/src/Resources/Parcel.php +++ b/src/Resources/Parcel.php @@ -91,10 +91,12 @@ public function insurance(int $cents, string $currency = 'EUR'): self 'amount' => $cents, 'currency' => $currency ]); + if ($this->recipient->cc === 'NL') { $this->onlyRecipient(); $this->signature(); } + return $this; } From 0d2740d509bdf44f0268561718be682d6f1dc8f4 Mon Sep 17 00:00:00 2001 From: Mark van den Broek Date: Thu, 19 Nov 2020 17:59:30 +0100 Subject: [PATCH 14/22] Change `reject` method --- src/Resources/ShipmentOptions.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Resources/ShipmentOptions.php b/src/Resources/ShipmentOptions.php index ef2d0d6..c9b79ef 100644 --- a/src/Resources/ShipmentOptions.php +++ b/src/Resources/ShipmentOptions.php @@ -77,6 +77,9 @@ public function setInsuranceAttribute(array $insurance = []): self public function toArray(): array { return collect(parent::toArray()) + ->reject(function ($value, $key) { + return $key === 'insurance' && empty($value); + }) ->map(function ($value) { if (is_bool($value)) { return (int) $value; @@ -84,9 +87,6 @@ public function toArray(): array return $value; }) - ->reject(function ($value) { - return (is_array($value) && !count($value)); - }) ->all(); } } From a5f2b5c9e13d05f59c8f072127f879cb2f349605 Mon Sep 17 00:00:00 2001 From: Mark van den Broek Date: Thu, 19 Nov 2020 18:07:11 +0100 Subject: [PATCH 15/22] Formatting --- src/Resources/ShipmentOptions.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Resources/ShipmentOptions.php b/src/Resources/ShipmentOptions.php index c9b79ef..b896912 100644 --- a/src/Resources/ShipmentOptions.php +++ b/src/Resources/ShipmentOptions.php @@ -13,6 +13,9 @@ class ShipmentOptions extends BaseResource /** @var int */ public $delivery_type; + /** @var array */ + public $insurance = []; + /** @var string */ public $label_description; @@ -31,9 +34,6 @@ class ShipmentOptions extends BaseResource /** @var bool */ public $signature; - /** @var array */ - public $insurance = []; - public function __construct(array $attributes = []) { $this->setDefaultOptions(); @@ -44,13 +44,13 @@ public function __construct(array $attributes = []) public function setDefaultOptions(): self { $this->age_check = false; + $this->insurance = []; $this->return = false; $this->signature = false; $this->large_format = false; + $this->only_recipient = false; $this->package_type = PackageType::PACKAGE; $this->delivery_type = DeliveryType::STANDARD; - $this->only_recipient = false; - $this->insurance = []; return $this; } From 477c12a6dc912063bac0e5ce07c8467f6f853619 Mon Sep 17 00:00:00 2001 From: Mark van den Broek Date: Fri, 20 Nov 2020 11:55:42 +0100 Subject: [PATCH 16/22] Formatting --- tests/Feature/Endpoints/ShipmentsTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/Feature/Endpoints/ShipmentsTest.php b/tests/Feature/Endpoints/ShipmentsTest.php index b54bac0..c4592cb 100644 --- a/tests/Feature/Endpoints/ShipmentsTest.php +++ b/tests/Feature/Endpoints/ShipmentsTest.php @@ -223,9 +223,7 @@ public function get_a_shipment_by_its_id() $this->assertTrue($this->cleanUp($shipment)); } - /** - * @test - */ + /** @test */ public function get_a_shipment_by_its_id_with_insurance() { $array = [ From 82513d36f6585d648538cd6be919c61cce751c7e Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Mon, 23 Nov 2020 09:04:06 +0100 Subject: [PATCH 17/22] Added money resource --- src/Resources/Money.php | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/Resources/Money.php diff --git a/src/Resources/Money.php b/src/Resources/Money.php new file mode 100644 index 0000000..cf16de2 --- /dev/null +++ b/src/Resources/Money.php @@ -0,0 +1,27 @@ +amount = $value; + } + + public function setCurrencyAttribute(string $value): void + { + $this->currency = $value; + } +} From fa3eecacd88d201cc80ed46dfb9d1571aba56002 Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Mon, 23 Nov 2020 09:04:21 +0100 Subject: [PATCH 18/22] Updated insurance related tests --- tests/Feature/Endpoints/ShipmentsTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Feature/Endpoints/ShipmentsTest.php b/tests/Feature/Endpoints/ShipmentsTest.php index b54bac0..a1cdb86 100644 --- a/tests/Feature/Endpoints/ShipmentsTest.php +++ b/tests/Feature/Endpoints/ShipmentsTest.php @@ -119,8 +119,8 @@ public function create_a_new_shipment_concept_for_a_parcel_with_insurance() $this->assertEquals(ShipmentStatus::CONCEPT, $shipment->status); $this->assertEquals('John', $shipment->recipient->first_name); $this->assertEquals('Doe', $shipment->recipient->last_name); - $this->assertEquals(500, $shipment->options->insurance['amount']); - $this->assertEquals('EUR', $shipment->options->insurance['currency']); + $this->assertEquals(500, $shipment->options->insurance->amount); + $this->assertEquals('EUR', $shipment->options->insurance->currency); $this->assertTrue($this->cleanUp($shipment)); } @@ -242,8 +242,8 @@ public function get_a_shipment_by_its_id_with_insurance() $this->assertNotNull($shipment->id); $this->assertNotNull($shipment->created); $this->assertNotNull($shipment->status); - $this->assertEquals(25000, $shipment->options->insurance['amount']); - $this->assertEquals('EUR', $shipment->options->insurance['currency']); + $this->assertEquals(25000, $shipment->options->insurance->amount); + $this->assertEquals('EUR', $shipment->options->insurance->currency); $this->assertTrue($this->cleanUp($shipment)); } From ef72358be2fa3194a5a41c94c4a7769b862528f0 Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Mon, 23 Nov 2020 09:08:42 +0100 Subject: [PATCH 19/22] Restred Shipments endpoint --- src/Endpoints/Shipments.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Endpoints/Shipments.php b/src/Endpoints/Shipments.php index 6018c87..62c509c 100644 --- a/src/Endpoints/Shipments.php +++ b/src/Endpoints/Shipments.php @@ -70,11 +70,7 @@ protected function getShipmentsResource(string $apiMethod, string $message = '') $apiMethod ); - $shipment = collect($response->data->shipments) - ->map(function ($shipment) { - return json_decode(json_encode($shipment), true); - }) - ->first(); + $shipment = collect($response->data->shipments)->first(); if ($shipment === null) { throw new MyParcelException($message); From 1aac64ec0cd4639a896ca57a96744d298c66ad0d Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Mon, 23 Nov 2020 09:08:59 +0100 Subject: [PATCH 20/22] Updated insurance for shippingOptions --- src/Resources/ShipmentOptions.php | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/Resources/ShipmentOptions.php b/src/Resources/ShipmentOptions.php index ef2d0d6..4957a2f 100644 --- a/src/Resources/ShipmentOptions.php +++ b/src/Resources/ShipmentOptions.php @@ -31,8 +31,8 @@ class ShipmentOptions extends BaseResource /** @var bool */ public $signature; - /** @var array */ - public $insurance = []; + /** @var Money|null */ + public $insurance; public function __construct(array $attributes = []) { @@ -50,7 +50,7 @@ public function setDefaultOptions(): self $this->package_type = PackageType::PACKAGE; $this->delivery_type = DeliveryType::STANDARD; $this->only_recipient = false; - $this->insurance = []; + $this->insurance = null; return $this; } @@ -65,13 +65,16 @@ public function setDescriptionAttribute(string $value): void $this->label_description = $value; } - public function setInsuranceAttribute(array $insurance = []): self + public function setInsuranceAttribute($value): void { - if ($this->package_type === PackageType::PACKAGE) { - $this->insurance = $insurance; + if ($value instanceof Money) { + $this->insurance = $value; + } + if (is_null($this->insurance)) { + $this->insurance = new Money($value); + } else { + $this->insurance->fill($value); } - - return $this; } public function toArray(): array @@ -81,12 +84,8 @@ public function toArray(): array if (is_bool($value)) { return (int) $value; } - return $value; }) - ->reject(function ($value) { - return (is_array($value) && !count($value)); - }) ->all(); } } From fc563015ec7f23469c58234342c8b58a09f0f7a4 Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Mon, 23 Nov 2020 09:10:39 +0100 Subject: [PATCH 21/22] Updated method 'insurance' for parcel resource --- src/Resources/Parcel.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resources/Parcel.php b/src/Resources/Parcel.php index 91617fe..b9d2d58 100644 --- a/src/Resources/Parcel.php +++ b/src/Resources/Parcel.php @@ -87,7 +87,7 @@ public function ageCheck(): self public function insurance(int $cents, string $currency = 'EUR'): self { - $this->options->setInsuranceAttribute([ + $this->options->insurance = new Money([ 'amount' => $cents, 'currency' => $currency ]); From 06ed09c1e4b7c92574b0563ce3c100c31fede421 Mon Sep 17 00:00:00 2001 From: Sebastiaan Date: Mon, 23 Nov 2020 09:25:25 +0100 Subject: [PATCH 22/22] Fix wrong type for insurance in defaultOptions --- src/Resources/ShipmentOptions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resources/ShipmentOptions.php b/src/Resources/ShipmentOptions.php index f9a3c80..8392722 100644 --- a/src/Resources/ShipmentOptions.php +++ b/src/Resources/ShipmentOptions.php @@ -44,7 +44,7 @@ public function __construct(array $attributes = []) public function setDefaultOptions(): self { $this->age_check = false; - $this->insurance = []; + $this->insurance = null; $this->return = false; $this->signature = false; $this->large_format = false;