class ApiResponse extends Response

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected mixed $decoded

The decoded JSON response.

from  Response
protected bool $hasDecoded

Whether the response body has been decoded.

from  Response
protected int $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 int $defaultJsonDecodingFlags

The flags passed to json_decode by default.

from  Response
protected array $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
mixed
json(string|null $key = null, mixed $default = null, int|null $flags = null)

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

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

Get the JSON decoded 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 JSON decoded body of the response as a collection.

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

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

from  Response
resource
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
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
throw()

Throw an exception if a server or client error occurred.

from  Response
throwIf(bool|Closure $condition)

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

from  Response
throwUnless(bool|Closure $condition)

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

from  Response
throwIfStatus(callable|int $statusCode)

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

from  Response
throwUnlessStatus(callable|int $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
void
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.

from  Response
mixed
offsetGet(string $offset)

Get the value for a given offset.

from  Response
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(string|array $key, mixed $value = null)

Set the API request/response context.

mixed
context(string|null $key = null)

Get the API request/response context.

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

No description

withProtocolVersion(string $version)

No description

bool
hasHeader(string $name)

No description

withHeader(string $name, mixed $value)

No description

withAddedHeader(string $name, mixed $value)

No description

withoutHeader(string $name)

No description

withBody(StreamInterface $body)

No description

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 15
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 75
__construct(ResponseInterface $response)

Create a new response instance.

Parameters

ResponseInterface $response

in Response at line 82
string body()

Get the body of the response.

Return Value

string

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

Get the JSON decoded 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 121
array|object|null object(int|null $flags = null)

Get the JSON decoded 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.

Parameters

int|null $flags

Return Value

array|object|null

in Response at line 137
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: When true, the decoder should return an array of objects.

Parameters

Closure|null $callback

Return Value

Response

in Response at line 150
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 162
Collection collect(string|null $key = null, int|null $flags = null)

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

Parameters

string|null $key
int|null $flags

Return Value

Collection

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

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

Parameters

string|null $key
int|null $flags

Return Value

Fluent

in Response at line 182
resource resource()

Get the body of the response as a PHP resource.

Return Value

resource

Exceptions

InvalidArgumentException

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

Get a header from the response.

Parameters

string $header

Return Value

string

in Response at line 198
array headers()

Get the headers from the response.

Return Value

array

in Response at line 206
int status()

Get the status code of the response.

Return Value

int

in Response at line 214
string reason()

Get the reason phrase of the response.

Return Value

string

in Response at line 222
UriInterface|null effectiveUri()

Get the effective URI of the response.

Return Value

UriInterface|null

in Response at line 230
bool successful()

Determine if the request was successful.

Return Value

bool

in Response at line 238
bool redirect()

Determine if the response was a redirect.

Return Value

bool

in Response at line 246
bool failed()

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

Return Value

bool

in Response at line 254
bool clientError()

Determine if the response indicates a client error occurred.

Return Value

bool

in Response at line 262
bool serverError()

Determine if the response indicates a server error occurred.

Return Value

bool

in Response at line 270
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 282
CookieJar cookies()

Get the response cookies.

Return Value

CookieJar

in Response at line 290
array handlerStats()

Get the handler stats of the response.

Return Value

array

in Response at line 298
Response close()

Close the stream and any underlying resources.

Return Value

Response

in Response at line 308
ResponseInterface toPsrResponse()

Get the underlying PSR response for the response.

Return Value

ResponseInterface

in Response at line 316
RequestException|null toException()

Create an exception if a server or client error occurred.

Return Value

RequestException|null

in Response at line 330
Response throw()

Throw an exception if a server or client error occurred.

Return Value

Response

Exceptions

RequestException

in Response at line 350
Response throwIf(bool|Closure $condition)

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

Parameters

bool|Closure $condition

Return Value

Response

Exceptions

RequestException

in Response at line 360
Response throwUnless(bool|Closure $condition)

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

Parameters

bool|Closure $condition

Return Value

Response

Exceptions

RequestException

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

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

Parameters

callable|int $statusCode

Return Value

Response

Exceptions

RequestException

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

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

Parameters

callable|int $statusCode

Return Value

Response

Exceptions

RequestException

in Response at line 403
Response throwIfClientError()

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

Return Value

Response

Exceptions

RequestException

in Response at line 413
Response throwIfServerError()

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

Return Value

Response

Exceptions

RequestException

in Response at line 421
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 431
Response dontTruncateExceptions()

Indicate that request exceptions should not be truncated.

Return Value

Response

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

Dump the content from the response.

Parameters

string|null $key

Return Value

Response

in Response at line 467
void dd(string|null $key = null)

Dump the content from the response and end the script.

Parameters

string|null $key

Return Value

void

in Response at line 477
Response dumpHeaders()

Dump the headers from the response.

Return Value

Response

in Response at line 489
never ddHeaders()

Dump the headers from the response and end the script.

Return Value

never

in Response at line 501
bool offsetExists(string $offset)

Determine if the given offset exists.

Parameters

string $offset

Return Value

bool

in Response at line 511
mixed offsetGet(string $offset)

Get the value for a given offset.

Parameters

string $offset

Return Value

mixed

in Response at line 523
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 535
void offsetUnset(string $offset)

Unset the value at the given offset.

Parameters

string $offset

Return Value

void

Exceptions

LogicException

in Response at line 543
static void flushState()

Flush all static state.

Return Value

void

in Response at line 554
string __toString()

Get the body of the response.

Return Value

string

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

Set the API request/response context.

Parameters

string|array $key
mixed $value

Return Value

HasContext

in HasContext at line 30
mixed context(string|null $key = null)

Get the API request/response context.

Parameters

string|null $key

Return Value

mixed

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

No description

Parameters

int $code
string $reasonPhrase

Return Value

ApiResponse

at line 22
ApiResponse withProtocolVersion(string $version)

No description

Parameters

string $version

Return Value

ApiResponse

at line 30
bool hasHeader(string $name)

No description

Parameters

string $name

Return Value

bool

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

No description

Parameters

string $name
mixed $value

Return Value

ApiResponse

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

No description

Parameters

string $name
mixed $value

Return Value

ApiResponse

at line 52
ApiResponse withoutHeader(string $name)

No description

Parameters

string $name

Return Value

ApiResponse

at line 60
ApiResponse withBody(StreamInterface $body)

No description

Parameters

StreamInterface $body

Return Value

ApiResponse