1414use stdClass ;
1515use function array_merge ;
1616use function basename ;
17- use function defined ;
1817use function dirname ;
1918use function file_exists ;
2019use function file_get_contents ;
2726use function preg_replace ;
2827use function trim ;
2928use const JSON_PRETTY_PRINT ;
29+ use const JSON_THROW_ON_ERROR ;
3030use const JSON_UNESCAPED_SLASHES ;
3131use const JSON_UNESCAPED_UNICODE ;
3232
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-
3933/**
4034 * Class JsonHelper
4135 *
@@ -53,7 +47,7 @@ class JsonHelper
5347 * @return string
5448 * @noinspection PhpDocMissingThrowsInspection
5549 */
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
5751 {
5852 /** @noinspection PhpUnhandledExceptionInspection */
5953 return self ::encode ($ data , $ flags , $ depth );
@@ -69,10 +63,10 @@ public static function enc($data, int $flags = 0, int $depth = 512): string
6963 * @return string
7064 * @noinspection PhpDocMissingThrowsInspection
7165 */
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
7367 {
7468 /** @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 );
7670 }
7771
7872 /**
@@ -86,7 +80,7 @@ public static function encode($data, int $options = 0, int $depth = 512): string
8680 * @noinspection PhpDocMissingThrowsInspection
8781 */
8882 public static function encodeCN (
89- $ data ,
83+ mixed $ data ,
9084 int $ options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ,
9185 int $ depth = 512
9286 ): string {
@@ -114,7 +108,7 @@ public static function pretty($data): string
114108 * @noinspection PhpDocMissingThrowsInspection
115109 */
116110 public static function prettyJSON (
117- $ data ,
111+ mixed $ data ,
118112 int $ flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
119113 ): string {
120114 /** @noinspection PhpUnhandledExceptionInspection */
@@ -161,13 +155,12 @@ public static function unescapedUnicode($data): string
161155
162156 /**
163157 * @param string $json
164- *
165158 * @param bool $assoc
166159 *
167- * @return array|mixed
160+ * @return array|stdClass
168161 * @noinspection PhpDocMissingThrowsInspection
169162 */
170- public static function dec (string $ json , bool $ assoc = true )
163+ public static function dec (string $ json , bool $ assoc = true ): array | stdClass
171164 {
172165 /** @noinspection PhpUnhandledExceptionInspection */
173166 $ data = json_decode ($ json , $ assoc , 512 , JSON_THROW_ON_ERROR );
@@ -190,7 +183,7 @@ public static function dec(string $json, bool $assoc = true)
190183 * @return array|object
191184 * @noinspection PhpDocMissingThrowsInspection
192185 */
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
194187 {
195188 /** @noinspection PhpUnhandledExceptionInspection */
196189 $ 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
214207 * @return array|object
215208 * @noinspection PhpDocMissingThrowsInspection
216209 */
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
218211 {
219212 if (!is_file ($ jsonFile )) {
220213 throw new InvalidArgumentException ("json file not found: $ jsonFile " );
@@ -232,7 +225,7 @@ public static function decodeFile(string $jsonFile, bool $assoc = false, int $de
232225 *
233226 * @return array|stdClass
234227 */
235- public static function parse (string $ data , bool $ toArray = true )
228+ public static function parse (string $ data , bool $ toArray = true ): array | stdClass
236229 {
237230 if (is_file ($ data )) {
238231 return self ::parseFile ($ data , $ toArray );
@@ -247,7 +240,7 @@ public static function parse(string $data, bool $toArray = true)
247240 *
248241 * @return array|stdClass
249242 */
250- public static function parseFile (string $ jsonFile , bool $ toArray = true )
243+ public static function parseFile (string $ jsonFile , bool $ toArray = true ): array | stdClass
251244 {
252245 if (!is_file ($ jsonFile )) {
253246 throw new InvalidArgumentException ("File not found: $ jsonFile " );
@@ -264,7 +257,7 @@ public static function parseFile(string $jsonFile, bool $toArray = true)
264257 * @return array|stdClass
265258 * @noinspection PhpDocMissingThrowsInspection
266259 */
267- public static function parseString (string $ json , bool $ toArray = true )
260+ public static function parseString (string $ json , bool $ toArray = true ): array | stdClass
268261 {
269262 if (!$ json = trim ($ json )) {
270263 return $ toArray ? [] : new stdClass ();
0 commit comments