Skip to content

Commit 9b44011

Browse files
committed
update: add func println(), update some helper methods
1 parent b867e36 commit 9b44011

File tree

4 files changed

+125
-31
lines changed

4 files changed

+125
-31
lines changed

src/Helper/Assert.php

Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Toolkit\Stdlib\Helper;
44

55
use InvalidArgumentException;
6-
use RuntimeException;
6+
use LogicException;
77
use function in_array;
88

99
/**
@@ -21,11 +21,18 @@ class Assert
2121
/**
2222
* @param mixed $value
2323
* @param string $errMsg
24+
* @param bool $isPrefix
2425
*/
25-
public static function notEmpty(mixed $value, string $errMsg = ''): void
26+
public static function notEmpty(mixed $value, string $errMsg = '', bool $isPrefix = false): void
2627
{
2728
if (empty($value)) {
28-
throw static::createEx($errMsg ?: 'Expected a non-empty value');
29+
if ($errMsg) {
30+
$errMsg = $isPrefix ? "$errMsg cannot be empty" : $errMsg;
31+
} else {
32+
$errMsg = 'Expected a non-empty value';
33+
}
34+
35+
throw static::createEx($errMsg);
2936
}
3037
}
3138

@@ -43,11 +50,18 @@ public static function notNull(mixed $value, string $errMsg = ''): void
4350
/**
4451
* @param string $value
4552
* @param string $errMsg
53+
* @param bool $isPrefix
4654
*/
47-
public static function notBlank(string $value, string $errMsg = ''): void
55+
public static function notBlank(string $value, string $errMsg = '', bool $isPrefix = false): void
4856
{
4957
if ('' === $value) {
50-
throw static::createEx($errMsg ?: 'Expected a non-blank string value');
58+
if ($errMsg) {
59+
$errMsg = $isPrefix ? "$errMsg cannot be empty string" : $errMsg;
60+
} else {
61+
$errMsg = 'Expected a non-blank string value';
62+
}
63+
64+
throw static::createEx($errMsg);
5165
}
5266
}
5367

@@ -127,54 +141,82 @@ public static function notZero(int $value, string $errMsg = ''): void
127141
}
128142

129143
/**
130-
* Natural number. >= 0
144+
* Positive integer. should > 0
131145
*
132146
* @param int $value
133147
* @param string $errMsg
148+
* @param bool $isPrefix
134149
*/
135-
public static function naturalInt(int $value, string $errMsg = ''): void
150+
public static function intShouldGt0(int $value, string $errMsg = '', bool $isPrefix = false): void
136151
{
137-
if ($value < 0) {
138-
throw static::createEx($errMsg ?: 'Expected a natural number value(>=0)');
152+
if ($value < 1) {
153+
if ($errMsg) {
154+
$errMsg = $isPrefix ? "$errMsg should > 0" : $errMsg;
155+
} else {
156+
$errMsg = 'Expected a integer value and should > 0';
157+
}
158+
159+
throw static::createEx($errMsg);
139160
}
140161
}
141162

142163
/**
143-
* Positive integer. should > 0
164+
* Positive integer. should >= 0
144165
*
145166
* @param int $value
146167
* @param string $errMsg
168+
* @param bool $isPrefix
147169
*/
148-
public static function intShouldGt0(int $value, string $errMsg = ''): void
170+
public static function intShouldGte0(int $value, string $errMsg = '', bool $isPrefix = false): void
149171
{
150-
if ($value < 1) {
151-
throw static::createEx($errMsg ?: 'Expected a integer value and should > 0');
172+
if ($value < 0) {
173+
if ($errMsg) {
174+
$errMsg = $isPrefix ? "$errMsg should >= 0" : $errMsg;
175+
} else {
176+
$errMsg = 'Expected a integer value and should >= 0';
177+
}
178+
179+
throw static::createEx($errMsg);
152180
}
153181
}
154182

155183
/**
156-
* Positive integer. should >= 0
184+
* Positive integer. should > 0
157185
*
158186
* @param int $value
159187
* @param string $errMsg
188+
* @param bool $isPrefix
160189
*/
161-
public static function intShouldGte0(int $value, string $errMsg = ''): void
190+
public static function positiveInt(int $value, string $errMsg = '', bool $isPrefix = false): void
162191
{
163-
if ($value < 0) {
164-
throw static::createEx($errMsg ?: 'Expected a integer value and should >= 0');
192+
if ($value < 1) {
193+
if ($errMsg) {
194+
$errMsg = $isPrefix ? "$errMsg should > 0" : $errMsg;
195+
} else {
196+
$errMsg = 'Expected a positive integer value(>0)';
197+
}
198+
199+
throw static::createEx($errMsg);
165200
}
166201
}
167202

168203
/**
169-
* Positive integer. > 0
204+
* Natural number. should >= 0
170205
*
171206
* @param int $value
172207
* @param string $errMsg
208+
* @param bool $isPrefix
173209
*/
174-
public static function positiveInt(int $value, string $errMsg = ''): void
210+
public static function naturalInt(int $value, string $errMsg = '', bool $isPrefix = false): void
175211
{
176-
if ($value < 1) {
177-
throw static::createEx($errMsg ?: 'Expected a positive integer value(>0)');
212+
if ($value < 0) {
213+
if ($errMsg) {
214+
$errMsg = $isPrefix ? "$errMsg should >= 0" : $errMsg;
215+
} else {
216+
$errMsg = 'Expected a natural number value(>=0)';
217+
}
218+
219+
throw static::createEx($errMsg);
178220
}
179221
}
180222

@@ -211,7 +253,7 @@ public static function arrayHasKeys(array $data, array $keys, string $errMsg = '
211253
*/
212254
public static function arrayHasNoEmptyKey(array $data, string $key, string $errMsg = ''): void
213255
{
214-
if (!isset($data[$key]) || empty($data[$key])) {
256+
if (empty($data[$key])) {
215257
throw static::createEx($errMsg ?: "Data must contains key '$key' and value non-empty");
216258
}
217259
}
@@ -221,9 +263,9 @@ public static function arrayHasNoEmptyKey(array $data, string $key, string $errM
221263
/**
222264
* @param string $errMsg
223265
*
224-
* @return RuntimeException
266+
* @return LogicException
225267
*/
226-
public static function createEx(string $errMsg): RuntimeException
268+
public static function createEx(string $errMsg): LogicException
227269
{
228270
return new self::$exClass($errMsg);
229271
}

src/Helper/DataHelper.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use function is_bool;
1616
use function is_object;
1717
use function is_scalar;
18+
use function is_string;
1819
use function json_encode;
1920
use function method_exists;
2021
use const FILTER_NULL_ON_FAILURE;
@@ -34,17 +35,13 @@ class DataHelper
3435
* 注意: NULL 不是标量类型
3536
*
3637
* @param int|string $val
37-
* @param bool|mixed $nullAsFalse
38+
* @param bool $nullAsFalse
3839
*
3940
* @return bool
4041
*/
4142
public static function boolean(int|string $val, bool $nullAsFalse = false): bool
4243
{
43-
if ($val !== null && !is_scalar($val)) {
44-
return (bool)$val;
45-
}
46-
47-
return filter_var($val, FILTER_VALIDATE_BOOLEAN, [
44+
return (bool)filter_var($val, FILTER_VALIDATE_BOOLEAN, [
4845
'flags' => $nullAsFalse ? FILTER_NULL_ON_FAILURE : 0
4946
]);
5047
}
@@ -72,7 +69,7 @@ public static function toString(mixed $val): string
7269
return $val ? 'bool(TRUE)' : 'bool(FALSE)';
7370
}
7471

75-
return (string)$val;
72+
return is_string($val) ? $val : (string)$val;
7673
}
7774

7875
// TIP: null is not scalar type.

src/Helper/PhpHelper.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
use Toolkit\Stdlib\Obj\ObjectHelper;
2020
use Toolkit\Stdlib\Util\PhpError;
2121
use Toolkit\Stdlib\Util\PhpException;
22+
use function array_shift;
2223
use function array_sum;
24+
use function basename;
2325
use function error_get_last;
2426
use function explode;
2527
use function fopen;
@@ -149,6 +151,29 @@ public static function newMemoryStream(string $mode = 'rwb')
149151
return $handle;
150152
}
151153

154+
private static ?string $scriptName = null;
155+
156+
/**
157+
* @param bool $refresh
158+
*
159+
* @return string
160+
*/
161+
public static function getBinName(bool $refresh = false): string
162+
{
163+
if (!$refresh && self::$scriptName !== null) {
164+
return self::$scriptName;
165+
}
166+
167+
$scriptName = '';
168+
if (isset($_SERVER['argv']) && ($argv = $_SERVER['argv'])) {
169+
$scriptFile = array_shift($argv);
170+
$scriptName = basename($scriptFile);
171+
}
172+
173+
self::$scriptName = $scriptName;
174+
return self::$scriptName;
175+
}
176+
152177
/**
153178
* get $_SERVER value
154179
*

src/func.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,36 @@
88
*/
99

1010
use JetBrains\PhpStorm\NoReturn;
11+
use Toolkit\Stdlib\Helper\DataHelper;
12+
13+
if (!function_exists('println')) {
14+
/**
15+
* @param mixed ...$vars
16+
*
17+
* @return void
18+
*/
19+
function println(...$vars): void
20+
{
21+
$eleNum = count($vars);
22+
if ($eleNum === 1) {
23+
echo DataHelper::toString($vars[0]) . PHP_EOL;
24+
return;
25+
}
26+
27+
if ($eleNum === 0) {
28+
echo PHP_EOL;
29+
return;
30+
}
31+
32+
// eleNum > 1
33+
$ss = [];
34+
foreach ($vars as $var) {
35+
$ss[] = DataHelper::toString($var);
36+
}
37+
38+
echo implode(' ', $ss) . PHP_EOL;
39+
}
40+
}
1141

1242
if (!function_exists('vdump')) {
1343
/**

0 commit comments

Comments
 (0)