feat(Validator): Add regex and date validation methods
This commit is contained in:
@@ -339,6 +339,48 @@ class Validator
|
||||
return static::measure($subject) === (float) $size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the value matches a regular expression.
|
||||
*
|
||||
* The pattern is received verbatim (delimiters included), thanks to
|
||||
* splitArguments() treating "regex" as an intact argument, so patterns
|
||||
* that contain ':' or ',' - such as "/^a,b$/" - work as expected.
|
||||
*
|
||||
* @param mixed $subject The value to check.
|
||||
* @param string $pattern A PCRE pattern including its delimiters.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function regex(mixed $subject, mixed $pattern): bool
|
||||
{
|
||||
return preg_match((string) $pattern, (string) $subject) === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the value is a parseable date.
|
||||
*
|
||||
* Any format accepted by DateTime is valid (e.g. "2026-09-03", an ISO
|
||||
* datetime, or a timestamp string). Empty and non-scalar values fail.
|
||||
*
|
||||
* @param mixed $subject The value to check.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public static function date(mixed $subject): bool
|
||||
{
|
||||
if (!is_scalar($subject) || trim((string) $subject) === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
new \DateTime((string) $subject);
|
||||
|
||||
return true;
|
||||
} catch (Exception $e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the value is a valid email address.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user