Completed
Push — master ( 06258d...f8a67c )
by Philip
02:14
created

DateTimeComparator::getDateTimeFormat()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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