Completed
Push — master ( 37a719...4fa3c4 )
by Philip
02:36
created

AbstractDateTimeComparator::isValid()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
rs 9.2
nc 5
cc 4
eloc 10
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 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
     * Holds whether to parse the parameters as \DateTimes so the child class
35
     * can decide.
36
     */
37
    protected $parseParametersAsDateTimes = true;
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $parseParametersAsDateTimes exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
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 compare(\DateTime $date, array $datetimes, array $parameters);
0 ignored issues
show
Coding Style introduced by
function compare() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
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
        $this->validateMinParameterCount($this->type, $this->amountOfParameters, $parameters);
101
102
        if (in_array($value, array('', null), true)) {
103
            return true;
104
        }
105
106
        $format = $this->getDateTimeFormat($parameters);
107
108
        $datetimes = $this->parseParametersAsDateTimes ? $this->getDateTimes($parameters, $format) : array();
109
        $date      = \DateTime::createFromFormat($format, $value);
110
        if ($date === false) {
111
            return false;
112
        }
113
114
        return $this->compare($date, $datetimes, $parameters);
115
    }
116
}
117