class Response implements ArrayAccess (View source)

Traits

Properties

static protected array $macros

The registered string macros.

from  Macroable
protected ResponseInterface $response

The underlying PSR response.

protected array $decoded

The decoded JSON response.

CookieJar $cookies

The request cookies.

TransferStats|null $transferStats

The transfer stats for the request.

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
unprocessableEntity()

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

bool
tooManyRequests()

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

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

Register a custom macro.

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

Mix another object into the class.

static bool
hasMacro(string $name)

Checks 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 proxy other methods to the underlying response.

void
__construct(MessageInterface $response)

Create a new response instance.

string
body()

Get the body of the response.

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

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

object|null
object()

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

collect(string|null $key = null)

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

string
header(string $header)

Get a header from the response.

array
headers()

Get the headers from the response.

int
status()

Get the status code of the response.

string
reason()

Get the reason phrase of the response.

UriInterface|null
effectiveUri()

Get the effective URI of the response.

bool
successful()

Determine if the request was successful.

bool
redirect()

Determine if the response was a redirect.

bool
failed()

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

bool
clientError()

Determine if the response indicates a client error occurred.

bool
serverError()

Determine if the response indicates a server error occurred.

$this
onError(callable $callback)

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

CookieJar
cookies()

Get the response cookies.

array
handlerStats()

Get the handler stats of the response.

$this
close()

Close the stream and any underlying resources.

ResponseInterface
toPsrResponse()

Get the underlying PSR response for the response.

toException()

Create an exception if a server or client error occurred.

$this
throw()

Throw an exception if a server or client error occurred.

$this
throwIf(Closure|bool $condition)

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

$this
throwIfStatus(callable|int $statusCode)

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

$this
throwUnlessStatus(callable|int $statusCode)

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

$this
throwIfClientError()

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

$this
throwIfServerError()

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

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.

void
offsetUnset(string $offset)

Unset the value at the given offset.

string
__toString()

Get the body of the response.

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

bool notFound()

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

Return Value

bool

bool requestTimeout()

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

Return Value

bool

bool conflict()

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

Return Value

bool

bool unprocessableEntity()

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

Return Value

bool

bool tooManyRequests()

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

Return Value

bool

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

Register a custom macro.

Parameters

string $name
object|callable $macro

Return Value

void

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

Mix another object into the class.

Parameters

object $mixin
bool $replace

Return Value

void

Exceptions

ReflectionException

static bool hasMacro(string $name)

Checks if macro is registered.

Parameters

string $name

Return Value

bool

static void flushMacros()

Flush the existing macros.

Return Value

void

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

Dynamically handle calls to the class.

Parameters

string $method
array $parameters

Return Value

mixed

Exceptions

BadMethodCallException

mixed __call(string $method, array $parameters)

Dynamically proxy other methods to the underlying response.

Parameters

string $method
array $parameters

Return Value

mixed

void __construct(MessageInterface $response)

Create a new response instance.

Parameters

MessageInterface $response

Return Value

void

string body()

Get the body of the response.

Return Value

string

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

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

Parameters

string|null $key
mixed $default

Return Value

mixed

object|null object()

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

Return Value

object|null

Collection collect(string|null $key = null)

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

Parameters

string|null $key

Return Value

Collection

string header(string $header)

Get a header from the response.

Parameters

string $header

Return Value

string

array headers()

Get the headers from the response.

Return Value

array

int status()

Get the status code of the response.

Return Value

int

string reason()

Get the reason phrase of the response.

Return Value

string

UriInterface|null effectiveUri()

Get the effective URI of the response.

Return Value

UriInterface|null

bool successful()

Determine if the request was successful.

Return Value

bool

bool redirect()

Determine if the response was a redirect.

Return Value

bool

bool failed()

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

Return Value

bool

bool clientError()

Determine if the response indicates a client error occurred.

Return Value

bool

bool serverError()

Determine if the response indicates a server error occurred.

Return Value

bool

$this onError(callable $callback)

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

Parameters

callable $callback

Return Value

$this

CookieJar cookies()

Get the response cookies.

Return Value

CookieJar

array handlerStats()

Get the handler stats of the response.

Return Value

array

$this close()

Close the stream and any underlying resources.

Return Value

$this

ResponseInterface toPsrResponse()

Get the underlying PSR response for the response.

Return Value

ResponseInterface

RequestException|null toException()

Create an exception if a server or client error occurred.

Return Value

RequestException|null

$this throw()

Throw an exception if a server or client error occurred.

Return Value

$this

Exceptions

RequestException

$this throwIf(Closure|bool $condition)

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

Parameters

Closure|bool $condition

Return Value

$this

Exceptions

RequestException

$this throwIfStatus(callable|int $statusCode)

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

Parameters

callable|int $statusCode

Return Value

$this

Exceptions

RequestException

$this throwUnlessStatus(callable|int $statusCode)

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

Parameters

callable|int $statusCode

Return Value

$this

Exceptions

RequestException

$this throwIfClientError()

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

Return Value

$this

Exceptions

RequestException

$this throwIfServerError()

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

Return Value

$this

Exceptions

RequestException

bool offsetExists(string $offset)

Determine if the given offset exists.

Parameters

string $offset

Return Value

bool

mixed offsetGet(string $offset)

Get the value for a given offset.

Parameters

string $offset

Return Value

mixed

void offsetSet(string $offset, mixed $value)

Set the value at the given offset.

Parameters

string $offset
mixed $value

Return Value

void

Exceptions

LogicException

void offsetUnset(string $offset)

Unset the value at the given offset.

Parameters

string $offset

Return Value

void

Exceptions

LogicException

string __toString()

Get the body of the response.

Return Value

string