Skip to content

Commit 4bcaf5a

Browse files
committed
update: add new helper class Valid, update some helper
1 parent 7cc8ae3 commit 4bcaf5a

File tree

4 files changed

+350
-98
lines changed

4 files changed

+350
-98
lines changed

src/Helper/Assert.php

+41-75
Original file line numberDiff line numberDiff line change
@@ -13,49 +13,42 @@
1313
*/
1414
class Assert
1515
{
16+
/**
17+
* @var string
18+
*/
19+
private static string $exClass = InvalidArgumentException::class;
20+
1621
/**
1722
* @param mixed $value
1823
* @param string $errMsg
19-
*
20-
* @return mixed
2124
*/
22-
public static function notEmpty(mixed $value, string $errMsg = ''): mixed
25+
public static function notEmpty(mixed $value, string $errMsg = ''): void
2326
{
2427
if (empty($value)) {
2528
throw static::createEx($errMsg ?: 'Expected a non-empty value');
2629
}
27-
28-
return $value;
2930
}
3031

3132
/**
3233
* @param mixed $value
3334
* @param string $errMsg
34-
*
35-
* @return mixed
3635
*/
37-
public static function notNull(mixed $value, string $errMsg = ''): mixed
36+
public static function notNull(mixed $value, string $errMsg = ''): void
3837
{
3938
if (null === $value) {
4039
throw static::createEx($errMsg ?: 'Expected a non-null value');
4140
}
42-
43-
return $value;
4441
}
4542

4643
/**
4744
* @param string $value
4845
* @param string $errMsg
49-
*
50-
* @return string
5146
*/
52-
public static function notBlank(string $value, string $errMsg = ''): string
47+
public static function notBlank(string $value, string $errMsg = ''): void
5348
{
5449
if ('' === $value) {
5550
throw static::createEx($errMsg ?: 'Expected a non-blank string value');
5651
}
57-
58-
return $value;
5952
}
6053

6154
/**
@@ -64,16 +57,12 @@ public static function notBlank(string $value, string $errMsg = ''): string
6457
* @param mixed $value1
6558
* @param mixed $value2
6659
* @param string $errMsg
67-
*
68-
* @return bool
6960
*/
70-
public static function equals(mixed $value1, mixed $value2, string $errMsg = ''): bool
61+
public static function equals(mixed $value1, mixed $value2, string $errMsg = ''): void
7162
{
7263
if ($value1 !== $value2) {
7364
throw static::createEx($errMsg ?: "The $value1 should equals to $value2");
7465
}
75-
76-
return true;
7766
}
7867

7968
/**
@@ -82,175 +71,152 @@ public static function equals(mixed $value1, mixed $value2, string $errMsg = '')
8271
* @param mixed $value1
8372
* @param mixed $value2
8473
* @param string $errMsg
85-
*
86-
* @return bool
8774
*/
88-
public static function notEquals(mixed $value1, mixed $value2, string $errMsg = ''): bool
75+
public static function notEquals(mixed $value1, mixed $value2, string $errMsg = ''): void
8976
{
9077
if ($value1 === $value2) {
9178
throw static::createEx($errMsg ?: "The $value1 should not equals to $value2");
9279
}
93-
94-
return true;
9580
}
9681

9782
/**
9883
* @param bool $value
9984
* @param string $errMsg
100-
*
101-
* @return bool
10285
*/
103-
public static function isTrue(bool $value, string $errMsg = ''): bool
86+
public static function isTrue(bool $value, string $errMsg = ''): void
10487
{
10588
if (false === $value) {
10689
throw static::createEx($errMsg ?: 'Expected a true value');
10790
}
108-
109-
return $value;
11091
}
11192

11293
/**
11394
* @param bool $value
11495
* @param string $errMsg
115-
*
116-
* @return bool
11796
*/
118-
public static function isFalse(bool $value, string $errMsg = ''): bool
97+
public static function isFalse(bool $value, string $errMsg = ''): void
11998
{
12099
if (true === $value) {
121100
throw static::createEx($errMsg ?: 'Expected a false value');
122101
}
123-
124-
return $value;
125102
}
126103

127104
/**
128105
* @param mixed $needle
129106
* @param array $haystack
130107
* @param string $errMsg
131-
*
132-
* @return mixed
133108
*/
134-
public static function inArray(mixed $needle, array $haystack, string $errMsg = ''): mixed
109+
public static function inArray(mixed $needle, array $haystack, string $errMsg = ''): void
135110
{
136111
if (!in_array($needle, $haystack, true)) {
137112
throw static::createEx($errMsg ?: 'Expected a value in array');
138113
}
139-
140-
return $needle;
141114
}
142115

143116
/**
144117
* Value should != 0
145118
*
146119
* @param int $value
147120
* @param string $errMsg
148-
*
149-
* @return int
150121
*/
151-
public static function notZero(int $value, string $errMsg = ''): int
122+
public static function notZero(int $value, string $errMsg = ''): void
152123
{
153124
if ($value === 0) {
154125
throw static::createEx($errMsg ?: 'Expected a non-zero integer value');
155126
}
156-
157-
return $value;
158127
}
159128

160129
/**
161130
* Natural number. >= 0
162131
*
163132
* @param int $value
164133
* @param string $errMsg
165-
*
166-
* @return int
167134
*/
168-
public static function naturalInt(int $value, string $errMsg = ''): int
135+
public static function naturalInt(int $value, string $errMsg = ''): void
169136
{
170137
if ($value < 0) {
171138
throw static::createEx($errMsg ?: 'Expected a natural number value(>=0)');
172139
}
173-
174-
return $value;
175140
}
176141

177142
/**
178143
* Positive integer. > 0
179144
*
180145
* @param int $value
181146
* @param string $errMsg
182-
*
183-
* @return int
184147
*/
185-
public static function positiveInt(int $value, string $errMsg = ''): int
148+
public static function positiveInt(int $value, string $errMsg = ''): void
186149
{
187150
if ($value < 1) {
188151
throw static::createEx($errMsg ?: 'Expected a positive integer value(>0)');
189152
}
190-
191-
return $value;
192153
}
193154

194155
/**
195156
* @param array $data
196157
* @param string $key
197158
* @param string $errMsg
198-
*
199-
* @return mixed
200159
*/
201-
public static function arrayHasKey(array $data, string $key, string $errMsg = ''): mixed
160+
public static function arrayHasKey(array $data, string $key, string $errMsg = ''): void
202161
{
203162
if (!isset($data[$key])) {
204163
throw static::createEx($errMsg ?: "Array data must contains key '$key'");
205164
}
206-
207-
return $data[$key];
208165
}
209166

210167
/**
211168
* @param array $data
212169
* @param array $keys
213170
* @param string $errMsg
214-
*
215-
* @return array
216171
*/
217-
public static function arrayHasKeys(array $data, array $keys, string $errMsg = ''): array
172+
public static function arrayHasKeys(array $data, array $keys, string $errMsg = ''): void
218173
{
219-
$values = [];
220174
foreach ($keys as $key) {
221175
if (!isset($data[$key])) {
222176
throw static::createEx($errMsg ?: "Array data must contains key '$key'");
223177
}
224-
225-
$values[$key] = $data[$key];
226178
}
227-
228-
return $values;
229179
}
230180

231181
/**
232182
* @param array $data
233183
* @param string $key
234184
* @param string $errMsg
235-
*
236-
* @return mixed
237185
*/
238-
public static function arrayHasNoEmptyKey(array $data, string $key, string $errMsg = ''): mixed
186+
public static function arrayHasNoEmptyKey(array $data, string $key, string $errMsg = ''): void
239187
{
240188
if (!isset($data[$key]) || empty($data[$key])) {
241189
throw static::createEx($errMsg ?: "Data must contains key '$key' and value non-empty");
242190
}
243-
244-
return $data[$key];
245191
}
246192

193+
// ------------- helper methods -------------
194+
247195
/**
248196
* @param string $errMsg
249197
*
250-
* @return RuntimeException|InvalidArgumentException
198+
* @return RuntimeException
251199
*/
252200
public static function createEx(string $errMsg): RuntimeException
253201
{
254-
return new InvalidArgumentException($errMsg);
202+
return new self::$exClass($errMsg);
203+
}
204+
205+
/**
206+
* @return string
207+
*/
208+
public static function getExClass(): string
209+
{
210+
return self::$exClass;
211+
}
212+
213+
/**
214+
* @param string $exClass
215+
*/
216+
public static function setExClass(string $exClass): void
217+
{
218+
if ($exClass) {
219+
self::$exClass = $exClass;
220+
}
255221
}
256222
}

0 commit comments

Comments
 (0)