From 4fe73143894d02ddaf574909efd53f7faaeb63d9 Mon Sep 17 00:00:00 2001 From: kj Date: Thu, 3 Sep 2026 14:42:15 -0300 Subject: [PATCH] feat(Validator): Add regex and date validation methods --- src/Libs/Validator.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/src/Libs/Validator.php b/src/Libs/Validator.php index e69a5f3..fd985e2 100644 --- a/src/Libs/Validator.php +++ b/src/Libs/Validator.php @@ -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. *