@@ -21,11 +21,7 @@ class JsonHelper
2121 */
2222 public static function encode ($ data ): string
2323 {
24- if (PHP_VERSION_ID >= 50400 ) {
25- return json_encode ($ data , JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
26- }
27-
28- return json_encode ($ data );
24+ return \json_encode ($ data , \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE );
2925 }
3026
3127 /**
@@ -34,7 +30,7 @@ public static function encode($data): string
3430 * @return array|mixed|null|\stdClass|string
3531 * @throws \InvalidArgumentException
3632 */
37- public static function parse (string $ data , $ toArray = true )
33+ public static function parse (string $ data , bool $ toArray = true )
3834 {
3935 if (is_file ($ data )) {
4036 return self ::parseFile ($ data , $ toArray );
@@ -44,18 +40,18 @@ public static function parse(string $data, $toArray = true)
4440 }
4541
4642 /**
47- * @param $file
43+ * @param string $file
4844 * @param bool|true $toArray
4945 * @return mixed|null|string
5046 * @throws \InvalidArgumentException
5147 */
52- public static function parseFile ($ file , $ toArray = true )
48+ public static function parseFile (string $ file , $ toArray = true )
5349 {
54- if (!is_file ($ file )) {
50+ if (!\ is_file ($ file )) {
5551 throw new \InvalidArgumentException ("File not found or does not exist resources: {$ file }" );
5652 }
5753
58- $ string = file_get_contents ($ file );
54+ $ string = \ file_get_contents ($ file );
5955
6056 return self ::parseString ($ string , $ toArray );
6157 }
@@ -71,7 +67,7 @@ public static function parseString(string $string, bool $toArray = true)
7167 return $ toArray ? [] : new \stdClass ();
7268 }
7369
74- $ string = (string )preg_replace ([
70+ $ string = (string )\ preg_replace ([
7571 // 去掉所有多行注释/* .... */
7672 '/\/\*.*?\*\/\s*/is ' ,
7773 // 去掉所有单行注释//....
@@ -81,7 +77,7 @@ public static function parseString(string $string, bool $toArray = true)
8177 ], ['' , '' , ' ' ], trim ($ string ));
8278
8379 // json_last_error() === JSON_ERROR_NONE
84- return json_decode ($ string , $ toArray );
80+ return \ json_decode ($ string , $ toArray );
8581 }
8682
8783 /**
@@ -100,17 +96,17 @@ public static function format($input, $output = false, array $options = [])
10096 return false ;
10197 }
10298
103- $ data = trim ($ input );
99+ $ data = \ trim ($ input );
104100
105- if (file_exists ($ input )) {
106- $ data = file_get_contents ($ input );
101+ if (\ file_exists ($ input )) {
102+ $ data = \ file_get_contents ($ input );
107103 }
108104
109105 if (!$ data ) {
110106 return false ;
111107 }
112108
113- $ data = preg_replace ([
109+ $ data = \ preg_replace ([
114110 // 去掉所有多行注释/* .... */
115111 '/\/\*.*?\*\/\s*/is ' ,
116112 // 去掉所有单行注释//....
@@ -124,17 +120,16 @@ public static function format($input, $output = false, array $options = [])
124120 }
125121
126122 $ default = ['type ' => 'min ' ];
127- $ options = array_merge ($ default , $ options );
123+ $ options = \ array_merge ($ default , $ options );
128124
129- if (file_exists ($ input ) && (empty ($ options ['file ' ]) || !is_file ($ options ['file ' ]))) {
125+ if (\ file_exists ($ input ) && (empty ($ options ['file ' ]) || !\ is_file ($ options ['file ' ]))) {
130126 $ dir = \dirname ($ input );
131- $ name = basename ($ input , '.json ' );
127+ $ name = \ basename ($ input , '.json ' );
132128 $ file = $ dir . '/ ' . $ name . '. ' . $ options ['type ' ] . '.json ' ;
133129 $ options ['file ' ] = $ file ;
134130 }
135131
136132 static ::saveAs ($ data , $ options ['file ' ], $ options ['type ' ]);
137-
138133 return $ data ;
139134 }
140135
@@ -150,18 +145,18 @@ public static function saveAs(string $data, string $output, array $options = [])
150145 $ options = array_merge ($ default , $ options );
151146 $ dir = \dirname ($ output );
152147
153- if (!file_exists ($ dir )) {
154- trigger_error ('设置的json文件输出 ' . $ dir . '目录不存在! ' );
148+ if (!\ file_exists ($ dir )) {
149+ throw new \ RuntimeException ('设置的json文件输出 ' . $ dir . '目录不存在! ' );
155150 }
156151
157- $ name = basename ($ output , '.json ' );
152+ $ name = \ basename ($ output , '.json ' );
158153 $ file = $ dir . '/ ' . $ name . '. ' . $ options ['type ' ] . '.json ' ;
159154
160155 if ($ options ['type ' ] === 'min ' ) {
161156 // 去掉空白
162- $ data = (string )preg_replace ('/(?!\w)\s*?(?!\w)/i ' , '' , $ data );
157+ $ data = (string )\ preg_replace ('/(?!\w)\s*?(?!\w)/i ' , '' , $ data );
163158 }
164159
165- return file_put_contents ($ file , $ data );
160+ return \ file_put_contents ($ file , $ data );
166161 }
167162}
0 commit comments