trait JsonResponse

A trait that support operations related to APIs and their associated JSON responses.

Properties

static string|null $rawInputOverride

Raw JSON override for tests

static bool $testing

When true, do not send headers or exit; just echo JSON and return.

static int $lastStatus

Captured status for tests (optional convenience).

static array $lastHeaders

Captured headers for tests (optional convenience).

Methods

bool
apiCsrfCheck()

Checks if CSRF token has been tampered with.

array|string
get(string|null $input = null)

Supports operations related to handling POST and GET requests.

void
jsonError(string $message, int $status = 400, array $errors = [])

Makes JSON Response for error payloads.

void
jsonResponse(mixed $data, int $status = 200, array $extraHeaders = [])

Sends a JSON response with headers and status code.

void
preflight()

Respond to CORS preflight.

static void
setRawInputOverride(mixed $payload = null)

Inject JSON body by providing payload parameter. Also performs cleanup to avoid leaking into other tests.

Details

at line 47
bool apiCsrfCheck()

Checks if CSRF token has been tampered with.

Return Value

bool

True if token is valid, otherwise we return false.

at line 62
array|string get(string|null $input = null)

Supports operations related to handling POST and GET requests.

Similar in behavior to the get function from the Input class but for JSON related operations.

Parameters

string|null $input

Field name from POST/GET request, or null to get all

Return Value

array|string

Sanitized input as array or string

at line 97
void jsonError(string $message, int $status = 400, array $errors = [])

Makes JSON Response for error payloads.

Parameters

string $message

The error message.

int $status

The status code.

array $errors

The array of errors.

Return Value

void

at line 112
void jsonResponse(mixed $data, int $status = 200, array $extraHeaders = [])

Sends a JSON response with headers and status code.

Parameters

mixed $data

The JSON response.

int $status

The status code.

array $extraHeaders

Any extra headers.

Return Value

void

at line 161
void preflight()

Respond to CORS preflight.

Return Value

void

at line 177
static void setRawInputOverride(mixed $payload = null)

Inject JSON body by providing payload parameter. Also performs cleanup to avoid leaking into other tests.

Parameters

mixed $payload

The JSON payload or null.

Return Value

void