Completed
Push — master ( 666e0b...f8cd2e )
by Philip
02:25
created

AbstractDateTimeComparator::getInvalidDetails()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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
        if (!$this->dateTimeParameters) {
85
            return array();
86
        }
87
        $datetimes = array();
88
        for ($i = 0; $i < $this->amountOfParameters; ++$i) {
89
            $datetime = \DateTime::createFromFormat($format, $parameters[$i]);
90
            if ($datetime === false) {
91
                throw new ValidationException('"'.$this->type.'" expects a date of the format "'.$format.'".');
92
            }
93
            $datetimes[] = $datetime;
94
        }
95
        return $datetimes;
96
    }
97
98
    /**
99
     * {@inheritdoc}
100
     */
101
    public function isValid($value, array $parameters) {
102
103
        if (count($parameters) < $this->amountOfParameters) {
104
            throw new ValidationException('"'.$this->type.'" expects at least '.$this->amountOfParameters.' parameter.');
105
        }
106
107
        if (in_array($value, array('', null), true)) {
108
            return true;
109
        }
110
111
        $format = $this->getDateTimeFormat($parameters);
112
113
        $datetimes = $this->getDateTimes($parameters, $format);
114
        $date      = \DateTime::createFromFormat($format, $value);
115
        if ($date === false) {
116
            return false;
117
        }
118
119
        return $this->isValidComparison($date, $datetimes, $parameters);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function getInvalidDetails() {
126
        return $this->type;
127
    }
128
}
129