Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Jan 7, 2025
1 parent 27f05b2 commit 982e00e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 32 deletions.
43 changes: 12 additions & 31 deletions docs/responses.md
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()`.
3 changes: 2 additions & 1 deletion src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

View workflow job for this annotation

GitHub Actions / PHP - 8.2

PHPDoc tag @var above assignment does not specify variable name.

Check failure on line 47 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8.1

PHPDoc tag @var above assignment does not specify variable name.

Check failure on line 47 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8.4

PHPDoc tag @var above assignment does not specify variable name.

Check failure on line 47 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8

PHPDoc tag @var above assignment does not specify variable name.

Check failure on line 47 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8.3

PHPDoc tag @var above assignment does not specify variable name.

Check failure on line 47 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8.3

PHPDoc tag @var above assignment does not specify variable name.

Check failure on line 47 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8.2

PHPDoc tag @var above assignment does not specify variable name.

Check failure on line 47 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8.1

PHPDoc tag @var above assignment does not specify variable name.

Check failure on line 47 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 7.4

PHPDoc tag @var above assignment does not specify variable name.

Check failure on line 47 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8.4

PHPDoc tag @var above assignment does not specify variable name.

Check failure on line 47 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8

PHPDoc tag @var above assignment does not specify variable name.

Check failure on line 47 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 7.4

PHPDoc tag @var above assignment does not specify variable name.

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

View workflow job for this annotation

GitHub Actions / PHP - 8.2

Part $body (stdClass) of encapsed string cannot be cast to string.

Check failure on line 50 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8.1

Part $body (stdClass) of encapsed string cannot be cast to string.

Check failure on line 50 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8.4

Part $body (stdClass) of encapsed string cannot be cast to string.

Check failure on line 50 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8

Part $body (stdClass) of encapsed string cannot be cast to string.

Check failure on line 50 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8.3

Part $body (stdClass) of encapsed string cannot be cast to string.

Check failure on line 50 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8.3

Part $body (stdClass) of encapsed string cannot be cast to string.

Check failure on line 50 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8.2

Part $body (stdClass) of encapsed string cannot be cast to string.

Check failure on line 50 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8.1

Part $body (stdClass) of encapsed string cannot be cast to string.

Check failure on line 50 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 7.4

Part $body (stdClass) of encapsed string cannot be cast to string.

Check failure on line 50 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8.4

Part $body (stdClass) of encapsed string cannot be cast to string.

Check failure on line 50 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 8

Part $body (stdClass) of encapsed string cannot be cast to string.

Check failure on line 50 in src/Http/Response.php

View workflow job for this annotation

GitHub Actions / PHP - 7.4

Part $body (stdClass) of encapsed string cannot be cast to string.
Expand Down

0 comments on commit 982e00e

Please sign in to comment.