Skip to content

Commit fe10c81

Browse files
committed
style: update readme and some comments
1 parent beeb28e commit fe10c81

File tree

3 files changed

+40
-57
lines changed

3 files changed

+40
-57
lines changed

README.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Php Version](https://img.shields.io/badge/php-%3E8.0-brightgreen.svg?maxAge=2592000)](https://packagist.org/packages/toolkit/stdlib)
55
[![Latest Stable Version](http://img.shields.io/packagist/v/toolkit/stdlib.svg)](https://packagist.org/packages/toolkit/stdlib)
66
[![Github Actions Status](https://github.com/php-toolkit/stdlib/workflows/Unit-Tests/badge.svg)](https://github.com/php-toolkit/stdlib/actions)
7-
[![Docs on pages](https://img.shields.io/badge/DocsOn-GhPages-brightgreen.svg?maxAge=2592000)](https://php-toolkit.github.io/stdlib/)
7+
[![Docs on pages](https://img.shields.io/badge/DocsOn-Pages-brightgreen.svg?maxAge=2592000)](https://php-toolkit.github.io/stdlib/)
88

99
🧰 Stdlib - Useful basic tools library for PHP development.
1010

@@ -15,8 +15,8 @@
1515

1616
**More Utils**
1717

18-
- Dotenv load `.env`
19-
- Simple autoloader
18+
- `PhpDotEnv` Dotenv(`.env`) file load
19+
- `AutoLoader` Simple autoloader
2020
- `ObjectBox` simple object container
2121
- `Optional` like java `java.util.Optional`
2222
- and more ...
@@ -170,10 +170,7 @@ use Toolkit\Stdlib\Util\Stream\DataStream;
170170
use Toolkit\Stdlib\Util\Stream\ListStream;
171171

172172
$userList = ListStream::of($userModels)
173-
->filter(function ($userModel) {
174-
// only need age > 20
175-
return $userModel->age > 20;
176-
})
173+
->filter(fn($userModel) => $userModel->age > 20) // only need age > 20
177174
->map(function ($userModel) {
178175
// only need field: age, name
179176
return [

src/Helper/JsonHelper.php

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use InvalidArgumentException;
1313
use RuntimeException;
1414
use stdClass;
15+
use Throwable;
1516
use function array_merge;
1617
use function basename;
1718
use function dirname;
@@ -21,8 +22,6 @@
2122
use function is_file;
2223
use function json_decode;
2324
use function json_encode;
24-
use function json_last_error;
25-
use function json_last_error_msg;
2625
use function preg_replace;
2726
use function trim;
2827
use const JSON_PRETTY_PRINT;
@@ -45,11 +44,9 @@ class JsonHelper
4544
* @param int $depth
4645
*
4746
* @return string
48-
* @noinspection PhpDocMissingThrowsInspection
4947
*/
5048
public static function enc(mixed $data, int $flags = 0, int $depth = 512): string
5149
{
52-
/** @noinspection PhpUnhandledExceptionInspection */
5350
return self::encode($data, $flags, $depth);
5451
}
5552

@@ -61,12 +58,14 @@ public static function enc(mixed $data, int $flags = 0, int $depth = 512): strin
6158
* @param int $depth
6259
*
6360
* @return string
64-
* @noinspection PhpDocMissingThrowsInspection
6561
*/
6662
public static function encode(mixed $data, int $options = 0, int $depth = 512): string
6763
{
68-
/** @noinspection PhpUnhandledExceptionInspection */
69-
return (string)json_encode($data, JSON_THROW_ON_ERROR | $options, $depth);
64+
try {
65+
return (string)json_encode($data, JSON_THROW_ON_ERROR | $options, $depth);
66+
} catch (Throwable $e) {
67+
throw new RuntimeException('JSON encode error - ' . $e->getMessage(), $e->getCode(), $e);
68+
}
7069
}
7170

7271
/**
@@ -77,26 +76,22 @@ public static function encode(mixed $data, int $options = 0, int $depth = 512):
7776
* @param int $depth
7877
*
7978
* @return string
80-
* @noinspection PhpDocMissingThrowsInspection
8179
*/
8280
public static function encodeCN(
8381
mixed $data,
8482
int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE,
8583
int $depth = 512
8684
): string {
87-
/** @noinspection PhpUnhandledExceptionInspection */
8885
return self::encode($data, $options, $depth);
8986
}
9087

9188
/**
9289
* @param $data
9390
*
9491
* @return string
95-
* @noinspection PhpDocMissingThrowsInspection
9692
*/
9793
public static function pretty($data): string
9894
{
99-
/** @noinspection PhpUnhandledExceptionInspection */
10095
return self::encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
10196
}
10297

@@ -105,49 +100,41 @@ public static function pretty($data): string
105100
* @param int $flags
106101
*
107102
* @return string
108-
* @noinspection PhpDocMissingThrowsInspection
109103
*/
110104
public static function prettyJSON(
111105
mixed $data,
112106
int $flags = JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES
113107
): string {
114-
/** @noinspection PhpUnhandledExceptionInspection */
115108
return self::encode($data, $flags);
116109
}
117110

118111
/**
119112
* @param $data
120113
*
121114
* @return string
122-
* @noinspection PhpDocMissingThrowsInspection
123115
*/
124116
public static function unescaped($data): string
125117
{
126-
/** @noinspection PhpUnhandledExceptionInspection */
127118
return self::encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
128119
}
129120

130121
/**
131122
* @param $data
132123
*
133124
* @return string
134-
* @noinspection PhpDocMissingThrowsInspection
135125
*/
136126
public static function unescapedSlashes($data): string
137127
{
138-
/** @noinspection PhpUnhandledExceptionInspection */
139128
return self::encode($data, JSON_UNESCAPED_SLASHES);
140129
}
141130

142131
/**
143-
* @param $data
132+
* @param mixed $data
144133
*
145134
* @return string
146-
* @noinspection PhpDocMissingThrowsInspection
147135
*/
148-
public static function unescapedUnicode($data): string
136+
public static function unescapedUnicode(mixed $data): string
149137
{
150-
/** @noinspection PhpUnhandledExceptionInspection */
151138
return self::encode($data, JSON_UNESCAPED_UNICODE);
152139
}
153140

@@ -158,39 +145,28 @@ public static function unescapedUnicode($data): string
158145
* @param bool $assoc
159146
*
160147
* @return array|stdClass
161-
* @noinspection PhpDocMissingThrowsInspection
162148
*/
163149
public static function dec(string $json, bool $assoc = true): array|stdClass
164150
{
165-
/** @noinspection PhpUnhandledExceptionInspection */
166-
$data = json_decode($json, $assoc, 512, JSON_THROW_ON_ERROR);
167-
168-
if (JSON_ERROR_NONE !== json_last_error()) {
169-
throw new InvalidArgumentException('json_decode error: ' . json_last_error_msg());
170-
}
171-
172-
return $data;
151+
return self::decode($json, $assoc);
173152
}
174153

175154
/**
176-
* Decode json
155+
* Decode JSON string.
177156
*
178157
* @param string $json
179158
* @param bool $assoc
180159
* @param int $depth
181160
* @param int $options
182161
*
183162
* @return array|object
184-
* @noinspection PhpDocMissingThrowsInspection
185163
*/
186164
public static function decode(string $json, bool $assoc = false, int $depth = 512, int $options = 0): object|array
187165
{
188-
/** @noinspection PhpUnhandledExceptionInspection */
189-
$data = json_decode($json, $assoc, $depth, JSON_THROW_ON_ERROR | $options);
190-
191-
if ($errCode = json_last_error()) {
192-
$errMsg = json_last_error_msg();
193-
throw new RuntimeException("JSON decode error: $errMsg", $errCode);
166+
try {
167+
$data = json_decode($json, $assoc, $depth, JSON_THROW_ON_ERROR | $options);
168+
} catch (Throwable $e) {
169+
throw new RuntimeException('JSON decode error - ' . $e->getMessage(), $e->getCode(), $e);
194170
}
195171

196172
return $data;
@@ -205,17 +181,15 @@ public static function decode(string $json, bool $assoc = false, int $depth = 51
205181
* @param int $options
206182
*
207183
* @return array|object
208-
* @noinspection PhpDocMissingThrowsInspection
209184
*/
210185
public static function decodeFile(string $jsonFile, bool $assoc = false, int $depth = 512, int $options = 0): object|array
211186
{
212187
if (!is_file($jsonFile)) {
213-
throw new InvalidArgumentException("json file not found: $jsonFile");
188+
throw new InvalidArgumentException("JSON file not exists: $jsonFile");
214189
}
215190

216191
$json = file_get_contents($jsonFile);
217192

218-
/** @noinspection PhpUnhandledExceptionInspection */
219193
return self::decode($json, $assoc, $depth, $options);
220194
}
221195

@@ -255,7 +229,6 @@ public static function parseFile(string $jsonFile, bool $toArray = true): array|
255229
* @param bool $toArray
256230
*
257231
* @return array|stdClass
258-
* @noinspection PhpDocMissingThrowsInspection
259232
*/
260233
public static function parseString(string $json, bool $toArray = true): array|stdClass
261234
{
@@ -264,18 +237,22 @@ public static function parseString(string $json, bool $toArray = true): array|st
264237
}
265238

266239
$json = self::stripComments($json);
267-
/** @noinspection PhpUnhandledExceptionInspection */
268240
return self::decode($json, $toArray);
269241
}
270242

271243
/**
244+
* Format JSON string.
245+
*
246+
* ```php
247+
* $options = [
248+
* 'type' => 'min' // 输出数据类型 min 压缩过的 raw 正常的
249+
* 'file' => 'xx.json' // 输出文件路径;仅是文件名,则会取输入路径
250+
* ]
251+
* ```
252+
*
272253
* @param string $input JSON 数据
273254
* @param bool $output 是否输出到文件, 默认返回格式化的数据
274-
* @param array $options 当 $output=true,此选项有效
275-
* $options = [
276-
* 'type' => 'min' // 输出数据类型 min 压缩过的 raw 正常的
277-
* 'file' => 'xx.json' // 输出文件路径;仅是文件名,则会取输入路径
278-
* ]
255+
* @param array{type: string, file: string} $options 当 $output=true,此选项有效
279256
*
280257
* @return string
281258
*/
@@ -326,7 +303,7 @@ public static function saveAs(string $data, string $output, array $options = [])
326303

327304
// 去掉空白
328305
if ($options['type '] === 'min') {
329-
$data = preg_replace('/(?!\w)\s*?(?!\w)/i', '', $data);
306+
$data = preg_replace('/(?!\w)\s*?(?!\w)/', '', $data);
330307
}
331308

332309
return file_put_contents($file, $data) > 0;

src/Util/Stream/DataStream.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use ArrayIterator;
66
use Closure;
7+
use JsonSerializable;
78
use Toolkit\Stdlib\Util\Optional;
89
use Traversable;
910
use function array_rand;
@@ -16,7 +17,7 @@
1617
* @template T
1718
* @var $this T[]
1819
*/
19-
class DataStream extends ArrayIterator
20+
class DataStream extends ArrayIterator implements JsonSerializable
2021
{
2122
private static ?DataStream $emptyObj = null;
2223

@@ -622,4 +623,12 @@ public function isNotEmpty(): bool
622623
{
623624
return $this->count() > 0;
624625
}
626+
627+
/**
628+
* @return array
629+
*/
630+
public function jsonSerialize(): array
631+
{
632+
return $this->getArrayCopy();
633+
}
625634
}

0 commit comments

Comments
 (0)