-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,24 @@ | ||
# Responses | ||
|
||
Whether you interact with the endpoints using the traditional method (`$mollie->payments->...`) or the new `Request` classes, you can always inspect the `Response`. | ||
Whether you interact with the endpoints using the traditional method (`$mollie->payments->...`) or the new `Request` classes, you can always inspect the raw `Response`. | ||
|
||
## Resource Hydration | ||
By default, the response from the `EndpointCollection`s automatically hydrates into the corresponding `Resource` or `ResourceCollection` objects. You can still access the raw response using the `->getResponse()` method. | ||
By default, all responses from are automatically hydrated into the corresponding `Resource` or `ResourceCollection` objects. You can still access the raw response using the `->getResponse()` method. | ||
|
||
```php | ||
/** @var Mollie\Api\Resources\Payment $payment */ | ||
/** | ||
* Legacy approach | ||
* | ||
* @var Mollie\Api\Resources\Payment $payment | ||
*/ | ||
$payment = $mollie->payments->get('tr_*********'); | ||
|
||
$response = $payment->getResponse(); | ||
``` | ||
|
||
With the Request-based approach, you get a Response by default: | ||
|
||
```php | ||
/** @var Mollie\Api\Http\Response $response */ | ||
$response = $mollie->send(new GetPaymentRequest('tr_*********')); | ||
|
||
/** | ||
* Accessing the response is mainly for debugging, | ||
* like checking the status or inspecting the payload or URL. | ||
* New approach | ||
* | ||
* @var Mollie\Api\Resources\Payment $payment | ||
*/ | ||
$status = $response->status(); | ||
$sentPayload = $response->getPendingRequest()->payload; | ||
$sentUrlWithFilters = $response->getPendingRequest()->getUri(); | ||
$payment = $mollie->send(new GetPaymentRequest('tr_*********')); | ||
|
||
/** @var Mollie\Api\Resources\Payment $payment */ | ||
$payment = $response->toResource(); | ||
``` | ||
|
||
Thanks to the DelegatesToResource Trait in Response, you can still access methods and attributes from the underlying Resource: | ||
|
||
```php | ||
// calling a method on the underlying Mollie\Api\Resources\Payment object | ||
$response->hasSplitPayments(); | ||
|
||
// accessing an attribute on the underlying Mollie\Api\Resources\Payment object | ||
$amount = $response->amount; | ||
$response = $payment->getResponse(); | ||
``` | ||
|
||
If you prefer the old approach of directly receiving the Resource class, you can enable **auto-hydration** by calling `MollieApiClient::setAutoHydrate()`. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,8 @@ public function __construct( | |
public function json(): stdClass | ||
{ | ||
if (! $this->decoded) { | ||
$this->decoded = (object) @json_decode($body = $this->body() ?: '{}'); | ||
/** @var \stdClass */ | ||
$this->decoded = json_decode($body = $this->body() ?: '{}') ?? new \stdClass(); | ||
Check failure on line 47 in src/Http/Response.php GitHub Actions / PHP - 8.2
Check failure on line 47 in src/Http/Response.php GitHub Actions / PHP - 8.1
Check failure on line 47 in src/Http/Response.php GitHub Actions / PHP - 8.4
Check failure on line 47 in src/Http/Response.php GitHub Actions / PHP - 8
Check failure on line 47 in src/Http/Response.php GitHub Actions / PHP - 8.3
Check failure on line 47 in src/Http/Response.php GitHub Actions / PHP - 8.3
Check failure on line 47 in src/Http/Response.php GitHub Actions / PHP - 8.2
Check failure on line 47 in src/Http/Response.php GitHub Actions / PHP - 8.1
Check failure on line 47 in src/Http/Response.php GitHub Actions / PHP - 7.4
Check failure on line 47 in src/Http/Response.php GitHub Actions / PHP - 8.4
Check failure on line 47 in src/Http/Response.php GitHub Actions / PHP - 8
Check failure on line 47 in src/Http/Response.php GitHub Actions / PHP - 7.4
|
||
|
||
if (json_last_error() !== JSON_ERROR_NONE) { | ||
throw new ApiException("Unable to decode Mollie response: '{$body}'."); | ||
Check failure on line 50 in src/Http/Response.php GitHub Actions / PHP - 8.2
Check failure on line 50 in src/Http/Response.php GitHub Actions / PHP - 8.1
Check failure on line 50 in src/Http/Response.php GitHub Actions / PHP - 8.4
Check failure on line 50 in src/Http/Response.php GitHub Actions / PHP - 8
Check failure on line 50 in src/Http/Response.php GitHub Actions / PHP - 8.3
Check failure on line 50 in src/Http/Response.php GitHub Actions / PHP - 8.3
Check failure on line 50 in src/Http/Response.php GitHub Actions / PHP - 8.2
Check failure on line 50 in src/Http/Response.php GitHub Actions / PHP - 8.1
Check failure on line 50 in src/Http/Response.php GitHub Actions / PHP - 7.4
Check failure on line 50 in src/Http/Response.php GitHub Actions / PHP - 8.4
Check failure on line 50 in src/Http/Response.php GitHub Actions / PHP - 8
|
||
|