diff --git a/src/Client.php b/src/Client.php index 4649fad..5e5de94 100644 --- a/src/Client.php +++ b/src/Client.php @@ -554,9 +554,9 @@ protected function getParcelData( $parcelData = array_merge($parcelData, [ 'name' => $shippingAddress->getName(), 'company_name' => $shippingAddress->getCompanyName() ?? '', - 'address' => $shippingAddress->getStreet(), + 'address' => $shippingAddress->getAddressLine1(), 'address_2' => $shippingAddress->getAddressLine2() ?? '', - 'house_number' => $shippingAddress->getHouseNumber(), + 'house_number' => $shippingAddress->getHouseNumber() ?? '', 'city' => $shippingAddress->getCity(), 'postal_code' => $shippingAddress->getPostalCode(), 'country' => $shippingAddress->getCountryCode(), @@ -643,9 +643,9 @@ protected function getParcelData( $parcelData = array_merge($parcelData, [ 'from_name' => $senderAddress->getName(), 'from_company_name' => $senderAddress->getCompanyName() ?? '', - 'from_address_1' => $senderAddress->getStreet(), + 'from_address_1' => $senderAddress->getAddressLine1(), 'from_address_2' => $senderAddress->getAddressLine2() ?? '', - 'from_house_number' => $senderAddress->getHouseNumber(), + 'from_house_number' => $senderAddress->getHouseNumber() ?? '', 'from_city' => $senderAddress->getCity(), 'from_postal_code' => $senderAddress->getPostalCode(), 'from_country' => $senderAddress->getCountryCode(), diff --git a/src/Model/Address.php b/src/Model/Address.php index 0927dde..7f7235b 100644 --- a/src/Model/Address.php +++ b/src/Model/Address.php @@ -4,32 +4,43 @@ class Address { + /** Street parsed from address by Sendcloud. */ + protected ?string $street = null; + public static function fromParcelData(array $data): self { - return new self( + $address = new self( (string)$data['name'], (string)$data['company_name'], - (string)$data['address_divided']['street'], - (string)$data['address_divided']['house_number'], + (string)$data['address'], (string)$data['city'], (string)$data['postal_code'], (string)$data['country']['iso_2'], (string)$data['email'], + (string)$data['address_divided']['house_number'], ((string)$data['telephone'] ?: null), ((string)$data['address_2'] ?: null), ((string)$data['to_state'] ?: null) ); + + $address->street = (string)$data['address_divided']['street']; + + return $address; } + /** + * @param string $addressLine1 Full address line 1. Includes house number unless explicitly specifying {@see $houseNumber}. + * @param string|null $houseNumber Will be added onto {@see $addressLine1}. Leave out if {@see $addressLine1} already contains a house number. + */ public function __construct( protected string $name, protected ?string $companyName, - protected string $street, - protected string $houseNumber, + protected string $addressLine1, protected string $city, protected string $postalCode, protected string $countryCode, protected string $emailAddress, + protected ?string $houseNumber = null, protected ?string $phoneNumber = null, protected ?string $addressLine2 = null, protected ?string $countryStateCode = null @@ -46,14 +57,9 @@ public function getCompanyName(): ?string return $this->companyName; } - public function getStreet(): string + public function getAddressLine1(): string { - return $this->street; - } - - public function getHouseNumber(): string - { - return $this->houseNumber; + return $this->addressLine1; } public function getCity(): string @@ -76,6 +82,16 @@ public function getEmailAddress(): string return $this->emailAddress; } + public function getStreet(): ?string + { + return $this->street; + } + + public function getHouseNumber(): ?string + { + return $this->houseNumber; + } + public function getPhoneNumber(): ?string { return $this->phoneNumber; @@ -110,11 +126,12 @@ public function toArray(): array 'countryCode' => $this->getCountryCode(), 'displayName' => $this->getDisplayName(), 'emailAddress' => $this->getEmailAddress(), - 'houseNumber' => $this->getHouseNumber(), 'name' => $this->getName(), 'phoneNumber' => $this->getPhoneNumber(), 'postalCode' => $this->getPostalCode(), + 'address' => $this->getAddressLine1(), 'street' => $this->getStreet(), + 'houseNumber' => $this->getHouseNumber(), 'countryStateCode' => $this->getCountryStateCode(), ]; } diff --git a/test/ClientTest.php b/test/ClientTest.php index 4732b3a..552d5c9 100644 --- a/test/ClientTest.php +++ b/test/ClientTest.php @@ -40,7 +40,7 @@ public function testGetUser(): void $this->guzzleClientMock->expects($this->once())->method('request')->willReturn(new Response( 200, [], - '{"user":{"address":"Insulindelaan","city":"Eindhoven","company_logo":null,"company_name":"SendCloud","data":[],"email":"johndoe@sendcloud.nl","invoices":[{"date":"05-06-201811:58:52","id":1,"isPayed":false,"items":"https://local.sendcloud.sc/api/v2/user/invoices/1","price_excl":77.4,"price_incl":93.65,"ref":"1","type":"periodic"}],"modules":[{"activated":true,"id":5,"name":"SendCloudClient","settings":null,"short_name":"sendcloud_client"},{"id":3,"name":"PrestashopIntegration","settings":{"url_webshop":"http://localhost/testing/prestashop","api_key":"O8ALXHMM24QULWM213CC6SGQ5VDJKC8W"},"activated":true,"short_name":"prestashop"}],"postal_code":"5642CV","registered":"2018-05-2912:52:51","telephone":"+31626262626","username":"johndoe"}}' + '{"user":{"address":"Insulindelaan 115","city":"Eindhoven","company_logo":null,"company_name":"SendCloud","data":[],"email":"johndoe@sendcloud.nl","invoices":[{"date":"05-06-201811:58:52","id":1,"isPayed":false,"items":"https://local.sendcloud.sc/api/v2/user/invoices/1","price_excl":77.4,"price_incl":93.65,"ref":"1","type":"periodic"}],"modules":[{"activated":true,"id":5,"name":"SendCloudClient","settings":null,"short_name":"sendcloud_client"},{"id":3,"name":"PrestashopIntegration","settings":{"url_webshop":"http://localhost/testing/prestashop","api_key":"O8ALXHMM24QULWM213CC6SGQ5VDJKC8W"},"activated":true,"short_name":"prestashop"}],"postal_code":"5642CV","registered":"2018-05-2912:52:51","telephone":"+31626262626","username":"johndoe"}}' )); $user = $this->client->getUser(); @@ -48,7 +48,7 @@ public function testGetUser(): void $this->assertEquals('johndoe', $user->getUsername()); $this->assertEquals('SendCloud', $user->getCompanyName()); $this->assertEquals('+31626262626', $user->getPhoneNumber()); - $this->assertEquals('Insulindelaan', $user->getAddress()); + $this->assertEquals('Insulindelaan 115', $user->getAddress()); $this->assertEquals('Eindhoven', $user->getCity()); $this->assertEquals('5642CV', $user->getPostalCode()); $this->assertEquals('johndoe@sendcloud.nl', $user->getEmailAddress()); @@ -276,7 +276,7 @@ public function testCreateParcelCustoms(): void }); $parcel = $this->client->createParcel( - new Address('Dr. Coffee', null, 'Street', '123', 'Place', '7837', 'BM', 'drcoffee@drcoffee.dr', null, 'Unit 83'), + new Address('Dr. Coffee', null, 'Street', 'Place', '7837', 'BM', 'drcoffee@drcoffee.dr', '123', null, 'Unit 83'), null, null, null, @@ -320,11 +320,11 @@ public function testUpdateParcel(): void return new Response( 200, [], - '{"parcel":{"id":8293794,"address":"Rosebud 2134A","address_2":"Above the skies","address_divided":{"street":"Rosebud","house_number":"2134"},"city":"Almanda","company_name":"Some company","country":{"iso_2":"NL","iso_3":"NLD","name":"Netherlands"},"data":{},"date_created":"11-03-2019 14:35:10","email":"completelydifferent@email.com","name":"Completely different person","postal_code":"9238DD","reference":"0","shipment":null,"status":{"id":999,"message":"No label"},"to_service_point":null,"telephone":"+31699999999","tracking_number":"","weight":"2.490","label":{},"customs_declaration":{},"order_number":"201900001","insured_value":0,"total_insured_value":0,"to_state":"CS","customs_invoice_nr":"","customs_shipment_type":null,"parcel_items":[],"type":null,"shipment_uuid":"7ade61ad-c21a-4beb-b7fd-2f579feacdb6","shipping_method":null,"external_order_id":"8293794","external_shipment_id":"201900001"}}' + '{"parcel":{"id":8293794,"address":"Rosebud 2134 A","address_2":"Above the skies","address_divided":{"street":"Rosebud","house_number":"2134"},"city":"Almanda","company_name":"Some company","country":{"iso_2":"NL","iso_3":"NLD","name":"Netherlands"},"data":{},"date_created":"11-03-2019 14:35:10","email":"completelydifferent@email.com","name":"Completely different person","postal_code":"9238DD","reference":"0","shipment":null,"status":{"id":999,"message":"No label"},"to_service_point":null,"telephone":"+31699999999","tracking_number":"","weight":"2.490","label":{},"customs_declaration":{},"order_number":"201900001","insured_value":0,"total_insured_value":0,"to_state":"CS","customs_invoice_nr":"","customs_shipment_type":null,"parcel_items":[],"type":null,"shipment_uuid":"7ade61ad-c21a-4beb-b7fd-2f579feacdb6","shipping_method":null,"external_order_id":"8293794","external_shipment_id":"201900001"}}' ); }); - $parcel = $this->client->updateParcel(8293794, new Address('Completely different person', 'Some company', 'Rosebud', '2134A', 'Almanda', '9238DD', 'NL', 'completelydifferent@email.com', '+31699999999', 'Above the skies', 'CS')); + $parcel = $this->client->updateParcel(8293794, new Address('Completely different person', 'Some company', 'Rosebud', 'Almanda', '9238DD', 'NL', 'completelydifferent@email.com', '2134A', '+31699999999', 'Above the skies', 'CS')); $this->assertEquals('Some company', $parcel->getAddress()->getCompanyName()); } @@ -334,7 +334,7 @@ public function testCreateLabel(): void $this->guzzleClientMock->expects($this->once())->method('request')->willReturn(new Response( 200, [], - '{"parcel":{"id":8293794,"address":"Rosebud 2134A","address_2":"","address_divided":{"street":"Rosebud","house_number":"2134"},"city":"Almanda","company_name":"Some company","country":{"iso_2":"NL","iso_3":"NLD","name":"Netherlands"},"data":{},"date_created":"11-03-2019 14:35:10","email":"completelydifferent@email.com","name":"Completely different person","postal_code":"9238 DD","reference":"0","shipment":{"id":117,"name":"DHLForYou Drop Off"},"status":{"id":1000,"message":"Ready to send"},"to_service_point":null,"telephone":"+31699999999","tracking_number":"JVGL4004421100020097","weight":"2.490","label":{"label_printer":"https://panel.sendcloud.sc/api/v2/labels/label_printer/8293794","normal_printer":["https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=0","https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=1","https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=2","https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=3"]},"customs_declaration":{},"order_number":"201900001","insured_value":0,"total_insured_value":0,"to_state":null,"customs_invoice_nr":"","customs_shipment_type":null,"parcel_items":[],"type":"parcel","shipment_uuid":"7ade61ad-c21a-4beb-b7fd-2f579feacdb6","shipping_method":117,"external_order_id":"8293794","external_shipment_id":"201900001","carrier":{"code":"dhl"},"tracking_url":"https://jouwweb.shipping-portal.com/tracking/?country=nl&tracking_number=jvgl4004421100020097&postal_code=9238dd"}}' + '{"parcel":{"id":8293794,"address":"Rosebud 2134 A","address_2":"","address_divided":{"street":"Rosebud","house_number":"2134"},"city":"Almanda","company_name":"Some company","country":{"iso_2":"NL","iso_3":"NLD","name":"Netherlands"},"data":{},"date_created":"11-03-2019 14:35:10","email":"completelydifferent@email.com","name":"Completely different person","postal_code":"9238 DD","reference":"0","shipment":{"id":117,"name":"DHLForYou Drop Off"},"status":{"id":1000,"message":"Ready to send"},"to_service_point":null,"telephone":"+31699999999","tracking_number":"JVGL4004421100020097","weight":"2.490","label":{"label_printer":"https://panel.sendcloud.sc/api/v2/labels/label_printer/8293794","normal_printer":["https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=0","https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=1","https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=2","https://panel.sendcloud.sc/api/v2/labels/normal_printer/8293794?start_from=3"]},"customs_declaration":{},"order_number":"201900001","insured_value":0,"total_insured_value":0,"to_state":null,"customs_invoice_nr":"","customs_shipment_type":null,"parcel_items":[],"type":"parcel","shipment_uuid":"7ade61ad-c21a-4beb-b7fd-2f579feacdb6","shipping_method":117,"external_order_id":"8293794","external_shipment_id":"201900001","carrier":{"code":"dhl"},"tracking_url":"https://jouwweb.shipping-portal.com/tracking/?country=nl&tracking_number=jvgl4004421100020097&postal_code=9238dd"}}' )); $parcel = $this->client->createLabel(8293794, 117, 61361); diff --git a/test/UtilityTest.php b/test/UtilityTest.php index 508d85d..239dbf0 100644 --- a/test/UtilityTest.php +++ b/test/UtilityTest.php @@ -37,7 +37,9 @@ public function testParseWebhookRequest(): void $event = $client->parseWebhookRequest($request); $this->assertEquals(WebhookEvent::TYPE_PARCEL_STATUS_CHANGED, $event->getType()); + $this->assertEquals('Insulindelaan 115', $event->getParcel()->getAddress()->getAddressLine1()); $this->assertEquals('Insulindelaan', $event->getParcel()->getAddress()->getStreet()); + $this->assertEquals('115', $event->getParcel()->getAddress()->getHouseNumber()); $this->assertEquals(new \DateTimeImmutable('2018-05-02 14:38:05.993'), $event->getCreated()); $this->assertCount(1, $event->getPayload()); $this->assertArrayHasKey('parcel', $event->getPayload());