Initial commit
This commit is contained in:
78
rss/php/classes/core/Validator.php
Normal file
78
rss/php/classes/core/Validator.php
Normal file
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Vor\core;
|
||||
|
||||
class Validator{
|
||||
|
||||
public static function int($val) : bool{
|
||||
return filter_var($val, FILTER_VALIDATE_INT) !== false;
|
||||
}
|
||||
|
||||
public static function float($val) : bool{
|
||||
return filter_var($val, FILTER_VALIDATE_FLOAT) !== false;
|
||||
}
|
||||
|
||||
public static function string($val, int $minLen = 1): bool{
|
||||
if(!is_string($val)){
|
||||
return false;
|
||||
}
|
||||
|
||||
return mb_strlen(trim($val)) >= $minLen;
|
||||
}
|
||||
|
||||
public static function date($date, string $format = 'Y-m-d'): bool {
|
||||
try{
|
||||
$d = \DateTime::createFromFormat($format, (string)$date);
|
||||
return $d && $d->format($format) === (string)$date;
|
||||
}catch(\Throwable $e){
|
||||
error_log($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function json($val): bool{
|
||||
if(!is_string($val) || $val === ''){
|
||||
return false;
|
||||
}
|
||||
|
||||
try{
|
||||
json_decode($val, true, 512, JSON_THROW_ON_ERROR);
|
||||
return true;
|
||||
}catch(\JsonException $e){
|
||||
error_log($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static function email($val): bool{
|
||||
$val = trim((string)$val);
|
||||
|
||||
if(preg_match("/[\r\n]/", $val)){
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) filter_var($val, FILTER_VALIDATE_EMAIL);
|
||||
}
|
||||
|
||||
public static function domain($val): bool{
|
||||
$val = trim((string)$val);
|
||||
|
||||
if(preg_match("/[\r\n]/", $val)){
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) filter_var($val, FILTER_VALIDATE_DOMAIN, FILTER_FLAG_HOSTNAME);
|
||||
}
|
||||
|
||||
public static function minLen($val, int $min): bool{
|
||||
return mb_strlen(trim((string)$val)) >= $min;
|
||||
}
|
||||
|
||||
public static function maxLen($val, int $max): bool{
|
||||
return mb_strlen(trim((string)$val)) <= $max;
|
||||
}
|
||||
|
||||
public static function regex($val, string $pattern): bool{
|
||||
return (bool) preg_match($pattern, (string)$val);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user