Completed
Push — master ( d7d537...b89492 )
by Philip
08:02
created

DateTimeComparator::getDateTimes()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Valdi package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Valdi\Validator;
13
14
use Valdi\ValidationException;
15
16
/**
17
 * Abstract validator to compare date times.
18
 * For the format, see:
19
 * http://php.net/manual/en/datetime.createfromformat.php
20
 */
21
abstract class DateTimeComparator extends ParametrizedValidator {
22
23
    /**
24
     * Holds the amount of parameters.
25
     */
26
    protected $amountOfParameters = 1;
27
28
    /**
29
     * Holds the type of the validator.
30
     */
31
    protected $type;
32
33
    /**
34
     * Compares date times.
35
     *
36
     * @param \DateTime $date
37
     * the first date to compare
38
     * @param \DateTime[] $datetimes
39
     * the date times to compare to
40
     *
41
     * @return boolean
42
     * true if the dates compare
43
     */
44
    abstract protected function compare($date, array $datetimes);
45
46
    /**
47
     * Gets a date time format from the parameters if given or a default one.
48
     *
49
     * @param string[] $parameters
50
     * the parameters
51
     *
52
     * @return string
53
     * the date time format
54
     */
55
    protected function getDateTimeFormat($parameters) {
56
        $format = 'Y-m-d H:i:s';
57
        if (count($parameters) > $this->amountOfParameters) {
58
            $format = $parameters[$this->amountOfParameters];
59
        }
60
        return $format;
61
    }
62
63
    /**
64
     * Interprets the given parameters as date times and returns them.
65
     *
66
     * @param array $parameters
67
     * the paramters
68
     * @param string $format
69
     * the date time format
70
     *
71
     * @return \DateTime[]
72
     * the date times
73
     */
74
    protected function getDateTimes(array $parameters, $format) {
75
        $datetimes = array();
76
        for ($i = 0; $i < $this->amountOfParameters; ++$i) {
77
            $datetime = \DateTime::createFromFormat($format, $parameters[$i]);
78
            if ($datetime === false) {
79
                throw new ValidationException('"' . $this->type . '" expects a date of the format "' . $format . '".');
80
            }
81
            $datetimes[] = $datetime;
82
        }
83
        return $datetimes;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function validate($value, array $parameters) {
90
91
        $this->validateMinParameterCount($this->type, $this->amountOfParameters, $parameters);
92
93
        if (in_array($value, array('', null), true)) {
94
            return true;
95
        }
96
97
        $format = $this->getDateTimeFormat($parameters);
98
99
        $datetimes = $this->getDateTimes($parameters, $format);
100
        $date = \DateTime::createFromFormat($format, $value);
101
        if ($date === false) {
102
            return false;
103
        }
104
105
        return $this->compare($date, $datetimes);
106
    }
107
}
108