class ApiResponse extends Response implements Arrayable

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected mixed $decoded

The decoded JSON response.

from  Response
protected bool $decodedJson

Indicates if the JSON response has been decoded.

from  Response
protected JSON_THROW_ON_ERROR> $decodingFlags

The flags that were used when decoding the JSON response.

from  Response
protected Closure|null $decodeUsing

The custom decode callback.

from  Response
CookieJar|null $cookies

The request cookies.

from  Response
TransferStats|null $transferStats

The transfer stats for the request.

from  Response
protected false|int|null $truncateExceptionsAt

The length at which request exceptions will be truncated.

from  Response
static JSON_THROW_ON_ERROR> $defaultJsonDecodingFlags

The flags passed to json_decode by default.

from  Response
protected array<string, mixed> $context from  HasContext

Methods

bool
ok()

Determine if the response code was 200 "OK" response.

bool
created()

Determine if the response code was 201 "Created" response.

bool
accepted()

Determine if the response code was 202 "Accepted" response.

bool
noContent(int $status = 204)

Determine if the response code was the given status code and the body has no content.

bool
movedPermanently()

Determine if the response code was a 301 "Moved Permanently".

bool
found()

Determine if the response code was a 302 "Found" response.

bool
notModified()

Determine if the response code was a 304 "Not Modified" response.

bool
badRequest()

Determine if the response was a 400 "Bad Request" response.

bool
unauthorized()

Determine if the response was a 401 "Unauthorized" response.

bool
paymentRequired()

Determine if the response was a 402 "Payment Required" response.

bool
forbidden()

Determine if the response was a 403 "Forbidden" response.

bool
notFound()

Determine if the response was a 404 "Not Found" response.

bool
requestTimeout()

Determine if the response was a 408 "Request Timeout" response.

bool
conflict()

Determine if the response was a 409 "Conflict" response.

bool
unprocessableContent()

Determine if the response was a 422 "Unprocessable Content" response.

bool
unprocessableEntity()

Determine if the response was a 422 "Unprocessable Content" response.

bool
tooManyRequests()

Determine if the response was a 429 "Too Many Requests" response.

mixed
tap(callable|null $callback = null)

Call the given Closure with this instance then return the instance.

from  Tappable
static void
macro(string $name, callable|object $macro)

Register a custom macro.

static void
mixin(object $mixin, bool $replace = true)

Mix another object into the class.

static bool
hasMacro(string $name)

Check if macro is registered.

static void
flushMacros()

Flush the existing macros.

static mixed
__callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

mixed
__call(string $method, array $parameters)

Dynamically handle calls to the class.

__construct(ResponseInterface $response)

Create a new response instance.

from  Response
string
body()

Get the body of the response.

from  Response
lines()

Read lines from the current response body position without rewinding.

from  Response
jsonLines(int|null $flags = null)

Decode non-empty JSON lines from the current response body position.

from  Response
mixed
json(string|null $key = null, mixed $default = null, int|null $flags = null)

Get the decoded JSON body of the response as an array or scalar value.

from  Response
mixed
object(int|null $flags = null)

Get the decoded JSON body of the response as an object.

from  Response
decodeUsing(Closure|null $callback)

Set a custom decode callback.

from  Response
mixed
decode(string $body, bool $asObject = false, int $flags = 0)

Decode the given response body.

from  Response
collect(string|null $key = null, int|null $flags = null)

Get the decoded JSON body of the response as a collection.

from  Response
fluent(string|null $key = null, int|null $flags = null)

Get the decoded JSON body of the response as a fluent object.

from  Response
mixed
resource()

Get the body of the response as a PHP resource.

from  Response
string
header(string $header)

Get a header from the response.

from  Response
array
headers()

Get the headers from the response.

from  Response
int
status()

Get the status code of the response.

from  Response
string
reason()

Get the reason phrase of the response.

from  Response
UriInterface|null
effectiveUri()

Get the effective URI of the response.

from  Response
bool
successful()

Determine if the request was successful.

from  Response
bool
redirect()

Determine if the response was a redirect.

from  Response
bool
failed()

Determine if the response indicates a client or server error occurred.

from  Response
bool
clientError()

Determine if the response indicates a client error occurred.

from  Response
bool
serverError()

Determine if the response indicates a server error occurred.

from  Response
onError(callable $callback)

Execute the given callback if there was a server or client error.

from  Response
CookieJar|null
cookies()

Get the response cookies.

from  Response
array
handlerStats()

Get the handler stats of the response.

from  Response
close()

Close the stream and any underlying resources.

from  Response
ResponseInterface
toPsrResponse()

Get the underlying PSR response for the response.

from  Response
toException()

Create an exception if a server or client error occurred.

from  Response
newRequestException()

Create a new request exception for the response.

from  Response
throw(callable|null $callback = null)

Throw an exception if a server or client error occurred.

from  Response
throwIf(bool|Closure $condition, callable|null $callback = null)

Throw an exception if a server or client error occurred and the given condition evaluates to true.

from  Response
throwUnless(bool|Closure $condition, callable|null $callback = null)

Throw an exception if a server or client error occurred and the given condition evaluates to false.

from  Response
throwIfStatus(int|callable $statusCode)

Throw an exception if the response status code matches the given code.

from  Response
throwUnlessStatus(int|callable $statusCode)

Throw an exception unless the response status code matches the given code.

from  Response
throwIfClientError()

Throw an exception if the response status code is a 4xx level code.

from  Response
throwIfServerError()

Throw an exception if the response status code is a 5xx level code.

from  Response
truncateExceptionsAt(int $length)

Indicate that request exceptions should be truncated to the given length.

from  Response
dontTruncateExceptions()

Indicate that request exceptions should not be truncated.

from  Response
dump(string|null $key = null)

Dump the content from the response.

from  Response
never
dd(string|null $key = null)

Dump the content from the response and end the script.

from  Response
dumpHeaders()

Dump the headers from the response.

from  Response
never
ddHeaders()

Dump the headers from the response and end the script.

from  Response
bool
offsetExists(string $offset)

Determine if the given offset exists.

mixed
offsetGet(string $offset)

Get the value for a given offset.

void
offsetSet(string $offset, mixed $value)

Set the value at the given offset.

from  Response
void
offsetUnset(string $offset)

Unset the value at the given offset.

from  Response
static void
flushState()

Flush all static state.

from  Response
string
__toString()

Get the body of the response.

from  Response
withContext(array|string $key, mixed $value = null)

Set the API request/response context.

mixed
context(string|null $key = null, mixed $default = null)

Get the API request/response context.

static ApiResponse
createFrom(Response $response)

Create an API response from an HTTP client response.

withStatus(int $code, string $reasonPhrase = '')

Set the response status.

withProtocolVersion(string $version)

Set the response protocol version.

bool
hasHeader(string $name)

Determine whether the response has a header.

withHeader(string $name, mixed $value)

Set a response header.

withAddedHeader(string $name, mixed $value)

Add a value to a response header.

withoutHeader(string $name)

Remove a response header.

withBody(StreamInterface $body)

Set the response body.

array
toArray()

Transform the response into an array.

bool
hasEmptyOrNullJsonBody()

Determine whether the body is empty or contains JSON null.

Details

bool ok()

Determine if the response code was 200 "OK" response.

Return Value

bool

bool created()

Determine if the response code was 201 "Created" response.

Return Value

bool

bool accepted()

Determine if the response code was 202 "Accepted" response.

Return Value

bool

bool noContent(int $status = 204)

Determine if the response code was the given status code and the body has no content.

Parameters

int $status

Return Value

bool

bool movedPermanently()

Determine if the response code was a 301 "Moved Permanently".

Return Value

bool

bool found()

Determine if the response code was a 302 "Found" response.

Return Value

bool

bool notModified()

Determine if the response code was a 304 "Not Modified" response.

Return Value

bool

bool badRequest()

Determine if the response was a 400 "Bad Request" response.

Return Value

bool

bool unauthorized()

Determine if the response was a 401 "Unauthorized" response.

Return Value

bool

bool paymentRequired()

Determine if the response was a 402 "Payment Required" response.

Return Value

bool

bool forbidden()

Determine if the response was a 403 "Forbidden" response.

Return Value

bool

in DeterminesStatusCode at line 100
bool notFound()

Determine if the response was a 404 "Not Found" response.

Return Value

bool

in DeterminesStatusCode at line 108
bool requestTimeout()

Determine if the response was a 408 "Request Timeout" response.

Return Value

bool

in DeterminesStatusCode at line 116
bool conflict()

Determine if the response was a 409 "Conflict" response.

Return Value

bool

in DeterminesStatusCode at line 124
bool unprocessableContent()

Determine if the response was a 422 "Unprocessable Content" response.

Return Value

bool

in DeterminesStatusCode at line 132
bool unprocessableEntity()

Determine if the response was a 422 "Unprocessable Content" response.

Return Value

bool

in DeterminesStatusCode at line 140
bool tooManyRequests()

Determine if the response was a 429 "Too Many Requests" response.

Return Value

bool

in Tappable at line 17
mixed tap(callable|null $callback = null)

Call the given Closure with this instance then return the instance.

Parameters

callable|null $callback

Return Value

mixed

in Macroable at line 28
static void macro(string $name, callable|object $macro)

Register a custom macro.

Boot-only. Macros persist in a static property for the worker lifetime and apply to every subsequent call on the macroable class.

Parameters

string $name
callable|object $macro

Return Value

void

in Macroable at line 41
static void mixin(object $mixin, bool $replace = true)

Mix another object into the class.

Boot-only. Delegates to macro() for each method; registered macros persist in a static property for the worker lifetime.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

in Macroable at line 57
static bool hasMacro(string $name)

Check if macro is registered.

Parameters

string $name

Return Value

bool

in Macroable at line 68
static void flushMacros()

Flush the existing macros.

Boot or tests only. Clears worker-wide macros; concurrent coroutines may resolve different methods depending on timing.

Return Value

void

in Macroable at line 78
static mixed __callStatic(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

in Macroable at line 103
mixed __call(string $method, array $parameters)

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

in Response at line 84
__construct(ResponseInterface $response)

Create a new response instance.

Parameters

ResponseInterface $response

in Response at line 91
string body()

Get the body of the response.

Return Value

string

in Response at line 101
Generator lines()

Read lines from the current response body position without rewinding.

Return Value

Generator

in Response at line 135
Generator jsonLines(int|null $flags = null)

Decode non-empty JSON lines from the current response body position.

Parameters

int|null $flags

Return Value

Generator

in Response at line 151
mixed json(string|null $key = null, mixed $default = null, int|null $flags = null)

Get the decoded JSON body of the response as an array or scalar value.

Parameters

string|null $key
mixed $default
int|null $flags

Return Value

mixed

in Response at line 185
mixed object(int|null $flags = null)

Get the decoded JSON body of the response as an object.

Example: If the response body is: [ {"id": 1, "name": "John"}, {"id": 2, "name": "Jane"} ]

This method will return an array of objects. Scalar JSON values remain their decoded scalar type.

Parameters

int|null $flags

Return Value

mixed

in Response at line 201
Response decodeUsing(Closure|null $callback)

Set a custom decode callback.

The callback will be invoked with the following parameters:

  • string $body: The raw response body.
  • bool $asObject: Whether JSON objects should be decoded as objects.

Parameters

Closure|null $callback

Return Value

Response

in Response at line 216
protected mixed decode(string $body, bool $asObject = false, int $flags = 0)

Decode the given response body.

Parameters

string $body
bool $asObject
int $flags

Return Value

mixed

in Response at line 230
Collection collect(string|null $key = null, int|null $flags = null)

Get the decoded JSON body of the response as a collection.

Parameters

string|null $key
int|null $flags

Return Value

Collection

in Response at line 240
Fluent fluent(string|null $key = null, int|null $flags = null)

Get the decoded JSON body of the response as a fluent object.

Parameters

string|null $key
int|null $flags

Return Value

Fluent

in Response at line 252
mixed resource()

Get the body of the response as a PHP resource.

Return Value

mixed

Exceptions

InvalidArgumentException

in Response at line 260
string header(string $header)

Get a header from the response.

Parameters

string $header

Return Value

string

in Response at line 268
array headers()

Get the headers from the response.

Return Value

array

in Response at line 276
int status()

Get the status code of the response.

Return Value

int

in Response at line 284
string reason()

Get the reason phrase of the response.

Return Value

string

in Response at line 292
UriInterface|null effectiveUri()

Get the effective URI of the response.

Return Value

UriInterface|null

in Response at line 300
bool successful()

Determine if the request was successful.

Return Value

bool

in Response at line 308
bool redirect()

Determine if the response was a redirect.

Return Value

bool

in Response at line 316
bool failed()

Determine if the response indicates a client or server error occurred.

Return Value

bool

in Response at line 324
bool clientError()

Determine if the response indicates a client error occurred.

Return Value

bool

in Response at line 332
bool serverError()

Determine if the response indicates a server error occurred.

Return Value

bool

in Response at line 340
Response onError(callable $callback)

Execute the given callback if there was a server or client error.

Parameters

callable $callback

Return Value

Response

in Response at line 352
CookieJar|null cookies()

Get the response cookies.

Return Value

CookieJar|null

in Response at line 360
array handlerStats()

Get the handler stats of the response.

Return Value

array

in Response at line 368
Response close()

Close the stream and any underlying resources.

Return Value

Response

in Response at line 378
ResponseInterface toPsrResponse()

Get the underlying PSR response for the response.

Return Value

ResponseInterface

in Response at line 386
RequestException|null toException()

Create an exception if a server or client error occurred.

Return Value

RequestException|null

in Response at line 398
protected RequestException newRequestException()

Create a new request exception for the response.

Return Value

RequestException

in Response at line 410
Response throw(callable|null $callback = null)

Throw an exception if a server or client error occurred.

Parameters

callable|null $callback

Return Value

Response

Exceptions

RequestException

in Response at line 430
Response throwIf(bool|Closure $condition, callable|null $callback = null)

Throw an exception if a server or client error occurred and the given condition evaluates to true.

Parameters

bool|Closure $condition
callable|null $callback

Return Value

Response

Exceptions

RequestException

in Response at line 442
Response throwUnless(bool|Closure $condition, callable|null $callback = null)

Throw an exception if a server or client error occurred and the given condition evaluates to false.

Parameters

bool|Closure $condition
callable|null $callback

Return Value

Response

Exceptions

RequestException

in Response at line 452
Response throwIfStatus(int|callable $statusCode)

Throw an exception if the response status code matches the given code.

Parameters

int|callable $statusCode

Return Value

Response

Exceptions

RequestException

in Response at line 467
Response throwUnlessStatus(int|callable $statusCode)

Throw an exception unless the response status code matches the given code.

Parameters

int|callable $statusCode

Return Value

Response

Exceptions

RequestException

in Response at line 481
Response throwIfClientError()

Throw an exception if the response status code is a 4xx level code.

Return Value

Response

Exceptions

RequestException

in Response at line 491
Response throwIfServerError()

Throw an exception if the response status code is a 5xx level code.

Return Value

Response

Exceptions

RequestException

in Response at line 499
Response truncateExceptionsAt(int $length)

Indicate that request exceptions should be truncated to the given length.

Parameters

int $length

Return Value

Response

in Response at line 509
Response dontTruncateExceptions()

Indicate that request exceptions should not be truncated.

Return Value

Response

in Response at line 521
Response dump(string|null $key = null)

Dump the content from the response.

Parameters

string|null $key

Return Value

Response

in Response at line 543
never dd(string|null $key = null)

Dump the content from the response and end the script.

Parameters

string|null $key

Return Value

never

in Response at line 553
Response dumpHeaders()

Dump the headers from the response.

Return Value

Response

in Response at line 563
never ddHeaders()

Dump the headers from the response and end the script.

Return Value

never

at line 163
bool offsetExists(string $offset)

Determine if the given offset exists.

Parameters

string $offset

Return Value

bool

at line 173
mixed offsetGet(string $offset)

Get the value for a given offset.

Parameters

string $offset

Return Value

mixed

in Response at line 597
void offsetSet(string $offset, mixed $value)

Set the value at the given offset.

Parameters

string $offset
mixed $value

Return Value

void

Exceptions

LogicException

in Response at line 609
void offsetUnset(string $offset)

Unset the value at the given offset.

Parameters

string $offset

Return Value

void

Exceptions

LogicException

in Response at line 617
static void flushState()

Flush all static state.

Return Value

void

in Response at line 626
string __toString()

Get the body of the response.

Return Value

string

in HasContext at line 19
HasContext withContext(array|string $key, mixed $value = null)

Set the API request/response context.

Parameters

array|string $key
mixed $value

Return Value

HasContext

in HasContext at line 35
mixed context(string|null $key = null, mixed $default = null)

Get the API request/response context.

Parameters

string|null $key
mixed $default

Return Value

mixed

at line 24
static ApiResponse createFrom(Response $response)

Create an API response from an HTTP client response.

Parameters

Response $response

Return Value

ApiResponse

at line 41
ApiResponse withStatus(int $code, string $reasonPhrase = '')

Set the response status.

Parameters

int $code
string $reasonPhrase

Return Value

ApiResponse

at line 52
ApiResponse withProtocolVersion(string $version)

Set the response protocol version.

Parameters

string $version

Return Value

ApiResponse

at line 63
bool hasHeader(string $name)

Determine whether the response has a header.

Parameters

string $name

Return Value

bool

at line 72
ApiResponse withHeader(string $name, mixed $value)

Set a response header.

Parameters

string $name
mixed $value

Return Value

ApiResponse

at line 83
ApiResponse withAddedHeader(string $name, mixed $value)

Add a value to a response header.

Parameters

string $name
mixed $value

Return Value

ApiResponse

at line 94
ApiResponse withoutHeader(string $name)

Remove a response header.

Parameters

string $name

Return Value

ApiResponse

at line 105
ApiResponse withBody(StreamInterface $body)

Set the response body.

Parameters

StreamInterface $body

Return Value

ApiResponse

at line 123
array toArray()

Transform the response into an array.

Return Value

array

Exceptions

InvalidResourceDataException
JsonException

at line 141
protected bool hasEmptyOrNullJsonBody()

Determine whether the body is empty or contains JSON null.

Return Value

bool