From a4642d17e0e1ea018b128efdcc3db281461458b1 Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Wed, 4 Apr 2018 15:21:13 -0700 Subject: Move custom libs to the source tree --- libs/SimpleValidator/Validators/Date.php | 45 ++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 libs/SimpleValidator/Validators/Date.php (limited to 'libs/SimpleValidator/Validators/Date.php') diff --git a/libs/SimpleValidator/Validators/Date.php b/libs/SimpleValidator/Validators/Date.php new file mode 100644 index 00000000..4ec4b7fd --- /dev/null +++ b/libs/SimpleValidator/Validators/Date.php @@ -0,0 +1,45 @@ +formats = $formats; + } + + public function execute(array $data) + { + if ($this->isFieldNotEmpty($data)) { + foreach ($this->formats as $format) { + if ($this->isValidDate($data[$this->field], $format)) { + return true; + } + } + + return false; + } + + return true; + } + + public function isValidDate($value, $format) + { + $date = DateTime::createFromFormat($format, $value); + + if ($date !== false) { + $errors = DateTime::getLastErrors(); + if ($errors['error_count'] === 0 && $errors['warning_count'] === 0) { + return $date->getTimestamp() > 0; + } + } + + return false; + } +} -- cgit v1.2.3