From f313168d4b53b32b1ee3ce3e2e9ed749848f6985 Mon Sep 17 00:00:00 2001 From: Asko Nomm Date: Sun, 6 Mar 2022 22:48:02 +0100 Subject: (MINOR) Implement DateFormatValidator --- src/Validators/DateFormatValidator.php | 45 ++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/Validators/DateFormatValidator.php (limited to 'src/Validators') diff --git a/src/Validators/DateFormatValidator.php b/src/Validators/DateFormatValidator.php new file mode 100644 index 0000000..fedbb7d --- /dev/null +++ b/src/Validators/DateFormatValidator.php @@ -0,0 +1,45 @@ + + */ +class DateFormatValidator implements Validator +{ + /** + * Returns a boolean `true` when given `$value` is a valid e-mail + * address. Returns `false` otherwise. + * + * @param string $field + * @param mixed $value + * @param mixed $modifier + * @return boolean + */ + public static function validate(string $field, mixed $value, mixed $modifier = null): bool + { + if ($value) { + $datetime = \DateTime::createFromFormat($modifier, $value); + + return $datetime !== false && !array_sum($datetime::getLastErrors()); + } + + return true; + } + + /** + * Composes the error message in case the validation fails. + * + * @param string $field + * @param mixed $modifier + * @return string + */ + public static function composeError(string $field, mixed $modifier = null): string + { + return "${field} does not match the required date format ${modifier}."; + } +} -- cgit v1.2.3