1 | <?php |
||
22 | class BaseJson |
||
23 | { |
||
24 | /** |
||
25 | * List of JSON Error messages assigned to constant names for better handling of version differences |
||
26 | * @var array |
||
27 | * @since 2.0.7 |
||
28 | */ |
||
29 | public static $jsonErrorMessages = [ |
||
30 | 'JSON_ERROR_DEPTH' => 'The maximum stack depth has been exceeded.', |
||
31 | 'JSON_ERROR_STATE_MISMATCH' => 'Invalid or malformed JSON.', |
||
32 | 'JSON_ERROR_CTRL_CHAR' => 'Control character error, possibly incorrectly encoded.', |
||
33 | 'JSON_ERROR_SYNTAX' => 'Syntax error.', |
||
34 | 'JSON_ERROR_UTF8' => 'Malformed UTF-8 characters, possibly incorrectly encoded.', // PHP 5.3.3 |
||
35 | 'JSON_ERROR_RECURSION' => 'One or more recursive references in the value to be encoded.', // PHP 5.5.0 |
||
36 | 'JSON_ERROR_INF_OR_NAN' => 'One or more NAN or INF values in the value to be encoded', // PHP 5.5.0 |
||
37 | 'JSON_ERROR_UNSUPPORTED_TYPE' => 'A value of a type that cannot be encoded was given', // PHP 5.5.0 |
||
38 | ]; |
||
39 | |||
40 | |||
41 | /** |
||
42 | * Encodes the given value into a JSON string. |
||
43 | * |
||
44 | * The method enhances `json_encode()` by supporting JavaScript expressions. |
||
45 | * In particular, the method will not encode a JavaScript expression that is |
||
46 | * represented in terms of a [[JsExpression]] object. |
||
47 | * |
||
48 | * Note that data encoded as JSON must be UTF-8 encoded according to the JSON specification. |
||
49 | * You must ensure strings passed to this method have proper encoding before passing them. |
||
50 | * |
||
51 | * @param mixed $value the data to be encoded. |
||
52 | * @param int $options the encoding options. For more details please refer to |
||
53 | * <http://www.php.net/manual/en/function.json-encode.php>. Default is `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE`. |
||
54 | * @return string the encoding result. |
||
55 | * @throws InvalidParamException if there is any encoding error. |
||
56 | */ |
||
57 | 27 | public static function encode($value, $options = 320) |
|
70 | |||
71 | /** |
||
72 | * Encodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code. |
||
73 | * |
||
74 | * The method enhances `json_encode()` by supporting JavaScript expressions. |
||
75 | * In particular, the method will not encode a JavaScript expression that is |
||
76 | * represented in terms of a [[JsExpression]] object. |
||
77 | * |
||
78 | * Note that data encoded as JSON must be UTF-8 encoded according to the JSON specification. |
||
79 | * You must ensure strings passed to this method have proper encoding before passing them. |
||
80 | * |
||
81 | * @param mixed $value the data to be encoded |
||
82 | * @return string the encoding result |
||
83 | * @since 2.0.4 |
||
84 | * @throws InvalidParamException if there is any encoding error |
||
85 | */ |
||
86 | 4 | public static function htmlEncode($value) |
|
90 | |||
91 | /** |
||
92 | * Decodes the given JSON string into a PHP data structure. |
||
93 | * @param string $json the JSON string to be decoded |
||
94 | * @param bool $asArray whether to return objects in terms of associative arrays. |
||
95 | * @return mixed the PHP data |
||
96 | * @throws InvalidParamException if there is any decoding error |
||
97 | */ |
||
98 | 10 | public static function decode($json, $asArray = true) |
|
110 | |||
111 | /** |
||
112 | * Handles [[encode()]] and [[decode()]] errors by throwing exceptions with the respective error message. |
||
113 | * |
||
114 | * @param int $lastError error code from [json_last_error()](http://php.net/manual/en/function.json-last-error.php). |
||
115 | * @throws \yii\base\InvalidParamException if there is any encoding/decoding error. |
||
116 | * @since 2.0.6 |
||
117 | */ |
||
118 | 36 | protected static function handleJsonError($lastError) |
|
137 | |||
138 | /** |
||
139 | * Pre-processes the data before sending it to `json_encode()`. |
||
140 | * @param mixed $data the data to be processed |
||
141 | * @param array $expressions collection of JavaScript expressions |
||
142 | * @param string $expPrefix a prefix internally used to handle JS expressions |
||
143 | * @return mixed the processed data |
||
144 | */ |
||
145 | 27 | protected static function processData($data, &$expressions, $expPrefix) |
|
182 | } |
||
183 |