12
12
use RuntimeException ;
13
13
use InvalidArgumentException ;
14
14
use stdClass ;
15
+ use function json_encode ;
16
+ use function json_decode ;
17
+ use function is_string ;
18
+ use function file_get_contents ;
19
+ use function dirname ;
20
+ use function file_exists ;
21
+ use function basename ;
22
+ use function preg_replace ;
23
+ use function file_put_contents ;
24
+ use const JSON_PRETTY_PRINT ;
25
+ use const JSON_UNESCAPED_SLASHES ;
26
+ use const JSON_UNESCAPED_UNICODE ;
15
27
16
28
/**
17
29
* Class JsonHelper
@@ -26,9 +38,9 @@ class JsonHelper
26
38
*/
27
39
public static function prettyJSON (
28
40
$ data ,
29
- int $ flags = \ JSON_PRETTY_PRINT | \ JSON_UNESCAPED_UNICODE | \ JSON_UNESCAPED_SLASHES
41
+ int $ flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
30
42
) {
31
- return \ json_encode ($ data , $ flags );
43
+ return json_encode ($ data , $ flags );
32
44
}
33
45
34
46
/**
@@ -38,7 +50,7 @@ public static function prettyJSON(
38
50
*/
39
51
public static function encode ($ data ): string
40
52
{
41
- return \ json_encode ($ data , \ JSON_UNESCAPED_SLASHES | \ JSON_UNESCAPED_UNICODE );
53
+ return json_encode ($ data , JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
42
54
}
43
55
44
56
/**
@@ -68,7 +80,7 @@ public static function parseFile(string $file, $toArray = true)
68
80
throw new InvalidArgumentException ("File not found or does not exist resources: {$ file }" );
69
81
}
70
82
71
- $ string = \ file_get_contents ($ file );
83
+ $ string = file_get_contents ($ file );
72
84
73
85
return self ::parseString ($ string , $ toArray );
74
86
}
@@ -84,7 +96,7 @@ public static function parseString(string $string, bool $toArray = true)
84
96
return $ toArray ? [] : new stdClass ();
85
97
}
86
98
87
- $ string = (string )\ preg_replace ([
99
+ $ string = (string )preg_replace ([
88
100
// 去掉所有多行注释/* .... */
89
101
'/\/\*.*?\*\/\s*/is ' ,
90
102
// 去掉所有单行注释//....
@@ -94,7 +106,7 @@ public static function parseString(string $string, bool $toArray = true)
94
106
], ['' , '' , ' ' ], trim ($ string ));
95
107
96
108
// json_last_error() === JSON_ERROR_NONE
97
- return \ json_decode ($ string , $ toArray );
109
+ return json_decode ($ string , $ toArray );
98
110
}
99
111
100
112
/**
@@ -109,21 +121,21 @@ public static function parseString(string $string, bool $toArray = true)
109
121
*/
110
122
public static function format ($ input , $ output = false , array $ options = [])
111
123
{
112
- if (!\ is_string ($ input )) {
124
+ if (!is_string ($ input )) {
113
125
return false ;
114
126
}
115
127
116
128
$ data = \trim ($ input );
117
129
118
- if (\ file_exists ($ input )) {
119
- $ data = \ file_get_contents ($ input );
130
+ if (file_exists ($ input )) {
131
+ $ data = file_get_contents ($ input );
120
132
}
121
133
122
134
if (!$ data ) {
123
135
return false ;
124
136
}
125
137
126
- $ data = \ preg_replace ([
138
+ $ data = preg_replace ([
127
139
// 去掉所有多行注释/* .... */
128
140
'/\/\*.*?\*\/\s*/is ' ,
129
141
// 去掉所有单行注释//....
@@ -139,9 +151,9 @@ public static function format($input, $output = false, array $options = [])
139
151
$ default = ['type ' => 'min ' ];
140
152
$ options = \array_merge ($ default , $ options );
141
153
142
- if (\ file_exists ($ input ) && (empty ($ options ['file ' ]) || !\is_file ($ options ['file ' ]))) {
143
- $ dir = \ dirname ($ input );
144
- $ name = \ basename ($ input , '.json ' );
154
+ if (file_exists ($ input ) && (empty ($ options ['file ' ]) || !\is_file ($ options ['file ' ]))) {
155
+ $ dir = dirname ($ input );
156
+ $ name = basename ($ input , '.json ' );
145
157
$ file = $ dir . '/ ' . $ name . '. ' . $ options ['type ' ] . '.json ' ;
146
158
// save to options
147
159
$ options ['file ' ] = $ file ;
@@ -161,20 +173,20 @@ public static function saveAs(string $data, string $output, array $options = [])
161
173
{
162
174
$ default = ['type ' => 'min ' , 'file ' => '' ];
163
175
$ options = array_merge ($ default , $ options );
164
- $ saveDir = \ dirname ($ output );
176
+ $ saveDir = dirname ($ output );
165
177
166
- if (!\ file_exists ($ saveDir )) {
178
+ if (!file_exists ($ saveDir )) {
167
179
throw new RuntimeException ('设置的json文件输出 ' . $ saveDir . '目录不存在! ' );
168
180
}
169
181
170
- $ name = \ basename ($ output , '.json ' );
182
+ $ name = basename ($ output , '.json ' );
171
183
$ file = $ saveDir . '/ ' . $ name . '. ' . $ options ['type ' ] . '.json ' ;
172
184
173
185
// 去掉空白
174
186
if ($ options ['type ' ] === 'min ' ) {
175
- $ data = \ preg_replace ('/(?!\w)\s*?(?!\w)/i ' , '' , $ data );
187
+ $ data = preg_replace ('/(?!\w)\s*?(?!\w)/i ' , '' , $ data );
176
188
}
177
189
178
- return \ file_put_contents ($ file , $ data );
190
+ return file_put_contents ($ file , $ data );
179
191
}
180
192
}
0 commit comments