summaryrefslogtreecommitdiff
path: root/src/Validators
diff options
context:
space:
mode:
authorAsko Nomm <asko@bien.ee>2022-03-06 22:48:02 +0100
committerAsko Nomm <asko@bien.ee>2022-03-06 22:48:02 +0100
commitf313168d4b53b32b1ee3ce3e2e9ed749848f6985 (patch)
treedc55fed561e6d09ae8a9cd41e4f313d969abd36f /src/Validators
parent49a2e23470c291004f2fb203401408225fb61831 (diff)
(MINOR) Implement DateFormatValidator
Diffstat (limited to 'src/Validators')
-rw-r--r--src/Validators/DateFormatValidator.php45
1 files changed, 45 insertions, 0 deletions
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 @@
+<?php
+
+namespace Askonomm\Hird\Validators;
+
+/**
+ * Implements a DateFormat validator that has a job
+ * of validating that a given date is in the correct
+ * format.
+ *
+ * @author Asko Nomm <asko@bien.ee>
+ */
+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}.";
+ }
+}