Completed
Push — master ( b50394...04cf70 )
by Philip
02:32
created

AbstractDateTimeComparator::isValid()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 20
rs 8.8571
cc 5
eloc 11
nc 6
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 AbstractDateTimeComparator extends AbstractParametrizedValidator {
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
     * Holds whether to parse the parameters as \DateTimes so the child class
35
     * can decide.
36
     */
37
    protected $dateTimeParameters = true;
38
39
    /**
40
     * Compares date times.
41
     *
42
     * @param \DateTime $date
43
     * the first date to compare
44
     * @param \DateTime[] $datetimes
45
     * the date times to compare to
46
     * @param array $parameters
47
     * the original validator parameters
48
     *
49
     * @return boolean
50
     * true if the dates compare
51
     */
52
    abstract protected function isValidComparison(\DateTime $date, array $datetimes, array $parameters);
53
54
    /**
55
     * Gets a date time format from the parameters if given or a default one.
56
     *
57
     * @param string[] $parameters
58
     * the parameters
59
     *
60
     * @return string
61
     * the date time format
62
     */
63
    protected function getDateTimeFormat($parameters) {
64
        $format          = 'Y-m-d H:i:s';
65
        $parametersCount = count($parameters);
66
        if ($parametersCount > $this->amountOfParameters) {
67
            $format = $parameters[$parametersCount - 1];
68
        }
69
        return $format;
70
    }
71
72
    /**
73
     * Interprets the given parameters as date times and returns them.
74
     *
75
     * @param array $parameters
76
     * the paramters
77
     * @param string $format
78
     * the date time format
79
     *
80
     * @return \DateTime[]
81
     * the date times
82
     */
83
    protected function getDateTimes(array $parameters, $format) {
84
        $datetimes = array();
85
        for ($i = 0; $i < $this->amountOfParameters; ++$i) {
86
            $datetime = \DateTime::createFromFormat($format, $parameters[$i]);
87
            if ($datetime === false) {
88
                throw new ValidationException('"' . $this->type . '" expects a date of the format "' . $format . '".');
89
            }
90
            $datetimes[] = $datetime;
91
        }
92
        return $datetimes;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98
    public function isValid($value, array $parameters) {
99
100
        if (count($parameters) < $this->amountOfParameters) {
101
            throw new ValidationException('"' . $this->type . '" expects at least ' . $this->amountOfParameters . ' parameter.');
102
        }
103
104
        if (in_array($value, array('', null), true)) {
105
            return true;
106
        }
107
108
        $format = $this->getDateTimeFormat($parameters);
109
110
        $datetimes = $this->dateTimeParameters ? $this->getDateTimes($parameters, $format) : array();
111
        $date      = \DateTime::createFromFormat($format, $value);
112
        if ($date === false) {
113
            return false;
114
        }
115
116
        return $this->isValidComparison($date, $datetimes, $parameters);
117
    }
118
}
119