Skip to content

Commit ac73317

Browse files
committed
update some for json helper
1 parent 6c7a01c commit ac73317

File tree

1 file changed

+82
-55
lines changed

1 file changed

+82
-55
lines changed

src/Helper/JsonHelper.php

Lines changed: 82 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use function file_get_contents;
2020
use function file_put_contents;
2121
use function is_file;
22-
use function is_string;
2322
use function json_decode;
2423
use function json_encode;
2524
use function json_last_error;
@@ -54,8 +53,8 @@ public static function prettyJSON(
5453
* Encode data to json
5554
*
5655
* @param mixed $data
57-
* @param int $options
58-
* @param int $depth
56+
* @param int $options
57+
* @param int $depth
5958
*
6059
* @return string
6160
*/
@@ -68,13 +67,16 @@ public static function encode($data, int $options = 0, int $depth = 512): string
6867
* Encode data to json with some default options
6968
*
7069
* @param mixed $data
71-
* @param int $options
72-
* @param int $depth
70+
* @param int $options
71+
* @param int $depth
7372
*
7473
* @return string
7574
*/
76-
public static function encodeCN($data, int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE, int $depth = 512): string
77-
{
75+
public static function encodeCN(
76+
$data,
77+
int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
78+
int $depth = 512
79+
): string {
7880
return json_encode($data, $options, $depth);
7981
}
8082

@@ -100,11 +102,32 @@ public static function decode(string $json, bool $assoc = false, int $depth = 51
100102
return $data;
101103
}
102104

105+
/**
106+
* Decode json file
107+
*
108+
* @param string $jsonFile
109+
* @param bool $assoc
110+
* @param int $depth
111+
* @param int $options
112+
*
113+
* @return array|object
114+
*/
115+
public static function decodeFile(string $jsonFile, bool $assoc = false, int $depth = 512, int $options = 0)
116+
{
117+
if (!is_file($jsonFile)) {
118+
throw new InvalidArgumentException("json file not found: {$jsonFile}");
119+
}
120+
121+
$json = file_get_contents($jsonFile);
122+
123+
return self::decode($json, $assoc, $depth, $options);
124+
}
125+
103126
/**
104127
* @param string $data
105128
* @param bool $toArray
106129
*
107-
* @return array|mixed|null|stdClass|string
130+
* @return array|mixed|stdClass
108131
* @throws InvalidArgumentException
109132
*/
110133
public static function parse(string $data, bool $toArray = true)
@@ -117,83 +140,54 @@ public static function parse(string $data, bool $toArray = true)
117140
}
118141

119142
/**
120-
* @param string $file
121-
* @param bool|true $toArray
143+
* @param string $jsonFile
144+
* @param bool $toArray
122145
*
123-
* @return mixed|null|string
124-
* @throws InvalidArgumentException
146+
* @return array|mixed|stdClass
125147
*/
126-
public static function parseFile(string $file, $toArray = true)
148+
public static function parseFile(string $jsonFile, bool $toArray = true)
127149
{
128-
if (!is_file($file)) {
129-
throw new InvalidArgumentException("File not found: {$file}");
150+
if (!is_file($jsonFile)) {
151+
throw new InvalidArgumentException("File not found: {$jsonFile}");
130152
}
131153

132-
$string = file_get_contents($file);
133-
134-
return self::parseString($string, $toArray);
154+
$json = file_get_contents($jsonFile);
155+
return self::parseString($json, $toArray);
135156
}
136157

137158
/**
138-
* @param string $string
159+
* @param string $json
139160
* @param bool $toArray
140161
*
141162
* @return array|mixed|stdClass
142163
*/
143-
public static function parseString(string $string, bool $toArray = true)
164+
public static function parseString(string $json, bool $toArray = true)
144165
{
145-
if (!$string = trim($string)) {
166+
if (!$json = trim($json)) {
146167
return $toArray ? [] : new stdClass();
147168
}
148169

149-
$string = (string)preg_replace([
150-
// 去掉所有多行注释/* .... */
151-
'/\/\*.*?\*\/\s*/is',
152-
// 去掉所有单行注释//....
153-
'/\/\/.*?[\r\n]/is',
154-
// 去掉空白, 多个空格换成一个
155-
//'/(?!\w)\s*?(?!\w)/is'
156-
], ['', '', ' '], $string);
157-
158-
// json_last_error() === JSON_ERROR_NONE
159-
return json_decode($string, $toArray);
170+
$json = self::stripComments($json);
171+
return self::decode($json, $toArray);
160172
}
161173

162174
/**
163-
* @param string $input 文件 或 数据
175+
* @param string $input JSON 数据
164176
* @param bool $output 是否输出到文件, 默认返回格式化的数据
165177
* @param array $options 当 $output=true,此选项有效
166178
* $options = [
167179
* 'type' => 'min' // 输出数据类型 min 压缩过的 raw 正常的
168180
* 'file' => 'xx.json' // 输出文件路径;仅是文件名,则会取输入路径
169181
* ]
170182
*
171-
* @return string | bool
183+
* @return string
172184
*/
173-
public static function format($input, $output = false, array $options = [])
185+
public static function format(string $input, bool $output = false, array $options = []): string
174186
{
175-
if (!is_string($input)) {
176-
return false;
177-
}
178-
179-
$data = trim($input);
180-
if (file_exists($input)) {
181-
$data = file_get_contents($input);
182-
}
183-
184-
if (!$data) {
185-
return false;
187+
if (!$data = self::stripComments($input)) {
188+
return '';
186189
}
187190

188-
$data = preg_replace([
189-
// 去掉所有多行注释/* .... */
190-
'/\/\*.*?\*\/\s*/is',
191-
// 去掉所有单行注释//....
192-
'/\/\/.*?[\r\n]/is',
193-
// 去掉空白行
194-
"/(\n[\r])+/is"
195-
], ['', '', "\n"], $data);
196-
197191
if (!$output) {
198192
return $data;
199193
}
@@ -240,4 +234,37 @@ public static function saveAs(string $data, string $output, array $options = [])
240234

241235
return file_put_contents($file, $data);
242236
}
237+
238+
/**
239+
* @param string $json
240+
*
241+
* @return string
242+
*/
243+
public static function clearComments(string $json): string
244+
{
245+
return self::stripComments($json);
246+
}
247+
248+
/**
249+
* @param string $json
250+
*
251+
* @return string
252+
*/
253+
public static function stripComments(string $json): string
254+
{
255+
if (!$json = trim($json)) {
256+
return '';
257+
}
258+
259+
$pattern = [
260+
// 去掉所有多行注释/* .... */
261+
'/\/\*.*?\*\/\s*/is',
262+
// 去掉所有单行注释//....
263+
'/\/\/.*?[\r\n]/is',
264+
// 去掉空白行
265+
"/(\n[\r])+/is"
266+
];
267+
268+
return (string)preg_replace($pattern, ['', '', "\n"], $json);
269+
}
243270
}

0 commit comments

Comments
 (0)