summaryrefslogtreecommitdiff
path: root/vendor/SimpleValidator/Validators/Date.php
blob: 36b59fbee48982abefe890fcf6148dc98c359465 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?php

namespace SimpleValidator\Validators;

use SimpleValidator\Base;

class Date extends Base
{
    private $format;

    public function __construct($field, $error_message, $format)
    {
        parent::__construct($field, $error_message);
        $this->format = $format;
    }

    public function execute(array $data)
    {
        if (isset($data[$this->field]) && $data[$this->field] !== '') {

            $date = \DateTime::createFromFormat($this->format, $data[$this->field]);

            if ($date !== false) {
                $errors = \DateTime::getLastErrors();
                return $errors['error_count'] === 0 && $errors['warning_count'] === 0;
            }

            return false;
        }

        return true;
    }
}