BaseJson
in package
BaseJson provides concrete implementation for [[Json]].
Do not use BaseJson. Use [[Json]] instead.
Tags
Table of Contents
Properties
- $jsonErrorMessages : array<string|int, mixed>
- $keepObjectType : bool
- $prettyPrint : bool|null
Methods
- decode() : mixed
- Decodes the given JSON string into a PHP data structure.
- encode() : string
- Encodes the given value into a JSON string.
- errorSummary() : string
- Generates a summary of the validation errors.
- htmlEncode() : string
- Encodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code.
- handleJsonError() : mixed
- Handles [[encode()]] and [[decode()]] errors by throwing exceptions with the respective error message.
- processData() : mixed
- Pre-processes the data before sending it to `json_encode()`.
- collectErrors() : array<string|int, mixed>
- Return array of the validation errors.
Properties
$jsonErrorMessages
public
static array<string|int, mixed>
$jsonErrorMessages
= ['JSON_ERROR_SYNTAX' => 'Syntax error', 'JSON_ERROR_UNSUPPORTED_TYPE' => 'Type is not supported', 'JSON_ERROR_DEPTH' => 'The maximum stack depth has been exceeded', 'JSON_ERROR_STATE_MISMATCH' => 'Invalid or malformed JSON', 'JSON_ERROR_CTRL_CHAR' => 'Control character error, possibly incorrectly encoded', 'JSON_ERROR_UTF8' => 'Malformed UTF-8 characters, possibly incorrectly encoded']
List of JSON Error messages assigned to constant names for better handling of PHP <= 5.5.
Tags
$keepObjectType
public
static bool
$keepObjectType
= false
Avoids objects with zero-indexed keys to be encoded as array
Json::encode((object)['test'])
will be encoded as an object not as an array. This matches the behaviour of json_encode()
.
Defaults to false to avoid any backwards compatibility issues.
Enable for single purpose: Json::$keepObjectType = true;
Tags
$prettyPrint
public
static bool|null
$prettyPrint
Enables human readable output a.k.a. Pretty Print.
This can useful for debugging during development but is not recommended in a production environment!
In case prettyPrint
is null
(default) the options
passed to encode
functions will not be changed.
Tags
Methods
decode()
Decodes the given JSON string into a PHP data structure.
public
static decode(string $json[, bool $asArray = true ]) : mixed
Parameters
- $json : string
-
the JSON string to be decoded
- $asArray : bool = true
-
whether to return objects in terms of associative arrays.
Tags
Return values
mixed —the PHP data
encode()
Encodes the given value into a JSON string.
public
static encode(mixed $value[, int $options = 320 ]) : string
The method enhances json_encode()
by supporting JavaScript expressions.
In particular, the method will not encode a JavaScript expression that is
represented in terms of a [[JsExpression]] object.
Note that data encoded as JSON must be UTF-8 encoded according to the JSON specification. You must ensure strings passed to this method have proper encoding before passing them.
Parameters
- $value : mixed
-
the data to be encoded.
- $options : int = 320
-
the encoding options. For more details please refer to https://www.php.net/manual/en/function.json-encode.php. Default is
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE
.
Tags
Return values
string —the encoding result.
errorSummary()
Generates a summary of the validation errors.
public
static errorSummary(Model|array<string|int, Model> $models[, array<string|int, mixed> $options = [] ]) : string
Parameters
- $models : Model|array<string|int, Model>
-
the model(s) whose validation errors are to be displayed.
- $options : array<string|int, mixed> = []
-
the tag options in terms of name-value pairs. The following options are specially handled:
- showAllErrors: boolean, if set to true every error message for each attribute will be shown otherwise
only the first error message for each attribute will be shown. Defaults to
false
.
- showAllErrors: boolean, if set to true every error message for each attribute will be shown otherwise
only the first error message for each attribute will be shown. Defaults to
Tags
Return values
string —the generated error summary
htmlEncode()
Encodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code.
public
static htmlEncode(mixed $value) : string
The method enhances json_encode()
by supporting JavaScript expressions.
In particular, the method will not encode a JavaScript expression that is
represented in terms of a [[JsExpression]] object.
Note that data encoded as JSON must be UTF-8 encoded according to the JSON specification. You must ensure strings passed to this method have proper encoding before passing them.
Parameters
- $value : mixed
-
the data to be encoded
Tags
Return values
string —the encoding result
handleJsonError()
Handles [[encode()]] and [[decode()]] errors by throwing exceptions with the respective error message.
protected
static handleJsonError(int $lastError) : mixed
Parameters
- $lastError : int
-
error code from json_last_error().
Tags
processData()
Pre-processes the data before sending it to `json_encode()`.
protected
static processData(mixed $data, array<string|int, mixed> &$expressions, string $expPrefix) : mixed
Parameters
- $data : mixed
-
the data to be processed
- $expressions : array<string|int, mixed>
-
collection of JavaScript expressions
- $expPrefix : string
-
a prefix internally used to handle JS expressions
Return values
mixed —the processed data
collectErrors()
Return array of the validation errors.
private
static collectErrors(Model|array<string|int, Model> $models, bool $showAllErrors) : array<string|int, mixed>
Parameters
- $models : Model|array<string|int, Model>
-
the model(s) whose validation errors are to be displayed.
- $showAllErrors : bool
-
if set to true every error message for each attribute will be shown otherwise only the first error message for each attribute will be shown.
Tags
Return values
array<string|int, mixed> —of the validation errors