1010namespace PHPFuse \Validate ;
1111
1212use PHPFuse \Validate \Interfaces \InpInterface ;
13- use DateTime ;
1413use PHPFuse \Validate \Luhn ;
14+ use PHPFuse \DTO \Format \Str ;
15+ use InvalidArgumentException ;
16+ use DateTime ;
1517
1618class Inp implements InpInterface
1719{
20+ const WHITELIST_OPERATORS = [
21+ '!= ' ,
22+ '< ' ,
23+ '<= ' ,
24+ '<> ' ,
25+ '= ' ,
26+ '== ' ,
27+ '> ' ,
28+ '>= ' ,
29+ 'eq ' ,
30+ 'ge ' ,
31+ 'gt ' ,
32+ 'le ' ,
33+ 'lt ' ,
34+ 'ne '
35+ ];
36+
1837 private $ value ;
1938 private $ length ;
2039 private $ dateTime ;
2140 private $ luhn ;
41+ private $ getStr ;
42+
43+
44+ /**
45+ * Start instance
46+ * @param string $value the input value
47+ * @return self
48+ */
49+ public function __construct (string $ value )
50+ {
51+ $ this ->value = $ value ;
52+ $ this ->length = $ this ->getLength ($ value );
53+ $ this ->dateTime = new DateTime ("now " );
54+ $ this ->getStr = new Str ($ this ->value );
55+ }
2256
2357 /**
2458 * Start instance
@@ -27,11 +61,7 @@ class Inp implements InpInterface
2761 */
2862 public static function value (string $ value ): self
2963 {
30- $ inst = new self ();
31- $ inst ->value = $ value ;
32- $ inst ->length = $ inst ->getLength ($ value );
33- $ inst ->dateTime = new DateTime ("now " );
34- return $ inst ;
64+ return new self ($ value );
3565 }
3666
3767 /**
@@ -151,7 +181,7 @@ public function findInString(string $match, ?int $pos = null): bool
151181 */
152182 public function phone (): bool
153183 {
154- $ val = str_replace ( [" " , "- " , "— " , "– " , "( " , ") " ], ["" , "" , "" , "" , "" , "" ], $ this -> value );
184+ $ val = ( string ) $ this -> getStr -> replace ( [" " , "- " , "— " , "– " , "( " , ") " ], ["" , "" , "" , "" , "" , "" ]);
155185 $ match = preg_match ('/^[0-9]{7,14}+$/ ' , $ val );
156186 $ strict = preg_match ('/^\+[0-9]{1,2}[0-9]{6,13}$/ ' , $ val );
157187 return ($ strict || $ match );
@@ -165,7 +195,7 @@ public function phone(): bool
165195 */
166196 public function zip (int $ arg1 , int $ arg2 = null ): bool
167197 {
168- $ this ->value = str_replace ([" " , "- " , "— " , "– " ], ["" , "" , "" , "" ], $ this ->value );
198+ $ this ->value = ( string ) $ this -> getStr -> replace ([" " , "- " , "— " , "– " ], ["" , "" , "" , "" ], $ this ->value );
169199 $ this ->length = $ this ->getLength ($ this ->value );
170200 return ($ this ->int () && $ this ->length ($ arg1 , $ arg2 ));
171201 }
@@ -266,20 +296,6 @@ public function length(int $arg1, int $arg2 = null): bool
266296 return false ;
267297 }
268298
269- /**
270- * Value string length of OTHER field is more than start ($arg1) or between start ($arg1) and end ($arg2)
271- * @param string $key HTTP Post KEY
272- * @param int $arg1 start length
273- * @param int|null $arg2 end length
274- * @return bool
275- */
276- public function hasLength (string $ key , int $ arg1 , int $ arg2 = null ): bool
277- {
278- $ post = ($ _POST [$ key ] ?? 0 );
279- $ continue = ((int )$ post === 1 );
280- return (!$ continue || $ this ->length ($ arg1 , $ arg2 ));
281- }
282-
283299 /**
284300 * Value string length is equal to ($arg1)
285301 * @param int $arg1 length
@@ -323,13 +339,16 @@ public function validVersion($strict = false): bool
323339
324340 /**
325341 * Validate/compare if a version is equal/more/equalMore/less... e.g than withVersion
326- * @param string $withVersion [description]
327- * @param string $operator [description]
342+ * @param string $withVersion
343+ * @param '!='|'<'|'<='|'<>'|'='|'=='|'>'|'>='|'eq'|'ge'|'gt'|'le'|'lt'|'ne' $operator
328344 * @return bool
329345 */
330346 public function versionCompare (string $ withVersion , string $ operator = ">= " ): bool
331347 {
332- return (version_compare ((string )$ this ->value , $ withVersion , $ operator ) >= 0 );
348+ if (in_array ($ operator , self ::WHITELIST_OPERATORS )) {
349+ return (version_compare ((string )$ this ->value , $ withVersion , $ operator ) >= 0 );
350+ }
351+ return false ;
333352 }
334353
335354 /**
@@ -521,13 +540,13 @@ public function age(int $arg1): bool
521540
522541 /**
523542 * Check if is valid domain
524- * @param boolean $flag stricter = true
543+ * @param bool $strict stricter = true
525544 * @return bool
526545 */
527- public function domain ($ flag = true ): bool
546+ public function domain (bool $ strict = true ): bool
528547 {
529- $ flag = ($ flag ) ? FILTER_FLAG_HOSTNAME : false ;
530- return (filter_var ((string )$ this ->value , FILTER_VALIDATE_DOMAIN , $ flag ) !== false );
548+ $ strict = ($ strict ) ? FILTER_FLAG_HOSTNAME : 0 ;
549+ return (filter_var ((string )$ this ->value , FILTER_VALIDATE_DOMAIN , $ strict ) !== false );
531550 }
532551
533552 /**
@@ -536,13 +555,7 @@ public function domain($flag = true): bool
536555 */
537556 public function url (): bool
538557 {
539- $ val = (string )$ this ->value ;
540-
541- // Only used to pass validation will not change any data
542- $ val = str_replace (['{{root}} ' , '{{url}} ' ], ["https://example.se " , "https://example.se/ " ], $ val );
543- $ val = str_replace (["å " , "ä " , "ö " ], ["a " , "a " , "o " ], strtolower ($ val ));
544-
545- return (filter_var ($ val , FILTER_VALIDATE_URL ) !== false );
558+ return (filter_var ($ this ->value , FILTER_VALIDATE_URL ) !== false );
546559 }
547560
548561 /**
0 commit comments