Skip to content
Closed
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Response
/**
* Create a new response instance.
*/
public function __construct(ResponseInterface $psrResponse, PendingRequest $pendingRequest, RequestInterface $psrRequest, Throwable $senderException = null)
public function __construct(ResponseInterface $psrResponse, PendingRequest $pendingRequest, RequestInterface $psrRequest, ?Throwable $senderException = null)
{
$this->psrRequest = $psrRequest;
$this->psrResponse = $psrResponse;
Expand Down Expand Up @@ -193,7 +193,7 @@ public function getSenderException(): ?Throwable
/**
* Get the JSON decoded body of the response as an array or scalar value.
*
* @param array-key|null $key
* @param array-key|null $key
* @return ($key is null ? array<array-key, mixed> : mixed)
*/
public function json(string|int|null $key = null, mixed $default = null): mixed
Expand All @@ -214,7 +214,7 @@ public function json(string|int|null $key = null, mixed $default = null): mixed
*
* Alias of json()
*
* @param array-key|null $key
* @param array-key|null $key
* @return ($key is null ? array<array-key, mixed> : mixed)
*/
public function array(int|string|null $key = null, mixed $default = null): mixed
Expand Down Expand Up @@ -265,9 +265,10 @@ public function xmlReader(): XmlReader
* Get the JSON decoded body of the response as a collection.
*
* Requires Laravel Collections (composer require illuminate/collections)
*
* @see https://github.com/illuminate/collections
*
* @param array-key|null $key
* @param array-key|null $key
* @return \Illuminate\Support\Collection<array-key, mixed>
*/
public function collect(string|int|null $key = null): Collection
Expand All @@ -287,8 +288,12 @@ public function collect(string|int|null $key = null): Collection

/**
* Cast the response to a DTO.
*
* @template T of object
*
* @return ($dto is class-string<T> ? T : object)
*/
public function dto(): mixed
public function dto(?string $dto = null): mixed
{
$request = $this->pendingRequest->getRequest();
$connector = $this->pendingRequest->getConnector();
Expand All @@ -304,14 +309,18 @@ public function dto(): mixed

/**
* Convert the response into a DTO or throw a LogicException if the response failed
*
* @template T of object
*
* @return ($dto is class-string<T> ? T : object)
*/
public function dtoOrFail(): mixed
public function dtoOrFail(?string $dto = null): mixed
{
if ($this->failed()) {
throw new LogicException('Unable to create data transfer object as the response has failed.', 0, $this->toException());
}

return $this->dto();
return $this->dto($dto);
}

/**
Expand Down Expand Up @@ -394,7 +403,7 @@ public function serverError(): bool
/**
* Execute the given callback if there was a server or client error.
*
* @param callable($this): (void) $callback
* @param callable($this): (void) $callback
* @return $this
*/
public function onError(callable $callback): static
Expand Down Expand Up @@ -454,6 +463,7 @@ protected function createException(): Throwable
* Throw an exception if a server or client error occurred.
*
* @return $this
*
* @throws \Throwable
*/
public function throw(): static
Expand Down Expand Up @@ -498,7 +508,7 @@ public function getRawStream(): mixed
/**
* Save the body to a file
*
* @param string|resource $resourceOrPath
* @param string|resource $resourceOrPath
*/
public function saveBodyToFile(mixed $resourceOrPath, bool $closeResource = true): void
{
Expand Down