1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Phalcon. |
5
|
|
|
* |
6
|
|
|
* (c) Phalcon Team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Phalcon\Helper; |
15
|
|
|
|
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
|
18
|
|
|
use function json_decode; |
19
|
|
|
use function json_encode; |
20
|
|
|
use function json_last_error; |
21
|
|
|
use function json_last_error_msg; |
22
|
|
|
|
23
|
|
|
use const JSON_ERROR_NONE; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* This class offers a wrapper for JSON methods to serialize and unserialize |
27
|
|
|
*/ |
28
|
|
|
class Json |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* Decodes a string using `json_decode` and throws an exception if the |
32
|
|
|
* JSON data cannot be decoded |
33
|
|
|
* |
34
|
|
|
* ```php |
35
|
|
|
* use Phalcon\Helper\Json; |
36
|
|
|
* |
37
|
|
|
* $data = '{"one":"two","0":"three"}'; |
38
|
|
|
* |
39
|
|
|
* var_dump(Json::decode($data)); |
40
|
|
|
* // [ |
41
|
|
|
* // 'one' => 'two', |
42
|
|
|
* // 'three' |
43
|
|
|
* // ]; |
44
|
|
|
* ``` |
45
|
|
|
* |
46
|
|
|
* @param string $data JSON data to parse |
47
|
|
|
* @param bool $associative When `true`, objects are converted to arrays |
48
|
|
|
* @param int $depth Recursion depth. |
49
|
|
|
* @param int $options Bitmask of JSON decode options. |
50
|
|
|
* |
51
|
|
|
* @return mixed |
52
|
|
|
* |
53
|
|
|
* @throws InvalidArgumentException if the JSON cannot be decoded. |
54
|
|
|
* @link http://www.php.net/manual/en/function.json-decode.php |
55
|
|
|
*/ |
56
|
38 |
|
final public static function decode( |
57
|
|
|
string $data, |
58
|
|
|
bool $associative = false, |
59
|
|
|
int $depth = 512, |
60
|
|
|
int $options = 0 |
61
|
|
|
) { |
62
|
38 |
|
$decoded = json_decode($data, $associative, $depth, $options); |
63
|
|
|
|
64
|
38 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
65
|
1 |
|
throw new InvalidArgumentException( |
66
|
1 |
|
"json_decode error: " . json_last_error_msg() |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
|
70
|
37 |
|
return $decoded; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Encodes a string using `json_encode` and throws an exception if the |
75
|
|
|
* JSON data cannot be encoded |
76
|
|
|
* |
77
|
|
|
* ```php |
78
|
|
|
* use Phalcon\Helper\Json; |
79
|
|
|
* |
80
|
|
|
* $data = [ |
81
|
|
|
* 'one' => 'two', |
82
|
|
|
* 'three' |
83
|
|
|
* ]; |
84
|
|
|
* |
85
|
|
|
* echo Json::encode($data); |
86
|
|
|
* // {"one":"two","0":"three"} |
87
|
|
|
* ``` |
88
|
|
|
* |
89
|
|
|
* @param mixed $data JSON data to parse |
90
|
|
|
* @param int $options Bitmask of JSON decode options. |
91
|
|
|
* @param int $depth Recursion depth. |
92
|
|
|
* |
93
|
|
|
* @return mixed |
94
|
|
|
* |
95
|
|
|
* @throws InvalidArgumentException if the JSON cannot be encoded. |
96
|
|
|
* @link http://www.php.net/manual/en/function.json-encode.php |
97
|
|
|
*/ |
98
|
38 |
|
final public static function encode( |
99
|
|
|
$data, |
100
|
|
|
int $options = 0, |
101
|
|
|
int $depth = 512 |
102
|
|
|
): string { |
103
|
38 |
|
$encoded = json_encode($data, $options, $depth); |
104
|
|
|
|
105
|
38 |
|
if (JSON_ERROR_NONE !== json_last_error()) { |
106
|
1 |
|
throw new InvalidArgumentException( |
107
|
1 |
|
"json_encode error: " . json_last_error_msg() |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
37 |
|
return (string) $encoded; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|