33namespace Toolkit \Stdlib \Helper ;
44
55use InvalidArgumentException ;
6- use RuntimeException ;
6+ use LogicException ;
77use 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 }
0 commit comments