From c59dfcac93b5b4f2e658e00d2d262ce5dbee34b7 Mon Sep 17 00:00:00 2001 From: Thorry84 Date: Wed, 5 Aug 2020 15:11:41 +0200 Subject: [PATCH] Add support for an additional address line --- README.md | 1 + src/Resources/Address.php | 10 ++++++++++ tests/Unit/Resources/AddressTest.php | 11 +++++++++++ 3 files changed, 22 insertions(+) diff --git a/README.md b/README.md index 95625c4..8ffc04d 100755 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ $parcel = new \Mvdnbrk\DhlParcel\Resources\Parcel([ 'sender' => [ 'company_name' => 'Your Company Name', 'street' => 'Pakketstraat', + 'additional_address_line' => 'Industrie 9999', 'number' => '99', 'postal_code' => '9999AA', 'city' => 'Amsterdam', diff --git a/src/Resources/Address.php b/src/Resources/Address.php index 930c0ec..614f71a 100644 --- a/src/Resources/Address.php +++ b/src/Resources/Address.php @@ -11,6 +11,11 @@ class Address extends BaseResource */ public $street; + /** + * @var string + */ + public $additional_address_line; + /** * @var int|string */ @@ -120,6 +125,11 @@ public function toArray() ->put('addition', $this->number_suffix) ->forget('number_suffix'); }) + ->when(! empty($this->additional_address_line), function ($collection) { + return $collection + ->put('additionalAddressLine', $this->additional_address_line) + ->forget('additional_address_line'); + }) ->put('postalCode', $this->postal_code) ->put('countryCode', $this->cc) ->forget('postal_code') diff --git a/tests/Unit/Resources/AddressTest.php b/tests/Unit/Resources/AddressTest.php index 3e4e976..83c4ca5 100644 --- a/tests/Unit/Resources/AddressTest.php +++ b/tests/Unit/Resources/AddressTest.php @@ -11,6 +11,7 @@ private function validParams($overrides = []) { return array_merge([ 'street' => 'Poststraat', + 'additional_address_line' => 'Industrie 9999', 'number' => '1', 'number_suffix' => 'A', 'postal_code' => '1234AA', @@ -25,6 +26,7 @@ public function creating_a_valid_address_resource() { $address = new Address([ 'street' => 'Poststraat', + 'additional_address_line' => 'Industrie 9999', 'number' => '1', 'number_suffix' => 'A', 'postal_code' => '1234AA', @@ -34,6 +36,7 @@ public function creating_a_valid_address_resource() ]); $this->assertEquals('Poststraat', $address->street); + $this->assertEquals('Industrie 9999', $address->additional_address_line); $this->assertEquals('1', $address->number); $this->assertEquals('A', $address->number_suffix); $this->assertEquals('1234AA', $address->postal_code); @@ -110,6 +113,7 @@ public function to_array() { $attributes = [ 'street' => 'Poststraat', + 'additional_address_line' => 'Industrie 9999', 'number' => '1', 'number_suffix' => 'A', 'postal_code' => '1234AA', @@ -125,10 +129,12 @@ public function to_array() $this->assertIsArray($array); $this->assertFalse($array['isBusiness']); $this->assertEquals('Poststraat', $array['street']); + $this->assertEquals('Industrie 9999', $array['additionalAddressLine']); $this->assertEquals('1', $array['number']); $this->assertEquals('A', $array['addition']); $this->assertEquals('1234AA', $array['postalCode']); $this->assertEquals('NL', $array['countryCode']); + $this->assertArrayNotHasKey('additional_address_line', $array); $this->assertArrayNotHasKey('number_suffix', $array); $this->assertArrayNotHasKey('postal_code', $array); $this->assertArrayNotHasKey('cc', $array); @@ -137,5 +143,10 @@ public function to_array() $array = $address->toArray(); $this->assertArrayNotHasKey('addition', $array); $this->assertArrayNotHasKey('number_suffix', $array); + + $address->additional_address_line = null; + $array = $address->toArray(); + $this->assertArrayNotHasKey('additionalAddressLine', $array); + $this->assertArrayNotHasKey('additional_address_line', $array); } }