Sledgehammer Framework

Json extends Base
in package

Renders the data as Json Compatible with MVC's the Document/View inferface.

Table of Contents

$headers  : array<string|int, mixed>
The (HTTP) headers, changes Content-Type to "application/json".
$data  : mixed
UTF-8 encoded data.
__call()  : mixed
Report that the $method doesn't exist.
__callStatic()  : mixed
Report that the $method doesn't exist.
__construct()  : mixed
Constructor.
__get()  : mixed
Report that $property doesn't exist.
__set()  : mixed
Report that $property doesn't exist and set the property to the given $value.
__toString()  : string
Return the $this->data as json formatted string.
decode()  : mixed
Takes a JSON encoded string and converts it into a PHP variable.
encode()  : string
Returns a string containing the JSON representation of value.
error()  : Json
Reports the error/exception to the ErrorHandler and returns the error as Json object.
getHeaders()  : mixed
The (HTTP) headers.
isDocument()  : true
Render as a standalone document.
render()  : mixed
Render the $data as json.
success()  : Json
Short for "new Json(array('success' => true))".
convertToUTF8()  : mixed
Return a data-structure where all string are UTF-8 encoded.
errorMessage()  : string
Translates a json error into a human readable error.

Properties

$headers

The (HTTP) headers, changes Content-Type to "application/json".

public array<string|int, mixed> $headers

$data

UTF-8 encoded data.

private mixed $data

Methods

__call()

Report that the $method doesn't exist.

public __call(string $method, array<string|int, mixed> $arguments) : mixed
Parameters
$method : string
$arguments : array<string|int, mixed>
Return values
mixed

__callStatic()

Report that the $method doesn't exist.

public static __callStatic(string $method, array<string|int, mixed> $arguments) : mixed
Parameters
$method : string
$arguments : array<string|int, mixed>
Return values
mixed

__construct()

Constructor.

public __construct(mixed $data[, array<string|int, mixed> $headers = [] ][, string $dataCharset = 'UTF-8' ]) : mixed
Parameters
$data : mixed

The data to be sent as json.

$headers : array<string|int, mixed> = []

The (HTTP) headers

$dataCharset : string = 'UTF-8'

The encoding used in $data, use null for autodetection. Assume UTF-8 by default.

Return values
mixed

__get()

Report that $property doesn't exist.

public __get(string $property) : mixed
Parameters
$property : string
Return values
mixed

__set()

Report that $property doesn't exist and set the property to the given $value.

public __set(string $property, mixed $value) : mixed
Parameters
$property : string
$value : mixed
Return values
mixed

__toString()

Return the $this->data as json formatted string.

public __toString() : string

Allow echo $json;.

Return values
string

decode()

Takes a JSON encoded string and converts it into a PHP variable.

public static decode(string $json[, bool $assoc = false ][, int $depth = 512 ], int $options) : mixed
Parameters
$json : string

JSON formatted string

$assoc : bool = false
$depth : int = 512
$options : int

Optional bitmask, with flags: JSON_BIGINT_AS_STRING

Tags
throws
Exception
Return values
mixed

data

encode()

Returns a string containing the JSON representation of value.

public static encode(mixed $data[, int $options = null ]) : string
Parameters
$data : mixed
$options : int = null

Optional bitmask, with flags: JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, JSON_NUMERIC_CHECK, JSON_PRETTY_PRINT, JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT and/or JSON_UNESCAPED_UNICODE.

Tags
throws
Exception
Return values
string

JSON formatted string

error()

Reports the error/exception to the ErrorHandler and returns the error as Json object.

public static error(string|Exception $error[, int $http = 400 ]) : Json

The javascript client should detect and report the error to the user: if (result.success !== true) { alert(result.error); }.

Parameters
$error : string|Exception

The error message or Exception

$http : int = 400

The HTTP status code (defaults to 400 Bad Request)

Return values
Json

getHeaders()

The (HTTP) headers.

public getHeaders() : mixed
Return values
mixed

isDocument()

Render as a standalone document.

public isDocument() : true
Return values
true

render()

Render the $data as json.

public render() : mixed
Return values
mixed

success()

Short for "new Json(array('success' => true))".

public static success([mixed $data = null ]) : Json
Parameters
$data : mixed = null

[optional] The data payload

Return values
Json

convertToUTF8()

Return a data-structure where all string are UTF-8 encoded.

private convertToUTF8(mixed $data, string|null $charset) : mixed
Parameters
$data : mixed

The non UTF-8 encoded data

$charset : string|null

The from_encoding, Use null for autodetection

Return values
mixed

UTF8 encoded data

errorMessage()

Translates a json error into a human readable error.

private static errorMessage(int $errno) : string
Parameters
$errno : int

JSON_ERROR_*

Return values
string

Search results