|
|
|
@ -28,7 +28,7 @@ class BaseJson
|
|
|
|
|
* In case `prettyPrint` is `null` (default) the `options` passed to `encode` functions will not be changed. |
|
|
|
|
* @since 2.0.43 |
|
|
|
|
*/ |
|
|
|
|
public static $prettyPrint = null; |
|
|
|
|
public static $prettyPrint; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @var bool Avoids objects with zero-indexed keys to be encoded as array |
|
|
|
@ -41,19 +41,16 @@ class BaseJson
|
|
|
|
|
public static $keepObjectType = false; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* List of JSON Error messages assigned to constant names for better handling of version differences. |
|
|
|
|
* @var array |
|
|
|
|
* @var array List of JSON Error messages assigned to constant names for better handling of PHP <= 5.5. |
|
|
|
|
* @since 2.0.7 |
|
|
|
|
*/ |
|
|
|
|
public static $jsonErrorMessages = [ |
|
|
|
|
'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_SYNTAX' => 'Syntax error.', |
|
|
|
|
'JSON_ERROR_UTF8' => 'Malformed UTF-8 characters, possibly incorrectly encoded.', // PHP 5.3.3 |
|
|
|
|
'JSON_ERROR_RECURSION' => 'One or more recursive references in the value to be encoded.', // PHP 5.5.0 |
|
|
|
|
'JSON_ERROR_INF_OR_NAN' => 'One or more NAN or INF values in the value to be encoded', // PHP 5.5.0 |
|
|
|
|
'JSON_ERROR_UNSUPPORTED_TYPE' => 'A value of a type that cannot be encoded was given', // PHP 5.5.0 |
|
|
|
|
'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', |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -137,7 +134,7 @@ class BaseJson
|
|
|
|
|
/** |
|
|
|
|
* Handles [[encode()]] and [[decode()]] errors by throwing exceptions with the respective error message. |
|
|
|
|
* |
|
|
|
|
* @param int $lastError error code from [json_last_error()](https://secure.php.net/manual/en/function.json-last-error.php). |
|
|
|
|
* @param int $lastError error code from [json_last_error()](https://www.php.net/manual/en/function.json-last-error.php). |
|
|
|
|
* @throws InvalidArgumentException if there is any encoding/decoding error. |
|
|
|
|
* @since 2.0.6 |
|
|
|
|
*/ |
|
|
|
@ -147,15 +144,14 @@ class BaseJson
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$availableErrors = []; |
|
|
|
|
foreach (static::$jsonErrorMessages as $const => $message) { |
|
|
|
|
if (defined($const)) { |
|
|
|
|
$availableErrors[constant($const)] = $message; |
|
|
|
|
} |
|
|
|
|
if (PHP_VERSION_ID >= 50500) { |
|
|
|
|
throw new InvalidArgumentException(json_last_error_msg(), $lastError); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (isset($availableErrors[$lastError])) { |
|
|
|
|
throw new InvalidArgumentException($availableErrors[$lastError], $lastError); |
|
|
|
|
foreach (static::$jsonErrorMessages as $const => $message) { |
|
|
|
|
if (defined($const) && constant($const) === $lastError) { |
|
|
|
|
throw new InvalidArgumentException($message, $lastError); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
throw new InvalidArgumentException('Unknown JSON encoding/decoding error.'); |
|
|
|
@ -230,6 +226,7 @@ class BaseJson
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Generates a summary of the validation errors. |
|
|
|
|
* |
|
|
|
|
* @param Model|Model[] $models the model(s) whose validation errors are to be displayed. |
|
|
|
|
* @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
|
|
|
|
* |
|
|
|
@ -244,13 +241,14 @@ class BaseJson
|
|
|
|
|
$showAllErrors = ArrayHelper::remove($options, 'showAllErrors', false); |
|
|
|
|
$lines = self::collectErrors($models, $showAllErrors); |
|
|
|
|
|
|
|
|
|
return json_encode($lines); |
|
|
|
|
return static::encode($lines); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Return array of the validation errors |
|
|
|
|
* Return array of the validation errors. |
|
|
|
|
* |
|
|
|
|
* @param Model|Model[] $models the model(s) whose validation errors are to be displayed. |
|
|
|
|
* @param $showAllErrors boolean, if set to true every error message for each attribute will be shown otherwise |
|
|
|
|
* @param bool $showAllErrors 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. |
|
|
|
|
* @return array of the validation errors |
|
|
|
|
* @since 2.0.14 |
|
|
|
@ -258,14 +256,14 @@ class BaseJson
|
|
|
|
|
private static function collectErrors($models, $showAllErrors) |
|
|
|
|
{ |
|
|
|
|
$lines = []; |
|
|
|
|
|
|
|
|
|
if (!is_array($models)) { |
|
|
|
|
$models = [$models]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
foreach ($models as $model) { |
|
|
|
|
$lines = array_unique(array_merge($lines, $model->getErrorSummary($showAllErrors))); |
|
|
|
|
$lines[] = $model->getErrorSummary($showAllErrors); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return $lines; |
|
|
|
|
return array_unique(call_user_func_array('array_merge', $lines)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|