Skip to content
This repository has been archived by the owner on Oct 19, 2023. It is now read-only.

Commit

Permalink
Parse json as array instead of object (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobdekeizer authored Sep 24, 2020
1 parent d51a707 commit 5079f65
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 55 deletions.
11 changes: 6 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ public function __construct()
* @param string $apiRoute
* @param null|string $httpBody
* @param array $requestHeaders
* @return string|object|null
* @return array
* @throws RedJePakketjeException
*/
public function doRequest(
string $httpMethod,
string $apiRoute,
?string $httpBody = null,
array $requestHeaders = []
) {
): array {
$url = self::BASE_ENDPOINT . '/' . $apiRoute;

$response = $this->doRawRequest($httpMethod, $url, $httpBody, $requestHeaders);
Expand All @@ -88,20 +88,21 @@ public function doRequest(

$body = $response->getBody()->getContents();

$object = @json_decode($body);
$data = @json_decode($body, true);

if (json_last_error() !== JSON_ERROR_NONE) {
throw new RedJePakketjeException('Unable to decode api response: ' . $body);
}

if ($response->getStatusCode() >= 400) {
throw new RedJePakketjeException(
'Error executing api call: ' . $object->error_message . ', StatusCode: ' . $response->getStatusCode(),
'Error executing api call: ' . ($data['error_message'] ?? '')
. ', StatusCode: ' . $response->getStatusCode(),
$response->getStatusCode()
);
}

return $object;
return $data;
}

/**
Expand Down
16 changes: 5 additions & 11 deletions src/Endpoints/Contacts.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class Contacts extends Base
*/
public function all(): ContactsList
{
$data = $this->client->doRequest('GET', 'contacts');
$response = $this->client->doRequest('GET', 'contacts');

return ContactsList::fromArray((array) $data);
return ContactsList::fromArray($response);
}

/**
Expand All @@ -28,9 +28,7 @@ public function create(Contact $contact): Contact
{
$response = $this->client->doRequest('POST', 'contacts', json_encode($contact->toRequest()));

$data = ((array) $response)['data'] ?? [];

return Contact::fromArray((array) $data);
return Contact::fromArray($response['data'] ?? []);
}

/**
Expand All @@ -44,9 +42,7 @@ public function get(string $uuid): Contact

$response = $this->client->doRequest('GET', $apiRoute);

$data = ((array) $response)['data'] ?? [];

return Contact::fromArray((array) $data);
return Contact::fromArray($response['data']);
}

/**
Expand All @@ -60,8 +56,6 @@ public function update(Contact $contact): Contact

$response = $this->client->doRequest('POST', $apiRoute, json_encode($contact->toRequest()));

$data = ((array) $response)['data'] ?? [];

return Contact::fromArray((array) $data);
return Contact::fromArray($response['data'] ?? []);
}
}
4 changes: 1 addition & 3 deletions src/Endpoints/CutOffTimes.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ public function get(string $zipCode): CutOffTime
{
$response = $this->client->doRequest('POST', 'cut-off-times', json_encode(['zipcode' => $zipCode]));

$data = ((array) $response)['data'] ?? [];

return CutOffTime::fromArray((array) $data);
return CutOffTime::fromArray($response['data'] ?? []);
}
}
8 changes: 3 additions & 5 deletions src/Endpoints/PickUpPoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ class PickUpPoints extends Base
*/
public function all(): PickUpPointsList
{
$data = $this->client->doRequest('GET', 'pick-up-points');
$response = $this->client->doRequest('GET', 'pick-up-points');

return PickUpPointsList::fromArray((array) $data);
return PickUpPointsList::fromArray($response);
}

/**
Expand All @@ -30,8 +30,6 @@ public function get(string $uuid): PickUpPoint

$response = $this->client->doRequest('GET', $apiRoute);

$data = ((array) $response)['data'] ?? [];

return PickUpPoint::fromArray((array) $data);
return PickUpPoint::fromArray($response['data']);
}
}
12 changes: 4 additions & 8 deletions src/Endpoints/ReturnShipments.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public function all(?Parameters\All $parameter = null): ReturnShipmentsList
{
$apiRoute = 'shipments' . ($parameter ? $parameter->toQuery() : null);

$data = $this->client->doRequest('GET', $apiRoute);
$response = $this->client->doRequest('GET', $apiRoute);

return ReturnShipmentsList::fromArray((array) $data);
return ReturnShipmentsList::fromArray($response);
}

/**
Expand All @@ -35,9 +35,7 @@ public function get(string $uuid): ReturnShipmentResponse

$response = $this->client->doRequest('GET', $apiRoute);

$data = ((array) $response)['data'] ?? [];

return ReturnShipmentResponse::fromArray((array) $data);
return ReturnShipmentResponse::fromArray($response['data'] ?? []);
}

/**
Expand All @@ -49,8 +47,6 @@ public function create(ReturnShipment $returnShipment): ReturnShipmentResponse
{
$response = $this->client->doRequest('POST', 'return-shipments', json_encode($returnShipment->toRequest()));

$data = ((array) $response)['data'] ?? [];

return ReturnShipmentResponse::fromArray((array) $data);
return ReturnShipmentResponse::fromArray($response['data'] ?? []);
}
}
12 changes: 4 additions & 8 deletions src/Endpoints/Shipments.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public function all(?Parameters\All $parameter = null): ShipmentsList

$apiRoute = 'shipments' . $parameter->toQuery();

$data = $this->client->doRequest('GET', $apiRoute);
$response = $this->client->doRequest('GET', $apiRoute);

return ShipmentsList::fromArray((array) $data);
return ShipmentsList::fromArray($response);
}

/**
Expand All @@ -39,9 +39,7 @@ public function get(string $trackingCode): ShipmentResponse

$response = $this->client->doRequest('GET', $apiRoute);

$data = ((array) $response)['data'] ?? [];

return ShipmentResponse::fromArray((array) $data);
return ShipmentResponse::fromArray($response['data']);
}

/**
Expand All @@ -60,9 +58,7 @@ public function create(Shipment $shipment, ?Parameters\Create $parameter = null)

$response = $this->client->doRequest('POST', $apiRoute, json_encode($shipment->toRequest()));

$data = ((array) $response)['data'] ?? [];

return ShipmentResponse::fromArray((array) $data);
return ShipmentResponse::fromArray($response['data'] ?? []);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/BaseList.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ public function setData(?array $data): BaseList
{
$this->data = [];

foreach ($data as $shipment) {
$this->data[] = $this->createDataResourceFromArray((array) $shipment);
foreach ($data as $item) {
$this->data[] = $this->createDataResourceFromArray($item);
}

return $this;
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/BaseSimpleList.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public function setData(?array $data): BaseSimpleList
{
$this->data = [];

foreach ($data as $pickUpPoint) {
$this->data[] = $this->createDataResourceFromArray((array) $pickUpPoint);
foreach ($data as $item) {
$this->data[] = $this->createDataResourceFromArray($item);
}

return $this;
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/PickUpPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public function getContact(): ?Contact
protected function convertFromData(string $key, $value)
{
if ($key === 'contact' && $value !== null) {
return Contact::fromArray((array) $value);
return Contact::fromArray($value);
}

return $value;
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/ReturnShipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ protected function covertToData(string $key, $value)
'uuid' => $value,
];
} elseif ($key === 'product_options' && is_array($value)) {
return array_map(static function (ProductOption $productOption) {
return array_map(static function (ProductOption $productOption): array {
return $productOption->toRequest();
}, $value);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/ReturnShipmentResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public function getReturnAttempts(): ?array
protected function convertFromData(string $key, $value)
{
if ($key === 'return_attempts' && is_array($value)) {
return array_map(static function ($data) {
return ReturnAttempt::fromArray((array) $data);
return array_map(static function (array $data): ReturnAttempt {
return ReturnAttempt::fromArray($data);
}, $value);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Resources/ReturnShipmentsList.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class ReturnShipmentsList extends BaseList
*/
protected function createDataResourceFromArray(array $data): Dto
{
return ReturnShipmentResponse::fromArray((array) $data);
return ReturnShipmentResponse::fromArray($data);
}
}
2 changes: 1 addition & 1 deletion src/Resources/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ protected function covertToData(string $key, $value)
'uuid' => $value,
];
} elseif ($key === 'product_options' && is_array($value)) {
return array_map(static function (ProductOption $productOption) {
return array_map(static function (ProductOption $productOption): array {
return $productOption->toRequest();
}, $value);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Resources/ShipmentResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -624,14 +624,14 @@ public function getEvents(): ?array
protected function convertFromData(string $key, $value)
{
if ($key === 'pick_up_point' && $value !== null) {
return PickUpPoint::fromArray((array) $value);
return PickUpPoint::fromArray($value);
} elseif ($key === 'delivery_attempts' && is_array($value)) {
return array_map(static function ($data) {
return DeliveryAttempt::fromArray((array) $data);
return array_map(static function ($data): DeliveryAttempt {
return DeliveryAttempt::fromArray($data);
}, $value);
} elseif ($key === 'events' && is_array($value)) {
return array_map(static function ($data) {
return Event::fromArray((array) $data);
return array_map(static function ($data): Event {
return Event::fromArray($data);
}, $value);
}

Expand Down

0 comments on commit 5079f65

Please sign in to comment.