diff --git a/src/Client.php b/src/Client.php index a09c7e0..67874a7 100644 --- a/src/Client.php +++ b/src/Client.php @@ -227,7 +227,7 @@ public function createMultiParcel( ?ShippingMethod $shippingMethod = null, ?string $errors = null, int $quantity = 1 - ) : array { + ): array { $parcelData = $this->getParcelData( null, $shippingAddress, @@ -522,50 +522,46 @@ public function getReturnPortalUrl(Parcel|int $parcel): ?string } } - - /** * Summary of searchServicePoints + * * @see https://api.sendcloud.dev/docs/sendcloud-public-api/service-points%2Foperations%2Flist-service-points - * * @param string $country A country ISO 2 code (Example : 'NL') - * @param ?string $address Address of the destination address. Can accept postal code instead of the street and the house number. (Example : 'Stadhuisplein 10') - * @param ?string $carrier A comma-separated list of carrier codes (stringified) (Example : 'postnl,dpd') - * @param ?string $city City of the destination address. (Example : 'Eindhoven') - * @param ?string $house_number House number of the destination address. (Example : '10') - * @param ?string $latitude Used as a reference point to calculate the distance of the service point to the provided location. - * @param ?string $longitude Used as a reference point to calculate the distance of the service point to the provided location. - * @param ?string $ne_latitude Latitude of the northeast corner of the bounding box. - * @param ?string $ne_longitude Longitude of the northeast corner of the bounding box. - * @param ?string $postal_code Postal code of the destination address. Using postal_code will return you service points located around that particular postal code. (Example : '5611 EM') - * @param ?string $pudo_id DPD-specific query parameter. (<= 7 characters) - * @param ?int $radius Radius (in meter) of a bounding circle. Can be used instead of the ne_latitude, ne_longitude, sw_latitude, and sw_longitude parameters to define a bounding box. By default, it’s 100 meters. Minimum value: 100 meters. Maximum value: 50 000 meters. - * @param ?string $shop_type Filters results by their shop type. - * @param ?string $sw_latitude Latitude of the southwest corner of the bounding box. - * @param ?string $sw_longitude Longitude of the southwest corner of the bounding box. - * @param ?float $weight Weight (in kg.) of the parcel to be shipped to the service points. Certain carriers impose limits for certain service points that cannot accept parcels above a certain weight limit. - * - * @return array + * @param string|null $address Address of the destination address. Can accept postal code instead of the street and the house number. (Example : 'Stadhuisplein 10') + * @param string|null $carrier A comma-separated list of carrier codes (stringified) (Example : 'postnl,dpd') + * @param string|null $city City of the destination address. (Example : 'Eindhoven') + * @param string|null $houseNumber House number of the destination address. (Example : '10') + * @param string|null $latitude Used as a reference point to calculate the distance of the service point to the provided location. + * @param string|null $longitude Used as a reference point to calculate the distance of the service point to the provided location. + * @param string|null $neLatitude Latitude of the northeast corner of the bounding box. + * @param string|null $neLongitude Longitude of the northeast corner of the bounding box. + * @param string|null $postalCode Postal code of the destination address. Using postal_code will return you service points located around that particular postal code. (Example : '5611 EM') + * @param string|null $pudoId DPD-specific query parameter. (<= 7 characters) + * @param int|null $radius Radius (in meter) of a bounding circle. Can be used instead of the ne_latitude, ne_longitude, sw_latitude, and sw_longitude parameters to define a bounding box. By default, it’s 100 meters. Minimum value: 100 meters. Maximum value: 50 000 meters. + * @param string|null $shopType Filters results by their shop type. + * @param string|null $swLatitude Latitude of the southwest corner of the bounding box. + * @param string|null $swLongitude Longitude of the southwest corner of the bounding box. + * @param float|null $weight Weight (in kg.) of the parcel to be shipped to the service points. Certain carriers impose limits for certain service points that cannot accept parcels above a certain weight limit. + * @return ServicePoint[] */ public function searchServicePoints( string $country, ?string $address = null, ?string $carrier = null, ?string $city = null, - ?string $house_number = null, + ?string $houseNumber = null, ?string $latitude = null, ?string $longitude = null, - ?string $ne_latitude = null, - ?string $ne_longitude = null, - ?string $postal_code = null, - ?string $pudo_id = null, + ?string $neLatitude = null, + ?string $neLongitude = null, + ?string $postalCode = null, + ?string $pudoId = null, ?int $radius = null, - ?string $shop_type = null, - ?string $sw_latitude = null, - ?string $sw_longitude = null, + ?string $shopType = null, + ?string $swLatitude = null, + ?string $swLongitude = null, ?float $weight = null - ) : array - { + ): array { try { // Construct query array $query = []; @@ -580,8 +576,8 @@ public function searchServicePoints( if (isset($city)) { $query['city'] = $city; } - if (isset($house_number)) { - $query['house_number'] = $house_number; + if (isset($houseNumber)) { + $query['house_number'] = $houseNumber; } if (isset($latitude)) { $query['latitude'] = $latitude; @@ -589,34 +585,34 @@ public function searchServicePoints( if (isset($longitude)) { $query['longitude'] = $longitude; } - if (isset($ne_latitude)) { - $query['ne_latitude'] = $ne_latitude; + if (isset($neLatitude)) { + $query['ne_latitude'] = $neLatitude; } - if (isset($ne_longitude)) { - $query['ne_longitude'] = $ne_longitude; + if (isset($neLongitude)) { + $query['ne_longitude'] = $neLongitude; } - if (isset($postal_code)) { - $query['postal_code'] = $postal_code; + if (isset($postalCode)) { + $query['postal_code'] = $postalCode; } - if (isset($pudo_id)) { - $query['pudo_id'] = $pudo_id; + if (isset($pudoId)) { + $query['pudo_id'] = $pudoId; } if (isset($radius)) { $query['radius'] = $radius; } - if (isset($shop_type)) { - $query['shop_type'] = $shop_type; + if (isset($shopType)) { + $query['shop_type'] = $shopType; } - if (isset($sw_latitude)) { - $query['sw_latitude'] = $sw_latitude; + if (isset($swLatitude)) { + $query['sw_latitude'] = $swLatitude; } - if (isset($sw_longitude)) { - $query['sw_longitude'] = $sw_longitude; + if (isset($swLongitude)) { + $query['sw_longitude'] = $swLongitude; } if (isset($weight)) { $query['weight'] = $weight; } - + // Send request $response = $this->guzzleClient->get('service-point', [ 'query' => $query, @@ -625,28 +621,30 @@ public function searchServicePoints( // Decode and create ServicePoint objects $json = json_decode((string)$response->getBody(), true); - $service_points = []; - foreach($json as $obj) { - $service_points[] = ServicePoint::fromData($obj); + $servicePoints = []; + foreach ($json as $obj) { + $servicePoints[] = ServicePoint::fromData($obj); } - return $service_points; + return $servicePoints; } catch (TransferException $exception) { throw $this->parseGuzzleException($exception, 'Could not retrieve service point.'); } } /** - * Return corresponding service point for the given id. + * Returns service point by ID. + * * @see https://api.sendcloud.dev/docs/sendcloud-public-api/service-points%2Foperations%2Fget-a-service-point - * - * @param int $service_point_id - * @return \JouwWeb\Sendcloud\Model\ServicePoint + * @return ServicePoint + * @throws SendcloudRequestException */ - public function getServicePoint(int $service_point_id) : ServicePoint + public function getServicePoint(ServicePoint|int $servicePoint): ServicePoint { + $servicePointId = $servicePoint instanceof ServicePoint ? $servicePoint->getId() : $servicePoint; + try { - $response = $this->guzzleClient->get('service-point/' . $service_point_id); + $response = $this->guzzleClient->get('service-point/' . $servicePointId); return ServicePoint::fromData(json_decode((string)$response->getBody(), true)); } catch (TransferException $exception) { throw $this->parseGuzzleException($exception, 'Could not retrieve service point.'); @@ -851,6 +849,7 @@ protected function parseGuzzleException( return new SendcloudRequestException($message, $code, $exception, $responseCode, $responseMessage); } + // TODO: Remove parseParcelArgument() now we use native unions. protected function parseParcelArgument(Parcel|int $parcel): int { if (is_int($parcel)) { diff --git a/src/Model/ServicePoint.php b/src/Model/ServicePoint.php index 525db45..7227c83 100644 --- a/src/Model/ServicePoint.php +++ b/src/Model/ServicePoint.php @@ -3,53 +3,54 @@ namespace JouwWeb\Sendcloud\Model; /** - * Implementation of sencloud service point object - * @see https://api.sendcloud.dev/docs/sendcloud-public-api/service-points%2Foperations%2Flist-service-points + * Implementation of Sendcloud service point object. + * + * @see https://api.sendcloud.dev/docs/sendcloud-public-api/service-points%2Foperations%2Fget-a-service-point */ class ServicePoint { - public static function fromData(array $data) : self + public static function fromData(array $data): self { return new self( - (int) $data['id'], - (string) $data['code'], - (bool) $data['is_active'], - isset($data['shop_type']) ? (string) $data['shop_type'] : null, - (array) $data['extra_data'], - (string) $data['name'], - (string) $data['street'], - (string) $data['house_number'], - (string) $data['postal_code'], - (string) $data['city'], - (string) $data['latitude'], - (string) $data['longitude'], - (string) $data['email'], - (string) $data['phone'], - (string) $data['homepage'], - (string) $data['carrier'], - (string) $data['country'], - (array) $data['formatted_opening_times'], - (bool) $data['open_tomorrow'], - (bool) $data['open_upcoming_week'], - (int) $data['distance'] + (int)$data['id'], + (string)$data['code'], + (bool)$data['is_active'], + isset($data['shop_type']) ? (string)$data['shop_type'] : null, + (array)$data['extra_data'], + (string)$data['name'], + (string)$data['street'], + (string)$data['house_number'], + (string)$data['postal_code'], + (string)$data['city'], + (string)$data['latitude'], + (string)$data['longitude'], + (string)$data['email'], + (string)$data['phone'], + (string)$data['homepage'], + (string)$data['carrier'], + (string)$data['country'], + (array)$data['formatted_opening_times'], + (bool)$data['open_tomorrow'], + (bool)$data['open_upcoming_week'], + (int)$data['distance'] ); } /** - * @param array $extra_data Can contain carrier specific data - * @param array> $formatted_opening_times + * @param array $extraData Can contain carrier specific data + * @param array $formattedOpeningTimes * @param int $distance Distance between the reference point and the service point in meters. */ public function __construct( protected int $id, protected string $code, - protected bool $is_active, - protected ?string $shop_type = null, - protected array $extra_data, + protected bool $isActive, + protected ?string $shopType, + protected array $extraData, protected string $name, protected string $street, - protected string $house_number, - protected string $postal_code, + protected string $houseNumber, + protected string $postalCode, protected string $city, protected string $latitude, protected string $longitude, @@ -58,129 +59,126 @@ public function __construct( protected string $homepage, protected string $carrier, protected string $country, - protected array $formatted_opening_times, - protected bool $open_tomorrow, - protected bool $open_upcoming_week, - protected int $distance + protected array $formattedOpeningTimes, + protected bool $openTomorrow, + protected bool $openUpcomingWeek, + protected int $distance, ) { - } - /** Getters */ - - public function getId() : int + public function getId(): int { return $this->id; } - public function getCode() : string + public function getCode(): string { return $this->code; } - public function isActive() : bool + public function isActive(): bool { - return $this->is_active; + return $this->isActive; } - public function getShopType() : ?string + public function getShopType(): ?string { - return $this->shop_type; + return $this->shopType; } /** - * Can contain carrier specific data + * Can contain carrier specific data. + * * @return array */ - public function getExtraData() : array + public function getExtraData(): array { - return $this->extra_data; + return $this->extraData; } - public function getName() : string + public function getName(): string { return $this->name; } - public function getStreet() : string - { + public function getStreet(): string + { return $this->street; } - public function getHouseNumber() : string + public function getHouseNumber(): string { - return $this->house_number; + return $this->houseNumber; } - public function getPostalCode() : string + public function getPostalCode(): string { - return $this->postal_code; + return $this->postalCode; } - public function getCity() : string + public function getCity(): string { return $this->city; } - public function getLatitude() : string + public function getLatitude(): string { return $this->latitude; } - public function getLongitude() : string + public function getLongitude(): string { return $this->longitude; } - public function getEmail() : string + public function getEmail(): string { return $this->email; } - public function getPhone() : string + public function getPhone(): string { return $this->phone; } - public function getHomepage() : string + public function getHomepage(): string { return $this->homepage; } - public function getCarrier() : string + public function getCarrier(): string { return $this->carrier; } - public function getCountry() : string + public function getCountry(): string { return $this->country; } /** - * @return array> + * @return array */ - public function getFormattedOpeningTimes() : array + public function getFormattedOpeningTimes(): array { - return $this->formatted_opening_times; + return $this->formattedOpeningTimes; } - public function isOpenTomorrow() : bool + public function isOpenTomorrow(): bool { - return $this->open_tomorrow; + return $this->openTomorrow; } - public function isOpenUpcomingWeek() : bool + public function isOpenUpcomingWeek(): bool { - return $this->open_upcoming_week; + return $this->openUpcomingWeek; } /** * Distance between the reference point and the service point in meters. - * @return int */ - public function getDistance() : int + public function getDistance(): int { return $this->distance; } -} \ No newline at end of file +} diff --git a/test/ClientTest.php b/test/ClientTest.php index 3d306a3..5e3cba2 100644 --- a/test/ClientTest.php +++ b/test/ClientTest.php @@ -217,7 +217,7 @@ public function testCreateMultiParcel(): void $this->assertCount(2, $parcels); - foreach($parcels as $key => $parcel) { + foreach ($parcels as $key => $parcel) { $id = $key == 0 ? 8293794 : 8293795; $this->assertEquals($id, $parcel->getId()); @@ -479,12 +479,7 @@ function ($method, $url, $data) use (&$requestNumber) { $this->assertEquals('pdfdata', $pdf); } - /** - * Test of 'service-point' search api endpoint - * @see https://api.sendcloud.dev/docs/sendcloud-public-api/service-points%2Foperations%2Flist-service-points - * @return void - */ - public function testSearchServicePoint() : void + public function testSearchServicePoint(): void { $this->guzzleClientMock->expects($this->once())->method('request')->willReturn(new Response( 200, @@ -495,19 +490,14 @@ public function testSearchServicePoint() : void ]' )); - $service_points = $this->client->searchServicePoints('NL'); + $servicePoints = $this->client->searchServicePoints('NL'); - $this->assertEquals(2, count($service_points)); - $this->assertEquals(1, $service_points[0]->getID()); - $this->assertEquals(2, $service_points[1]->getID()); + $this->assertCount(2, $servicePoints); + $this->assertEquals(1, $servicePoints[0]->getID()); + $this->assertEquals(2, $servicePoints[1]->getID()); } - /** - * Test of 'service-point' api endpoint - * @see https://api.sendcloud.dev/docs/sendcloud-public-api/service-points%2Foperations%2Fget-a-service-point (Response Example : RetrieveServicePoint) - * @return void - */ - public function testGetServicePoint() : void + public function testGetServicePoint(): void { $this->guzzleClientMock->expects($this->once())->method('request')->willReturn(new Response( 200, @@ -515,59 +505,59 @@ public function testGetServicePoint() : void '{"id":26,"code":"4c8181feec8f49fdbe67d9c9f6aaaf6f","is_active":true,"shop_type":null,"extra_data":{"partner_name":"PostNL","sales_channel":"AFHAALPUNT","terminal_type":"NRS","retail_network_id":"PNPNL-01"},"name":"DUMMY-3f1d6384391f45ce","street":"Sesamstraat","house_number":"40","postal_code":"5699YE","city":"Eindhoven","latitude":"51.440400","longitude":"5.475800","email":"devnull@sendcloud.nl","phone":"+31401234567","homepage":"https://www.sendcloud.nl","carrier":"postnl","country":"NL","formatted_opening_times":{"0":["13:30 - 17:15"],"1":["09:00 - 12:00","13:30 - 17:15"],"2":["09:00 - 12:00","13:30 - 17:15"],"3":[],"4":["09:00 - 12:00","13:30 - 17:15"],"5":["09:00 - 12:00","13:30 - 17:15"],"6":[]},"open_tomorrow":true,"open_upcoming_week":true,"distance":361}' )); - $extra_data = [ - 'partner_name' => "PostNL", - 'sales_channel' => "AFHAALPUNT", - 'terminal_type' => "NRS", - 'retail_network_id' => "PNPNL-01" + $extraData = [ + 'partner_name' => 'PostNL', + 'sales_channel' => 'AFHAALPUNT', + 'terminal_type' => 'NRS', + 'retail_network_id' => 'PNPNL-01', ]; - $formatted_opening_times = [ - "0"=> [ - "13:30 - 17:15" + $formattedOpeningTimes = [ + '0' => [ + '13:30 - 17:15', ], - "1" => [ - "09:00 - 12:00", - "13:30 - 17:15" + '1' => [ + '09:00 - 12:00', + '13:30 - 17:15', ], - "2" => [ - "09:00 - 12:00", - "13:30 - 17:15" + '2' => [ + '09:00 - 12:00', + '13:30 - 17:15', ], - "3" => [], - "4" => [ - "09:00 - 12:00", - "13:30 - 17:15" + '3' => [], + '4' => [ + '09:00 - 12:00', + '13:30 - 17:15', ], - "5" => [ - "09:00 - 12:00", - "13:30 - 17:15" + '5' => [ + '09:00 - 12:00', + '13:30 - 17:15', ], - "6" => [] + '6' => [], ]; - $service_point = $this->client->getServicePoint(26); - - $this->assertEquals(26, $service_point->getId()); - $this->assertEquals('4c8181feec8f49fdbe67d9c9f6aaaf6f', $service_point->getCode()); - $this->assertTrue($service_point->isActive()); - $this->assertNull($service_point->getShopType()); - $this->assertEquals($extra_data, $service_point->getExtraData()); - $this->assertEquals('DUMMY-3f1d6384391f45ce', $service_point->getName()); - $this->assertEquals('Sesamstraat', $service_point->getStreet()); - $this->assertEquals('40', $service_point->getHouseNumber()); - $this->assertEquals('5699YE', $service_point->getPostalCode()); - $this->assertEquals('Eindhoven', $service_point->getCity()); - $this->assertEquals('51.440400', $service_point->getLatitude()); - $this->assertEquals('5.475800', $service_point->getLongitude()); - $this->assertEquals('devnull@sendcloud.nl', $service_point->getEmail()); - $this->assertEquals('+31401234567', $service_point->getPhone()); - $this->assertEquals('https://www.sendcloud.nl', $service_point->getHomepage()); - $this->assertEquals('postnl', $service_point->getCarrier()); - $this->assertEquals('NL', $service_point->getCountry()); - $this->assertEquals($formatted_opening_times, $service_point->getFormattedOpeningTimes()); - $this->assertTrue($service_point->isOpenTomorrow()); - $this->assertTrue($service_point->isOpenUpcomingWeek()); - $this->assertEquals(361, $service_point->getDistance()); + $servicePoint = $this->client->getServicePoint(26); + + $this->assertEquals(26, $servicePoint->getId()); + $this->assertEquals('4c8181feec8f49fdbe67d9c9f6aaaf6f', $servicePoint->getCode()); + $this->assertTrue($servicePoint->isActive()); + $this->assertNull($servicePoint->getShopType()); + $this->assertEquals($extraData, $servicePoint->getExtraData()); + $this->assertEquals('DUMMY-3f1d6384391f45ce', $servicePoint->getName()); + $this->assertEquals('Sesamstraat', $servicePoint->getStreet()); + $this->assertEquals('40', $servicePoint->getHouseNumber()); + $this->assertEquals('5699YE', $servicePoint->getPostalCode()); + $this->assertEquals('Eindhoven', $servicePoint->getCity()); + $this->assertEquals('51.440400', $servicePoint->getLatitude()); + $this->assertEquals('5.475800', $servicePoint->getLongitude()); + $this->assertEquals('devnull@sendcloud.nl', $servicePoint->getEmail()); + $this->assertEquals('+31401234567', $servicePoint->getPhone()); + $this->assertEquals('https://www.sendcloud.nl', $servicePoint->getHomepage()); + $this->assertEquals('postnl', $servicePoint->getCarrier()); + $this->assertEquals('NL', $servicePoint->getCountry()); + $this->assertEquals($formattedOpeningTimes, $servicePoint->getFormattedOpeningTimes()); + $this->assertTrue($servicePoint->isOpenTomorrow()); + $this->assertTrue($servicePoint->isOpenUpcomingWeek()); + $this->assertEquals(361, $servicePoint->getDistance()); } }