1 | <?php |
||
23 | class BaseJson |
||
24 | { |
||
25 | /** |
||
26 | * List of JSON Error messages assigned to constant names for better handling of version differences. |
||
27 | * @var array |
||
28 | * @since 2.0.7 |
||
29 | */ |
||
30 | public static $jsonErrorMessages = [ |
||
31 | 'JSON_ERROR_DEPTH' => 'The maximum stack depth has been exceeded.', |
||
32 | 'JSON_ERROR_STATE_MISMATCH' => 'Invalid or malformed JSON.', |
||
33 | 'JSON_ERROR_CTRL_CHAR' => 'Control character error, possibly incorrectly encoded.', |
||
34 | 'JSON_ERROR_SYNTAX' => 'Syntax error.', |
||
35 | 'JSON_ERROR_UTF8' => 'Malformed UTF-8 characters, possibly incorrectly encoded.', // PHP 5.3.3 |
||
36 | 'JSON_ERROR_RECURSION' => 'One or more recursive references in the value to be encoded.', // PHP 5.5.0 |
||
37 | 'JSON_ERROR_INF_OR_NAN' => 'One or more NAN or INF values in the value to be encoded', // PHP 5.5.0 |
||
38 | 'JSON_ERROR_UNSUPPORTED_TYPE' => 'A value of a type that cannot be encoded was given', // PHP 5.5.0 |
||
39 | ]; |
||
40 | |||
41 | |||
42 | /** |
||
43 | * Encodes the given value into a JSON string. |
||
44 | * |
||
45 | * The method enhances `json_encode()` by supporting JavaScript expressions. |
||
46 | * In particular, the method will not encode a JavaScript expression that is |
||
47 | * represented in terms of a [[JsExpression]] object. |
||
48 | * |
||
49 | * Note that data encoded as JSON must be UTF-8 encoded according to the JSON specification. |
||
50 | * You must ensure strings passed to this method have proper encoding before passing them. |
||
51 | * |
||
52 | * @param mixed $value the data to be encoded. |
||
53 | * @param int $options the encoding options. For more details please refer to |
||
54 | * <http://www.php.net/manual/en/function.json-encode.php>. Default is `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE`. |
||
55 | * @return string the encoding result. |
||
56 | * @throws InvalidParamException if there is any encoding error. |
||
57 | */ |
||
58 | 32 | public static function encode($value, $options = 320) |
|
71 | |||
72 | /** |
||
73 | * Encodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code. |
||
74 | * |
||
75 | * The method enhances `json_encode()` by supporting JavaScript expressions. |
||
76 | * In particular, the method will not encode a JavaScript expression that is |
||
77 | * represented in terms of a [[JsExpression]] object. |
||
78 | * |
||
79 | * Note that data encoded as JSON must be UTF-8 encoded according to the JSON specification. |
||
80 | * You must ensure strings passed to this method have proper encoding before passing them. |
||
81 | * |
||
82 | * @param mixed $value the data to be encoded |
||
83 | * @return string the encoding result |
||
84 | * @since 2.0.4 |
||
85 | * @throws InvalidParamException if there is any encoding error |
||
86 | */ |
||
87 | 9 | public static function htmlEncode($value) |
|
91 | |||
92 | /** |
||
93 | * Decodes the given JSON string into a PHP data structure. |
||
94 | * @param string $json the JSON string to be decoded |
||
95 | * @param bool $asArray whether to return objects in terms of associative arrays. |
||
96 | * @return mixed the PHP data |
||
97 | * @throws InvalidParamException if there is any decoding error |
||
98 | */ |
||
99 | 11 | public static function decode($json, $asArray = true) |
|
111 | |||
112 | /** |
||
113 | * Validates the given data and returns a boolean indicating whether |
||
114 | * it is a valid JSON string or not. |
||
115 | * |
||
116 | * @param mixed $data data to validate. |
||
117 | * @param int $error error which occurred while validation as returned from `json_last_error()`. |
||
118 | * `JSON_ERROR_SYNTAX` if `$data` is not a string. `JSON_ERROR_NONE` if `$data` is a valid JSON string. |
||
119 | * @return boolean `true` if `$data` is a valid JSON string, `false` otherwise. |
||
120 | * |
||
121 | * @see http://php.net/manual/en/function.json-last-error.php |
||
122 | * @since 2.0.14 |
||
123 | */ |
||
124 | 44 | public static function validate($data, &$error = null) |
|
125 | { |
||
126 | 44 | $isValid = false; |
|
127 | |||
128 | 44 | if (is_string($data) === false) { |
|
129 | 14 | $error = JSON_ERROR_SYNTAX; |
|
130 | } else { |
||
131 | 30 | json_decode($data); |
|
132 | 30 | $error = json_last_error(); |
|
133 | 30 | if ($error === JSON_ERROR_NONE) { |
|
134 | 22 | $isValid = true; |
|
135 | } |
||
136 | } |
||
137 | |||
138 | 44 | return $isValid; |
|
139 | } |
||
140 | |||
141 | /** |
||
142 | * Handles [[encode()]] and [[decode()]] errors by throwing exceptions with the respective error message. |
||
143 | * |
||
144 | * @param int $lastError error code from [json_last_error()](http://php.net/manual/en/function.json-last-error.php). |
||
145 | * @throws \yii\base\InvalidParamException if there is any encoding/decoding error. |
||
146 | * @since 2.0.6 |
||
147 | */ |
||
148 | 41 | protected static function handleJsonError($lastError) |
|
167 | |||
168 | /** |
||
169 | * Pre-processes the data before sending it to `json_encode()`. |
||
170 | * @param mixed $data the data to be processed |
||
171 | * @param array $expressions collection of JavaScript expressions |
||
172 | * @param string $expPrefix a prefix internally used to handle JS expressions |
||
173 | * @return mixed the processed data |
||
174 | */ |
||
175 | 32 | protected static function processData($data, &$expressions, $expPrefix) |
|
212 | |||
213 | /** |
||
214 | * Generates a summary of the validation errors. |
||
215 | * @param Model|Model[] $models the model(s) whose validation errors are to be displayed. |
||
216 | * @param array $options the tag options in terms of name-value pairs. The following options are specially handled: |
||
217 | * |
||
218 | * - showAllErrors: boolean, if set to true every error message for each attribute will be shown otherwise |
||
219 | * only the first error message for each attribute will be shown. Defaults to `false`. |
||
220 | * |
||
221 | * @return string the generated error summary |
||
222 | * @since 2.0.14 |
||
223 | */ |
||
224 | 1 | public static function errorSummary($models, $options = []) |
|
231 | |||
232 | /** |
||
233 | * Return array of the validation errors |
||
234 | * @param Model|Model[] $models the model(s) whose validation errors are to be displayed. |
||
235 | * @param $showAllErrors boolean, if set to true every error message for each attribute will be shown otherwise |
||
236 | * only the first error message for each attribute will be shown. |
||
237 | * @return array of the validation errors |
||
238 | * @since 2.0.14 |
||
239 | */ |
||
240 | 1 | private static function collectErrors($models, $showAllErrors) |
|
253 | } |
||
254 |