14
14
use stdClass ;
15
15
use function array_merge ;
16
16
use function basename ;
17
- use function defined ;
18
17
use function dirname ;
19
18
use function file_exists ;
20
19
use function file_get_contents ;
27
26
use function preg_replace ;
28
27
use function trim ;
29
28
use const JSON_PRETTY_PRINT ;
29
+ use const JSON_THROW_ON_ERROR ;
30
30
use const JSON_UNESCAPED_SLASHES ;
31
31
use const JSON_UNESCAPED_UNICODE ;
32
32
33
- // Compatible with lower versions
34
- if (!defined ('JSON_THROW_ON_ERROR ' )) {
35
- define ('JSON_THROW_ON_ERROR ' , 4194304 ); // since php 7.3
36
- // class JsonException extends RuntimeException {}
37
- }
38
-
39
33
/**
40
34
* Class JsonHelper
41
35
*
@@ -53,7 +47,7 @@ class JsonHelper
53
47
* @return string
54
48
* @noinspection PhpDocMissingThrowsInspection
55
49
*/
56
- public static function enc ($ data , int $ flags = 0 , int $ depth = 512 ): string
50
+ public static function enc (mixed $ data , int $ flags = 0 , int $ depth = 512 ): string
57
51
{
58
52
/** @noinspection PhpUnhandledExceptionInspection */
59
53
return self ::encode ($ data , $ flags , $ depth );
@@ -69,10 +63,10 @@ public static function enc($data, int $flags = 0, int $depth = 512): string
69
63
* @return string
70
64
* @noinspection PhpDocMissingThrowsInspection
71
65
*/
72
- public static function encode ($ data , int $ options = 0 , int $ depth = 512 ): string
66
+ public static function encode (mixed $ data , int $ options = 0 , int $ depth = 512 ): string
73
67
{
74
68
/** @noinspection PhpUnhandledExceptionInspection */
75
- return (string )json_encode ($ data , \ JSON_THROW_ON_ERROR | $ options , $ depth );
69
+ return (string )json_encode ($ data , JSON_THROW_ON_ERROR | $ options , $ depth );
76
70
}
77
71
78
72
/**
@@ -86,7 +80,7 @@ public static function encode($data, int $options = 0, int $depth = 512): string
86
80
* @noinspection PhpDocMissingThrowsInspection
87
81
*/
88
82
public static function encodeCN (
89
- $ data ,
83
+ mixed $ data ,
90
84
int $ options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ,
91
85
int $ depth = 512
92
86
): string {
@@ -114,7 +108,7 @@ public static function pretty($data): string
114
108
* @noinspection PhpDocMissingThrowsInspection
115
109
*/
116
110
public static function prettyJSON (
117
- $ data ,
111
+ mixed $ data ,
118
112
int $ flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
119
113
): string {
120
114
/** @noinspection PhpUnhandledExceptionInspection */
@@ -161,13 +155,12 @@ public static function unescapedUnicode($data): string
161
155
162
156
/**
163
157
* @param string $json
164
- *
165
158
* @param bool $assoc
166
159
*
167
- * @return array|mixed
160
+ * @return array|stdClass
168
161
* @noinspection PhpDocMissingThrowsInspection
169
162
*/
170
- public static function dec (string $ json , bool $ assoc = true )
163
+ public static function dec (string $ json , bool $ assoc = true ): array | stdClass
171
164
{
172
165
/** @noinspection PhpUnhandledExceptionInspection */
173
166
$ data = json_decode ($ json , $ assoc , 512 , JSON_THROW_ON_ERROR );
@@ -190,7 +183,7 @@ public static function dec(string $json, bool $assoc = true)
190
183
* @return array|object
191
184
* @noinspection PhpDocMissingThrowsInspection
192
185
*/
193
- public static function decode (string $ json , bool $ assoc = false , int $ depth = 512 , int $ options = 0 )
186
+ public static function decode (string $ json , bool $ assoc = false , int $ depth = 512 , int $ options = 0 ): object | array
194
187
{
195
188
/** @noinspection PhpUnhandledExceptionInspection */
196
189
$ data = json_decode ($ json , $ assoc , $ depth , JSON_THROW_ON_ERROR | $ options );
@@ -214,7 +207,7 @@ public static function decode(string $json, bool $assoc = false, int $depth = 51
214
207
* @return array|object
215
208
* @noinspection PhpDocMissingThrowsInspection
216
209
*/
217
- public static function decodeFile (string $ jsonFile , bool $ assoc = false , int $ depth = 512 , int $ options = 0 )
210
+ public static function decodeFile (string $ jsonFile , bool $ assoc = false , int $ depth = 512 , int $ options = 0 ): object | array
218
211
{
219
212
if (!is_file ($ jsonFile )) {
220
213
throw new InvalidArgumentException ("json file not found: $ jsonFile " );
@@ -232,7 +225,7 @@ public static function decodeFile(string $jsonFile, bool $assoc = false, int $de
232
225
*
233
226
* @return array|stdClass
234
227
*/
235
- public static function parse (string $ data , bool $ toArray = true )
228
+ public static function parse (string $ data , bool $ toArray = true ): array | stdClass
236
229
{
237
230
if (is_file ($ data )) {
238
231
return self ::parseFile ($ data , $ toArray );
@@ -247,7 +240,7 @@ public static function parse(string $data, bool $toArray = true)
247
240
*
248
241
* @return array|stdClass
249
242
*/
250
- public static function parseFile (string $ jsonFile , bool $ toArray = true )
243
+ public static function parseFile (string $ jsonFile , bool $ toArray = true ): array | stdClass
251
244
{
252
245
if (!is_file ($ jsonFile )) {
253
246
throw new InvalidArgumentException ("File not found: $ jsonFile " );
@@ -264,7 +257,7 @@ public static function parseFile(string $jsonFile, bool $toArray = true)
264
257
* @return array|stdClass
265
258
* @noinspection PhpDocMissingThrowsInspection
266
259
*/
267
- public static function parseString (string $ json , bool $ toArray = true )
260
+ public static function parseString (string $ json , bool $ toArray = true ): array | stdClass
268
261
{
269
262
if (!$ json = trim ($ json )) {
270
263
return $ toArray ? [] : new stdClass ();
0 commit comments