3
3
namespace Toolkit \Stdlib \Helper ;
4
4
5
5
use InvalidArgumentException ;
6
- use RuntimeException ;
6
+ use LogicException ;
7
7
use function in_array ;
8
8
9
9
/**
@@ -21,11 +21,18 @@ class Assert
21
21
/**
22
22
* @param mixed $value
23
23
* @param string $errMsg
24
+ * @param bool $isPrefix
24
25
*/
25
- public static function notEmpty (mixed $ value , string $ errMsg = '' ): void
26
+ public static function notEmpty (mixed $ value , string $ errMsg = '' , bool $ isPrefix = false ): void
26
27
{
27
28
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 );
29
36
}
30
37
}
31
38
@@ -43,11 +50,18 @@ public static function notNull(mixed $value, string $errMsg = ''): void
43
50
/**
44
51
* @param string $value
45
52
* @param string $errMsg
53
+ * @param bool $isPrefix
46
54
*/
47
- public static function notBlank (string $ value , string $ errMsg = '' ): void
55
+ public static function notBlank (string $ value , string $ errMsg = '' , bool $ isPrefix = false ): void
48
56
{
49
57
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 );
51
65
}
52
66
}
53
67
@@ -127,54 +141,82 @@ public static function notZero(int $value, string $errMsg = ''): void
127
141
}
128
142
129
143
/**
130
- * Natural number. >= 0
144
+ * Positive integer. should > 0
131
145
*
132
146
* @param int $value
133
147
* @param string $errMsg
148
+ * @param bool $isPrefix
134
149
*/
135
- public static function naturalInt (int $ value , string $ errMsg = '' ): void
150
+ public static function intShouldGt0 (int $ value , string $ errMsg = '' , bool $ isPrefix = false ): void
136
151
{
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 );
139
160
}
140
161
}
141
162
142
163
/**
143
- * Positive integer. should > 0
164
+ * Positive integer. should >= 0
144
165
*
145
166
* @param int $value
146
167
* @param string $errMsg
168
+ * @param bool $isPrefix
147
169
*/
148
- public static function intShouldGt0 (int $ value , string $ errMsg = '' ): void
170
+ public static function intShouldGte0 (int $ value , string $ errMsg = '' , bool $ isPrefix = false ): void
149
171
{
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 );
152
180
}
153
181
}
154
182
155
183
/**
156
- * Positive integer. should >= 0
184
+ * Positive integer. should > 0
157
185
*
158
186
* @param int $value
159
187
* @param string $errMsg
188
+ * @param bool $isPrefix
160
189
*/
161
- public static function intShouldGte0 (int $ value , string $ errMsg = '' ): void
190
+ public static function positiveInt (int $ value , string $ errMsg = '' , bool $ isPrefix = false ): void
162
191
{
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 );
165
200
}
166
201
}
167
202
168
203
/**
169
- * Positive integer. > 0
204
+ * Natural number. should >= 0
170
205
*
171
206
* @param int $value
172
207
* @param string $errMsg
208
+ * @param bool $isPrefix
173
209
*/
174
- public static function positiveInt (int $ value , string $ errMsg = '' ): void
210
+ public static function naturalInt (int $ value , string $ errMsg = '' , bool $ isPrefix = false ): void
175
211
{
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 );
178
220
}
179
221
}
180
222
@@ -211,7 +253,7 @@ public static function arrayHasKeys(array $data, array $keys, string $errMsg = '
211
253
*/
212
254
public static function arrayHasNoEmptyKey (array $ data , string $ key , string $ errMsg = '' ): void
213
255
{
214
- if (! isset ( $ data [ $ key ]) || empty ($ data [$ key ])) {
256
+ if (empty ($ data [$ key ])) {
215
257
throw static ::createEx ($ errMsg ?: "Data must contains key ' $ key' and value non-empty " );
216
258
}
217
259
}
@@ -221,9 +263,9 @@ public static function arrayHasNoEmptyKey(array $data, string $key, string $errM
221
263
/**
222
264
* @param string $errMsg
223
265
*
224
- * @return RuntimeException
266
+ * @return LogicException
225
267
*/
226
- public static function createEx (string $ errMsg ): RuntimeException
268
+ public static function createEx (string $ errMsg ): LogicException
227
269
{
228
270
return new self::$ exClass ($ errMsg );
229
271
}
0 commit comments