9
9
10
10
namespace MaplePHP \Validate ;
11
11
12
+ use ErrorException ;
12
13
use Exception ;
14
+ use http \Exception \InvalidArgumentException ;
15
+ use MaplePHP \DTO \MB ;
13
16
use MaplePHP \Validate \Interfaces \InpInterface ;
14
17
use MaplePHP \DTO \Format \Str ;
15
18
use DateTime ;
16
19
17
20
class Inp implements InpInterface
18
21
{
19
- const WHITELIST_OPERATORS = [
22
+ public const WHITELIST_OPERATORS = [
20
23
'!= ' ,
21
24
'< ' ,
22
25
'<= ' ,
@@ -42,22 +45,36 @@ class Inp implements InpInterface
42
45
43
46
/**
44
47
* Start instance
45
- * @param mixed $value the input value
48
+ * @param mixed $value the input value
49
+ * @throws ErrorException
46
50
*/
47
51
public function __construct (mixed $ value )
48
52
{
49
53
$ this ->value = $ value ;
50
54
$ this ->dateTime = new DateTime ("now " );
51
55
if (is_string ($ value ) || is_numeric ($ value )) {
52
- $ this ->length = $ this ->getLength ($ value );
56
+ $ this ->length = $ this ->getLength (( string ) $ value );
53
57
$ this ->getStr = new Str ($ this ->value );
54
58
}
55
59
}
56
60
61
+ /**
62
+ * Immutable: Validate against new value
63
+ * @param mixed $value
64
+ * @return InpInterface
65
+ */
66
+ public function withValue (mixed $ value ): InpInterface
67
+ {
68
+ $ inst = clone $ this ;
69
+ $ inst ->value = $ value ;
70
+ return $ inst ;
71
+ }
72
+
57
73
/**
58
74
* Start instance
59
- * @param string $value the input value
75
+ * @param string $value the input value
60
76
* @return self
77
+ * @throws ErrorException
61
78
*/
62
79
public static function value (mixed $ value ): self
63
80
{
@@ -66,12 +83,14 @@ public static function value(mixed $value): self
66
83
67
84
/**
68
85
* Get value string length
69
- * @param string $value
86
+ * @param string $value
70
87
* @return int
88
+ * @throws ErrorException
71
89
*/
72
90
public function getLength (string $ value ): int
73
91
{
74
- return strlen ($ value );
92
+ $ mb = new MB ($ value );
93
+ return (int )$ mb ->strlen ();
75
94
}
76
95
77
96
/**
@@ -181,6 +200,9 @@ public function findInString(string $match, ?int $pos = null): bool
181
200
*/
182
201
public function phone (): bool
183
202
{
203
+ if (is_null ($ this ->getStr )) {
204
+ return false ;
205
+ }
184
206
$ val = (string )$ this ->getStr ->replace ([" " , "- " , "— " , "– " , "( " , ") " ], ["" , "" , "" , "" , "" , "" ]);
185
207
$ match = preg_match ('/^[0-9]{7,14}+$/ ' , $ val );
186
208
$ strict = preg_match ('/^\+[0-9]{1,2}[0-9]{6,13}$/ ' , $ val );
@@ -189,15 +211,19 @@ public function phone(): bool
189
211
190
212
/**
191
213
* Check if is valid ZIP
192
- * @param int $arg1 start length
193
- * @param int|null $arg2 end length
214
+ * @param int $arg1 start length
215
+ * @param int|null $arg2 end length
194
216
* @return bool
217
+ * @throws ErrorException
195
218
*/
196
219
public function zip (int $ arg1 , int $ arg2 = null ): bool
197
220
{
221
+ if (is_null ($ this ->getStr )) {
222
+ return false ;
223
+ }
198
224
$ this ->value = (string )$ this ->getStr ->replace ([" " , "- " , "— " , "– " ], ["" , "" , "" , "" ]);
199
225
$ this ->length = $ this ->getLength ($ this ->value );
200
- return ($ this ->int () && $ this ->length ($ arg1 , $ arg2 ));
226
+ return ($ this ->isInt () && $ this ->length ($ arg1 , $ arg2 ));
201
227
}
202
228
203
229
/**
@@ -210,12 +236,6 @@ public function isFloat(): bool
210
236
return (filter_var ($ this ->value , FILTER_VALIDATE_FLOAT ) !== false );
211
237
}
212
238
213
- // Deprecated
214
- public function float (): bool
215
- {
216
- return $ this ->isFloat ();
217
- }
218
-
219
239
/**
220
240
* Is value int
221
241
* Will validate whether a string is a valid integer (User input is always a string)
@@ -226,12 +246,6 @@ public function isInt(): bool
226
246
return (filter_var ($ this ->value , FILTER_VALIDATE_INT ) !== false );
227
247
}
228
248
229
- // Deprecated
230
- public function int (): bool
231
- {
232
- return $ this ->isInt ();
233
- }
234
-
235
249
/**
236
250
* Is value string
237
251
* @return bool
@@ -241,8 +255,11 @@ public function isString(): bool
241
255
return is_string ($ this ->value );
242
256
}
243
257
244
- // Deprecated
245
- public function string (): bool
258
+ /**
259
+ * Is value string
260
+ * @return bool
261
+ */
262
+ public function isStr (): bool
246
263
{
247
264
return $ this ->isString ();
248
265
}
@@ -256,12 +273,6 @@ public function isArray(): bool
256
273
return is_array ($ this ->value );
257
274
}
258
275
259
- // Deprecated
260
- public function array (): bool
261
- {
262
- return $ this ->isArray ();
263
- }
264
-
265
276
/**
266
277
* Is value object
267
278
* @return bool
@@ -271,12 +282,6 @@ public function isObject(): bool
271
282
return is_object ($ this ->value );
272
283
}
273
284
274
- // Deprecated
275
- public function object (): bool
276
- {
277
- return $ this ->isObject ();
278
- }
279
-
280
285
/**
281
286
* Is value bool
282
287
* @return bool
@@ -286,12 +291,6 @@ public function isBool(): bool
286
291
return (is_bool ($ this ->value ));
287
292
}
288
293
289
- // Deprecated
290
- public function bool (): bool
291
- {
292
- return $ this ->isBool ();
293
- }
294
-
295
294
/**
296
295
* Check if the value itself can be Interpreted as a bool value
297
296
* E.g. If value === ([on, off], [yes, no], [1, 0] or [true, false])
@@ -305,10 +304,58 @@ public function isBoolVal(): bool
305
304
return ($ true || $ false );
306
305
}
307
306
308
- // Deprecated
309
- public function boolVal (): bool
307
+ /**
308
+ * Is null
309
+ * @return bool
310
+ */
311
+ public function isNull (): bool
312
+ {
313
+ return is_null ($ this ->value );
314
+ }
315
+
316
+ /**
317
+ * Is file
318
+ * @return bool
319
+ */
320
+ public function isFile (): bool
321
+ {
322
+ return is_file ($ this ->value );
323
+ }
324
+
325
+ /**
326
+ * Is directory
327
+ * @return bool
328
+ */
329
+ public function isDir (): bool
330
+ {
331
+ return is_dir ($ this ->value );
332
+ }
333
+
334
+ /**
335
+ * Is resource
336
+ * @return bool
337
+ */
338
+ public function isResource (): bool
339
+ {
340
+ return is_resource ($ this ->value );
341
+ }
342
+
343
+ /**
344
+ * Is writable
345
+ * @return bool
346
+ */
347
+ public function isWritable (): bool
348
+ {
349
+ return is_writable ($ this ->value );
350
+ }
351
+
352
+ /**
353
+ * Is readable
354
+ * @return bool
355
+ */
356
+ public function isReadable (): bool
310
357
{
311
- return $ this ->isBoolVal ( );
358
+ return is_readable ( $ this ->value );
312
359
}
313
360
314
361
/**
@@ -440,7 +487,7 @@ public function validVersion(bool $strict = false): bool
440
487
/**
441
488
* Validate/compare if a version is equal/more/equalMore/less... e.g than withVersion
442
489
* @param string $withVersion
443
- * @param string $operator '!='|'<'|'<='|'<>'|'='|'=='|'>'|'>='|'eq'|'ge'|'gt'|'le'|'lt'|'ne'
490
+ * @param '!='|'<'|'<='|'<>'|'='|'=='|'>'|'>='|'eq'|'ge'|'gt'|'le'|'lt'|'ne' $operator
444
491
* @return bool
445
492
*/
446
493
public function versionCompare (string $ withVersion , string $ operator = "== " ): bool
@@ -586,10 +633,10 @@ public function dateRange(string $format = "Y-m-d H:i"): array|false
586
633
*/
587
634
public function age (int $ arg1 ): bool
588
635
{
589
- $ now = $ this ->dateTime ->format ("Y " );
636
+ $ now = ( int ) $ this ->dateTime ->format ("Y " );
590
637
$ dateTime = new DateTime ($ this ->value );
591
- $ birth = $ dateTime ->format ("Y " );
592
- $ age = (int )( $ now - $ birth );
638
+ $ birth = ( int ) $ dateTime ->format ("Y " );
639
+ $ age = ($ now - $ birth );
593
640
return ($ age <= $ arg1 );
594
641
}
595
642
@@ -664,8 +711,9 @@ private function getHost(string $host): string
664
711
665
712
/**
666
713
* Validate multiple. Will return true if "one" matches
667
- * @param array $arr
714
+ * @param array $arr
668
715
* @return bool
716
+ * @throws ErrorException
669
717
*/
670
718
public function oneOf (array $ arr ): bool
671
719
{
@@ -681,8 +729,9 @@ public function oneOf(array $arr): bool
681
729
682
730
/**
683
731
* Validate multiple. Will return true if "all" matches
684
- * @param array $arr
732
+ * @param array $arr
685
733
* @return bool
734
+ * @throws ErrorException
686
735
*/
687
736
public function allOf (array $ arr ): bool
688
737
{
0 commit comments