7
7
* @license MIT
8
8
*/
9
9
10
- namespace Toolkit \Stdlib \ Str ;
10
+ namespace Toolkit \Stdlib ;
11
11
12
- use RuntimeException ;
13
12
use InvalidArgumentException ;
13
+ use RuntimeException ;
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 ;
15
+ use function array_merge ;
16
+ use function basename ;
19
17
use function dirname ;
20
18
use function file_exists ;
21
- use function basename ;
22
- use function preg_replace ;
19
+ use function file_get_contents ;
23
20
use function file_put_contents ;
21
+ use function is_file ;
22
+ use function is_string ;
23
+ use function json_decode ;
24
+ use function json_encode ;
25
+ use function json_last_error ;
26
+ use function json_last_error_msg ;
27
+ use function preg_replace ;
28
+ use function trim ;
24
29
use const JSON_PRETTY_PRINT ;
25
30
use const JSON_UNESCAPED_SLASHES ;
26
31
use const JSON_UNESCAPED_UNICODE ;
27
32
28
33
/**
29
34
* Class JsonHelper
30
- * @package Toolkit\Stdlib\Str
35
+ *
36
+ * @package Toolkit\Stdlib
31
37
*/
32
38
class JsonHelper
33
39
{
34
40
/**
35
41
* @param mixed $data
36
42
* @param int $flags
43
+ *
37
44
* @return false|string
38
45
*/
39
46
public static function prettyJSON (
@@ -44,18 +51,59 @@ public static function prettyJSON(
44
51
}
45
52
46
53
/**
47
- * encode data to json
48
- * @param $data
54
+ * Encode data to json
55
+ *
56
+ * @param mixed $data
57
+ * @param int $options
58
+ * @param int $depth
59
+ *
60
+ * @return string
61
+ */
62
+ public static function encode ($ data , int $ options = 0 , int $ depth = 512 ): string
63
+ {
64
+ return json_encode ($ data , $ options , $ depth );
65
+ }
66
+
67
+ /**
68
+ * Encode data to json with some default options
69
+ *
70
+ * @param mixed $data
71
+ * @param int $options
72
+ * @param int $depth
73
+ *
49
74
* @return string
50
75
*/
51
- public static function encode ($ data ): string
76
+ public static function encodeCN ($ data, int $ options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE , int $ depth = 512 ): string
52
77
{
53
- return json_encode ($ data , JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE );
78
+ return json_encode ($ data , $ options , $ depth );
79
+ }
80
+
81
+ /**
82
+ * Decode json
83
+ *
84
+ * @param string $json
85
+ * @param bool $assoc
86
+ * @param int $depth
87
+ * @param int $options
88
+ *
89
+ * @return array|object
90
+ */
91
+ public static function decode (string $ json , bool $ assoc = false , int $ depth = 512 , int $ options = 0 )
92
+ {
93
+ $ data = json_decode ($ json , $ assoc , $ depth , $ options );
94
+
95
+ if ($ errCode = json_last_error ()) {
96
+ $ errMsg = json_last_error_msg ();
97
+ throw new RuntimeException ("JSON decode error: $ errMsg " , $ errCode );
98
+ }
99
+
100
+ return $ data ;
54
101
}
55
102
56
103
/**
57
104
* @param string $data
58
105
* @param bool $toArray
106
+ *
59
107
* @return array|mixed|null|stdClass|string
60
108
* @throws InvalidArgumentException
61
109
*/
@@ -71,13 +119,14 @@ public static function parse(string $data, bool $toArray = true)
71
119
/**
72
120
* @param string $file
73
121
* @param bool|true $toArray
122
+ *
74
123
* @return mixed|null|string
75
124
* @throws InvalidArgumentException
76
125
*/
77
126
public static function parseFile (string $ file , $ toArray = true )
78
127
{
79
- if (!\ is_file ($ file )) {
80
- throw new InvalidArgumentException ("File not found or does not exist resources : {$ file }" );
128
+ if (!is_file ($ file )) {
129
+ throw new InvalidArgumentException ("File not found: {$ file }" );
81
130
}
82
131
83
132
$ string = file_get_contents ($ file );
@@ -88,11 +137,12 @@ public static function parseFile(string $file, $toArray = true)
88
137
/**
89
138
* @param string $string
90
139
* @param bool $toArray
140
+ *
91
141
* @return array|mixed|stdClass
92
142
*/
93
143
public static function parseString (string $ string , bool $ toArray = true )
94
144
{
95
- if (!$ string ) {
145
+ if (!$ string = trim ( $ string ) ) {
96
146
return $ toArray ? [] : new stdClass ();
97
147
}
98
148
@@ -103,20 +153,21 @@ public static function parseString(string $string, bool $toArray = true)
103
153
'/\/\/.*?[\r\n]/is ' ,
104
154
// 去掉空白, 多个空格换成一个
105
155
//'/(?!\w)\s*?(?!\w)/is'
106
- ], ['' , '' , ' ' ], trim ( $ string) );
156
+ ], ['' , '' , ' ' ], $ string );
107
157
108
158
// json_last_error() === JSON_ERROR_NONE
109
159
return json_decode ($ string , $ toArray );
110
160
}
111
161
112
162
/**
113
- * @param string $input 文件 或 数据
114
- * @param bool $output 是否输出到文件, 默认返回格式化的数据
163
+ * @param string $input 文件 或 数据
164
+ * @param bool $output 是否输出到文件, 默认返回格式化的数据
115
165
* @param array $options 当 $output=true,此选项有效
116
- * $options = [
117
- * 'type' => 'min' // 输出数据类型 min 压缩过的 raw 正常的
118
- * 'file' => 'xx.json' // 输出文件路径;仅是文件名,则会取输入路径
119
- * ]
166
+ * $options = [
167
+ * 'type' => 'min' // 输出数据类型 min 压缩过的 raw 正常的
168
+ * 'file' => 'xx.json' // 输出文件路径;仅是文件名,则会取输入路径
169
+ * ]
170
+ *
120
171
* @return string | bool
121
172
*/
122
173
public static function format ($ input , $ output = false , array $ options = [])
@@ -125,8 +176,7 @@ public static function format($input, $output = false, array $options = [])
125
176
return false ;
126
177
}
127
178
128
- $ data = \trim ($ input );
129
-
179
+ $ data = trim ($ input );
130
180
if (file_exists ($ input )) {
131
181
$ data = file_get_contents ($ input );
132
182
}
@@ -149,9 +199,9 @@ public static function format($input, $output = false, array $options = [])
149
199
}
150
200
151
201
$ default = ['type ' => 'min ' ];
152
- $ options = \ array_merge ($ default , $ options );
202
+ $ options = array_merge ($ default , $ options );
153
203
154
- if (file_exists ($ input ) && (empty ($ options ['file ' ]) || !\ is_file ($ options ['file ' ]))) {
204
+ if (file_exists ($ input ) && (empty ($ options ['file ' ]) || !is_file ($ options ['file ' ]))) {
155
205
$ dir = dirname ($ input );
156
206
$ name = basename ($ input , '.json ' );
157
207
$ file = $ dir . '/ ' . $ name . '. ' . $ options ['type ' ] . '.json ' ;
@@ -167,6 +217,7 @@ public static function format($input, $output = false, array $options = [])
167
217
* @param string $data
168
218
* @param string $output
169
219
* @param array $options
220
+ *
170
221
* @return bool|int
171
222
*/
172
223
public static function saveAs (string $ data , string $ output , array $ options = [])
0 commit comments