Skip to content

Commit 4cbfa4a

Browse files
committed
up: move some file to parent dir
1 parent aecf78c commit 4cbfa4a

File tree

2 files changed

+80
-29
lines changed

2 files changed

+80
-29
lines changed

src/Str/Json.php renamed to src/Json.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
* @license MIT
88
*/
99

10-
namespace Toolkit\Stdlib\Str;
10+
namespace Toolkit\Stdlib;
1111

1212
/**
1313
* Class Json
14-
* @package Toolkit\Stdlib\Str
14+
* @package Toolkit\Stdlib
1515
*/
1616
final class Json extends JsonHelper
1717
{

src/Str/JsonHelper.php renamed to src/JsonHelper.php

Lines changed: 78 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -7,33 +7,40 @@
77
* @license MIT
88
*/
99

10-
namespace Toolkit\Stdlib\Str;
10+
namespace Toolkit\Stdlib;
1111

12-
use RuntimeException;
1312
use InvalidArgumentException;
13+
use RuntimeException;
1414
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;
1917
use function dirname;
2018
use function file_exists;
21-
use function basename;
22-
use function preg_replace;
19+
use function file_get_contents;
2320
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;
2429
use const JSON_PRETTY_PRINT;
2530
use const JSON_UNESCAPED_SLASHES;
2631
use const JSON_UNESCAPED_UNICODE;
2732

2833
/**
2934
* Class JsonHelper
30-
* @package Toolkit\Stdlib\Str
35+
*
36+
* @package Toolkit\Stdlib
3137
*/
3238
class JsonHelper
3339
{
3440
/**
3541
* @param mixed $data
3642
* @param int $flags
43+
*
3744
* @return false|string
3845
*/
3946
public static function prettyJSON(
@@ -44,18 +51,59 @@ public static function prettyJSON(
4451
}
4552

4653
/**
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+
*
4974
* @return string
5075
*/
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
5277
{
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;
54101
}
55102

56103
/**
57104
* @param string $data
58105
* @param bool $toArray
106+
*
59107
* @return array|mixed|null|stdClass|string
60108
* @throws InvalidArgumentException
61109
*/
@@ -71,13 +119,14 @@ public static function parse(string $data, bool $toArray = true)
71119
/**
72120
* @param string $file
73121
* @param bool|true $toArray
122+
*
74123
* @return mixed|null|string
75124
* @throws InvalidArgumentException
76125
*/
77126
public static function parseFile(string $file, $toArray = true)
78127
{
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}");
81130
}
82131

83132
$string = file_get_contents($file);
@@ -88,11 +137,12 @@ public static function parseFile(string $file, $toArray = true)
88137
/**
89138
* @param string $string
90139
* @param bool $toArray
140+
*
91141
* @return array|mixed|stdClass
92142
*/
93143
public static function parseString(string $string, bool $toArray = true)
94144
{
95-
if (!$string) {
145+
if (!$string = trim($string)) {
96146
return $toArray ? [] : new stdClass();
97147
}
98148

@@ -103,20 +153,21 @@ public static function parseString(string $string, bool $toArray = true)
103153
'/\/\/.*?[\r\n]/is',
104154
// 去掉空白, 多个空格换成一个
105155
//'/(?!\w)\s*?(?!\w)/is'
106-
], ['', '', ' '], trim($string));
156+
], ['', '', ' '], $string);
107157

108158
// json_last_error() === JSON_ERROR_NONE
109159
return json_decode($string, $toArray);
110160
}
111161

112162
/**
113-
* @param string $input 文件 或 数据
114-
* @param bool $output 是否输出到文件, 默认返回格式化的数据
163+
* @param string $input 文件 或 数据
164+
* @param bool $output 是否输出到文件, 默认返回格式化的数据
115165
* @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+
*
120171
* @return string | bool
121172
*/
122173
public static function format($input, $output = false, array $options = [])
@@ -125,8 +176,7 @@ public static function format($input, $output = false, array $options = [])
125176
return false;
126177
}
127178

128-
$data = \trim($input);
129-
179+
$data = trim($input);
130180
if (file_exists($input)) {
131181
$data = file_get_contents($input);
132182
}
@@ -149,9 +199,9 @@ public static function format($input, $output = false, array $options = [])
149199
}
150200

151201
$default = ['type' => 'min'];
152-
$options = \array_merge($default, $options);
202+
$options = array_merge($default, $options);
153203

154-
if (file_exists($input) && (empty($options['file']) || !\is_file($options['file']))) {
204+
if (file_exists($input) && (empty($options['file']) || !is_file($options['file']))) {
155205
$dir = dirname($input);
156206
$name = basename($input, '.json');
157207
$file = $dir . '/' . $name . '.' . $options['type'] . '.json';
@@ -167,6 +217,7 @@ public static function format($input, $output = false, array $options = [])
167217
* @param string $data
168218
* @param string $output
169219
* @param array $options
220+
*
170221
* @return bool|int
171222
*/
172223
public static function saveAs(string $data, string $output, array $options = [])

0 commit comments

Comments
 (0)