Browse Source

Fix #18812: Added error messages and optimized "error" methods in `yii\helpers\BaseJson`

tags/2.0.44
Anton 3 years ago committed by GitHub
parent
commit
932806b97d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/CHANGELOG.md
  2. 48
      framework/helpers/BaseJson.php

1
framework/CHANGELOG.md

@ -4,6 +4,7 @@ Yii Framework 2 Change Log
2.0.44 under development 2.0.44 under development
------------------------ ------------------------
- Enh #18812: Added error messages and optimized "error" methods in `yii\helpers\BaseJson` (WinterSilence, samdark)
- Chg #18823: Rollback changes #18806 in `yii\validators\ExistValidator::checkTargetRelationExistence()` (WinterSilence) - Chg #18823: Rollback changes #18806 in `yii\validators\ExistValidator::checkTargetRelationExistence()` (WinterSilence)
- Enh #18826: Add ability to turn the sorting off for a clicked column in GridView with multisort (ditibal) - Enh #18826: Add ability to turn the sorting off for a clicked column in GridView with multisort (ditibal)
- Bug #18646: Remove stale identity data from session if `IdentityInterface::findIdentity()` returns `null` (mikehaertl) - Bug #18646: Remove stale identity data from session if `IdentityInterface::findIdentity()` returns `null` (mikehaertl)

48
framework/helpers/BaseJson.php

@ -28,7 +28,7 @@ class BaseJson
* In case `prettyPrint` is `null` (default) the `options` passed to `encode` functions will not be changed. * In case `prettyPrint` is `null` (default) the `options` passed to `encode` functions will not be changed.
* @since 2.0.43 * @since 2.0.43
*/ */
public static $prettyPrint = null; public static $prettyPrint;
/** /**
* @var bool Avoids objects with zero-indexed keys to be encoded as array * @var bool Avoids objects with zero-indexed keys to be encoded as array
@ -41,19 +41,16 @@ class BaseJson
public static $keepObjectType = false; public static $keepObjectType = false;
/** /**
* List of JSON Error messages assigned to constant names for better handling of version differences. * @var array List of JSON Error messages assigned to constant names for better handling of PHP <= 5.5.
* @var array
* @since 2.0.7 * @since 2.0.7
*/ */
public static $jsonErrorMessages = [ public static $jsonErrorMessages = [
'JSON_ERROR_DEPTH' => 'The maximum stack depth has been exceeded.', 'JSON_ERROR_SYNTAX' => 'Syntax error',
'JSON_ERROR_STATE_MISMATCH' => 'Invalid or malformed JSON.', 'JSON_ERROR_UNSUPPORTED_TYPE' => 'Type is not supported',
'JSON_ERROR_CTRL_CHAR' => 'Control character error, possibly incorrectly encoded.', 'JSON_ERROR_DEPTH' => 'The maximum stack depth has been exceeded',
'JSON_ERROR_SYNTAX' => 'Syntax error.', 'JSON_ERROR_STATE_MISMATCH' => 'Invalid or malformed JSON',
'JSON_ERROR_UTF8' => 'Malformed UTF-8 characters, possibly incorrectly encoded.', // PHP 5.3.3 'JSON_ERROR_CTRL_CHAR' => 'Control character error, possibly incorrectly encoded',
'JSON_ERROR_RECURSION' => 'One or more recursive references in the value to be encoded.', // PHP 5.5.0 'JSON_ERROR_UTF8' => 'Malformed UTF-8 characters, possibly incorrectly encoded',
'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
]; ];
@ -137,7 +134,7 @@ class BaseJson
/** /**
* Handles [[encode()]] and [[decode()]] errors by throwing exceptions with the respective error message. * 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. * @throws InvalidArgumentException if there is any encoding/decoding error.
* @since 2.0.6 * @since 2.0.6
*/ */
@ -147,15 +144,14 @@ class BaseJson
return; return;
} }
$availableErrors = []; if (PHP_VERSION_ID >= 50500) {
foreach (static::$jsonErrorMessages as $const => $message) { throw new InvalidArgumentException(json_last_error_msg(), $lastError);
if (defined($const)) {
$availableErrors[constant($const)] = $message;
}
} }
if (isset($availableErrors[$lastError])) { foreach (static::$jsonErrorMessages as $const => $message) {
throw new InvalidArgumentException($availableErrors[$lastError], $lastError); if (defined($const) && constant($const) === $lastError) {
throw new InvalidArgumentException($message, $lastError);
}
} }
throw new InvalidArgumentException('Unknown JSON encoding/decoding error.'); throw new InvalidArgumentException('Unknown JSON encoding/decoding error.');
@ -230,6 +226,7 @@ class BaseJson
/** /**
* Generates a summary of the validation errors. * Generates a summary of the validation errors.
*
* @param Model|Model[] $models the model(s) whose validation errors are to be displayed. * @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: * @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); $showAllErrors = ArrayHelper::remove($options, 'showAllErrors', false);
$lines = self::collectErrors($models, $showAllErrors); $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 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. * only the first error message for each attribute will be shown.
* @return array of the validation errors * @return array of the validation errors
* @since 2.0.14 * @since 2.0.14
@ -258,14 +256,14 @@ class BaseJson
private static function collectErrors($models, $showAllErrors) private static function collectErrors($models, $showAllErrors)
{ {
$lines = []; $lines = [];
if (!is_array($models)) { if (!is_array($models)) {
$models = [$models]; $models = [$models];
} }
foreach ($models as $model) { 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));
} }
} }

Loading…
Cancel
Save