Skip to content

Commit 1bb4910

Browse files
committed
Allow retrieval of full and split address
1 parent 7dfa79f commit 1bb4910

File tree

4 files changed

+33
-14
lines changed

4 files changed

+33
-14
lines changed

src/Client.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,7 @@ protected function getParcelData(
556556
'company_name' => $shippingAddress->getCompanyName() ?? '',
557557
'address' => $shippingAddress->getAddress(),
558558
'address_2' => $shippingAddress->getAddressLine2() ?? '',
559-
'house_number' => $shippingAddress->getHouseNumber(),
559+
'house_number' => $shippingAddress->getHouseNumber() ?? '',
560560
'city' => $shippingAddress->getCity(),
561561
'postal_code' => $shippingAddress->getPostalCode(),
562562
'country' => $shippingAddress->getCountryCode(),
@@ -643,9 +643,9 @@ protected function getParcelData(
643643
$parcelData = array_merge($parcelData, [
644644
'from_name' => $senderAddress->getName(),
645645
'from_company_name' => $senderAddress->getCompanyName() ?? '',
646-
'from_address_1' => $senderAddress->getStreet(),
646+
'from_address_1' => $senderAddress->getAddress(),
647647
'from_address_2' => $senderAddress->getAddressLine2() ?? '',
648-
'from_house_number' => $senderAddress->getHouseNumber(),
648+
'from_house_number' => $senderAddress->getHouseNumber() ?? '',
649649
'from_city' => $senderAddress->getCity(),
650650
'from_postal_code' => $senderAddress->getPostalCode(),
651651
'from_country' => $senderAddress->getCountryCode(),

src/Model/Address.php

+23-6
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,34 @@
44

55
class Address
66
{
7+
/** Street parsed from address by Sendcloud. */
8+
protected ?string $street = null;
9+
710
public static function fromParcelData(array $data): self
811
{
9-
return new self(
12+
$address = new self(
1013
(string)$data['name'],
1114
(string)$data['company_name'],
12-
(string)$data['address_divided']['street'],
13-
(string)$data['address_divided']['house_number'],
15+
(string)$data['address'],
1416
(string)$data['city'],
1517
(string)$data['postal_code'],
1618
(string)$data['country']['iso_2'],
1719
(string)$data['email'],
20+
(string)$data['address_divided']['house_number'],
1821
((string)$data['telephone'] ?: null),
1922
((string)$data['address_2'] ?: null),
2023
((string)$data['to_state'] ?: null)
2124
);
25+
26+
$address->street = (string)$data['address_divided']['street'];
27+
28+
return $address;
2229
}
2330

31+
/**
32+
* @param string $address Full address line 1 including house number, unless explicitly specifying {@see $houseNumber}.
33+
* @param string|null $houseNumber Will be added onto {@see $address}. Leave out if address already contains a house number.
34+
*/
2435
public function __construct(
2536
protected string $name,
2637
protected ?string $companyName,
@@ -29,7 +40,7 @@ public function __construct(
2940
protected string $postalCode,
3041
protected string $countryCode,
3142
protected string $emailAddress,
32-
protected string $houseNumber = '',
43+
protected ?string $houseNumber = null,
3344
protected ?string $phoneNumber = null,
3445
protected ?string $addressLine2 = null,
3546
protected ?string $countryStateCode = null
@@ -71,7 +82,12 @@ public function getEmailAddress(): string
7182
return $this->emailAddress;
7283
}
7384

74-
public function getHouseNumber(): string
85+
public function getStreet(): ?string
86+
{
87+
return $this->street;
88+
}
89+
90+
public function getHouseNumber(): ?string
7591
{
7692
return $this->houseNumber;
7793
}
@@ -110,11 +126,12 @@ public function toArray(): array
110126
'countryCode' => $this->getCountryCode(),
111127
'displayName' => $this->getDisplayName(),
112128
'emailAddress' => $this->getEmailAddress(),
113-
'houseNumber' => $this->getHouseNumber(),
114129
'name' => $this->getName(),
115130
'phoneNumber' => $this->getPhoneNumber(),
116131
'postalCode' => $this->getPostalCode(),
117132
'address' => $this->getAddress(),
133+
'street' => $this->getStreet(),
134+
'houseNumber' => $this->getHouseNumber(),
118135
'countryStateCode' => $this->getCountryStateCode(),
119136
];
120137
}

test/ClientTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ public function testGetUser(): void
4040
$this->guzzleClientMock->expects($this->once())->method('request')->willReturn(new Response(
4141
200,
4242
[],
43-
'{"user":{"address":"Insulindelaan","city":"Eindhoven","company_logo":null,"company_name":"SendCloud","data":[],"email":"[email protected]","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"}}'
43+
'{"user":{"address":"Insulindelaan 115","city":"Eindhoven","company_logo":null,"company_name":"SendCloud","data":[],"email":"[email protected]","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"}}'
4444
));
4545

4646
$user = $this->client->getUser();
4747

4848
$this->assertEquals('johndoe', $user->getUsername());
4949
$this->assertEquals('SendCloud', $user->getCompanyName());
5050
$this->assertEquals('+31626262626', $user->getPhoneNumber());
51-
$this->assertEquals('Insulindelaan', $user->getAddress());
51+
$this->assertEquals('Insulindelaan 115', $user->getAddress());
5252
$this->assertEquals('Eindhoven', $user->getCity());
5353
$this->assertEquals('5642CV', $user->getPostalCode());
5454
$this->assertEquals('[email protected]', $user->getEmailAddress());
@@ -320,7 +320,7 @@ public function testUpdateParcel(): void
320320
return new Response(
321321
200,
322322
[],
323-
'{"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":"[email protected]","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"}}'
323+
'{"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":"[email protected]","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"}}'
324324
);
325325
});
326326

@@ -334,7 +334,7 @@ public function testCreateLabel(): void
334334
$this->guzzleClientMock->expects($this->once())->method('request')->willReturn(new Response(
335335
200,
336336
[],
337-
'{"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":"[email protected]","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"}}'
337+
'{"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":"[email protected]","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"}}'
338338
));
339339

340340
$parcel = $this->client->createLabel(8293794, 117, 61361);

test/UtilityTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ public function testParseWebhookRequest(): void
3737
$event = $client->parseWebhookRequest($request);
3838

3939
$this->assertEquals(WebhookEvent::TYPE_PARCEL_STATUS_CHANGED, $event->getType());
40-
$this->assertEquals('Insulindelaan', $event->getParcel()->getAddress()->getAddress());
40+
$this->assertEquals('Insulindelaan 115', $event->getParcel()->getAddress()->getAddress());
41+
$this->assertEquals('Insulindelaan', $event->getParcel()->getAddress()->getStreet());
42+
$this->assertEquals('115', $event->getParcel()->getAddress()->getHouseNumber());
4143
$this->assertEquals(new \DateTimeImmutable('2018-05-02 14:38:05.993'), $event->getCreated());
4244
$this->assertCount(1, $event->getPayload());
4345
$this->assertArrayHasKey('parcel', $event->getPayload());

0 commit comments

Comments
 (0)