@@ -21,11 +21,7 @@ class JsonHelper
21
21
*/
22
22
public static function encode ($ data ): string
23
23
{
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 );
29
25
}
30
26
31
27
/**
@@ -34,7 +30,7 @@ public static function encode($data): string
34
30
* @return array|mixed|null|\stdClass|string
35
31
* @throws \InvalidArgumentException
36
32
*/
37
- public static function parse (string $ data , $ toArray = true )
33
+ public static function parse (string $ data , bool $ toArray = true )
38
34
{
39
35
if (is_file ($ data )) {
40
36
return self ::parseFile ($ data , $ toArray );
@@ -44,18 +40,18 @@ public static function parse(string $data, $toArray = true)
44
40
}
45
41
46
42
/**
47
- * @param $file
43
+ * @param string $file
48
44
* @param bool|true $toArray
49
45
* @return mixed|null|string
50
46
* @throws \InvalidArgumentException
51
47
*/
52
- public static function parseFile ($ file , $ toArray = true )
48
+ public static function parseFile (string $ file , $ toArray = true )
53
49
{
54
- if (!is_file ($ file )) {
50
+ if (!\ is_file ($ file )) {
55
51
throw new \InvalidArgumentException ("File not found or does not exist resources: {$ file }" );
56
52
}
57
53
58
- $ string = file_get_contents ($ file );
54
+ $ string = \ file_get_contents ($ file );
59
55
60
56
return self ::parseString ($ string , $ toArray );
61
57
}
@@ -71,7 +67,7 @@ public static function parseString(string $string, bool $toArray = true)
71
67
return $ toArray ? [] : new \stdClass ();
72
68
}
73
69
74
- $ string = (string )preg_replace ([
70
+ $ string = (string )\ preg_replace ([
75
71
// 去掉所有多行注释/* .... */
76
72
'/\/\*.*?\*\/\s*/is ' ,
77
73
// 去掉所有单行注释//....
@@ -81,7 +77,7 @@ public static function parseString(string $string, bool $toArray = true)
81
77
], ['' , '' , ' ' ], trim ($ string ));
82
78
83
79
// json_last_error() === JSON_ERROR_NONE
84
- return json_decode ($ string , $ toArray );
80
+ return \ json_decode ($ string , $ toArray );
85
81
}
86
82
87
83
/**
@@ -100,17 +96,17 @@ public static function format($input, $output = false, array $options = [])
100
96
return false ;
101
97
}
102
98
103
- $ data = trim ($ input );
99
+ $ data = \ trim ($ input );
104
100
105
- if (file_exists ($ input )) {
106
- $ data = file_get_contents ($ input );
101
+ if (\ file_exists ($ input )) {
102
+ $ data = \ file_get_contents ($ input );
107
103
}
108
104
109
105
if (!$ data ) {
110
106
return false ;
111
107
}
112
108
113
- $ data = preg_replace ([
109
+ $ data = \ preg_replace ([
114
110
// 去掉所有多行注释/* .... */
115
111
'/\/\*.*?\*\/\s*/is ' ,
116
112
// 去掉所有单行注释//....
@@ -124,17 +120,16 @@ public static function format($input, $output = false, array $options = [])
124
120
}
125
121
126
122
$ default = ['type ' => 'min ' ];
127
- $ options = array_merge ($ default , $ options );
123
+ $ options = \ array_merge ($ default , $ options );
128
124
129
- if (file_exists ($ input ) && (empty ($ options ['file ' ]) || !is_file ($ options ['file ' ]))) {
125
+ if (\ file_exists ($ input ) && (empty ($ options ['file ' ]) || !\ is_file ($ options ['file ' ]))) {
130
126
$ dir = \dirname ($ input );
131
- $ name = basename ($ input , '.json ' );
127
+ $ name = \ basename ($ input , '.json ' );
132
128
$ file = $ dir . '/ ' . $ name . '. ' . $ options ['type ' ] . '.json ' ;
133
129
$ options ['file ' ] = $ file ;
134
130
}
135
131
136
132
static ::saveAs ($ data , $ options ['file ' ], $ options ['type ' ]);
137
-
138
133
return $ data ;
139
134
}
140
135
@@ -150,18 +145,18 @@ public static function saveAs(string $data, string $output, array $options = [])
150
145
$ options = array_merge ($ default , $ options );
151
146
$ dir = \dirname ($ output );
152
147
153
- if (!file_exists ($ dir )) {
154
- trigger_error ('设置的json文件输出 ' . $ dir . '目录不存在! ' );
148
+ if (!\ file_exists ($ dir )) {
149
+ throw new \ RuntimeException ('设置的json文件输出 ' . $ dir . '目录不存在! ' );
155
150
}
156
151
157
- $ name = basename ($ output , '.json ' );
152
+ $ name = \ basename ($ output , '.json ' );
158
153
$ file = $ dir . '/ ' . $ name . '. ' . $ options ['type ' ] . '.json ' ;
159
154
160
155
if ($ options ['type ' ] === 'min ' ) {
161
156
// 去掉空白
162
- $ data = (string )preg_replace ('/(?!\w)\s*?(?!\w)/i ' , '' , $ data );
157
+ $ data = (string )\ preg_replace ('/(?!\w)\s*?(?!\w)/i ' , '' , $ data );
163
158
}
164
159
165
- return file_put_contents ($ file , $ data );
160
+ return \ file_put_contents ($ file , $ data );
166
161
}
167
162
}
0 commit comments