Completed
Push — master ( 5ad706...418f81 )
by Sebastian
03:53
created

DateCompare   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Test Coverage

Coverage 94.44%

Importance

Changes 0
Metric Value
eloc 44
dl 0
loc 128
ccs 34
cts 36
cp 0.9444
rs 10
c 0
b 0
f 0
wmc 14

5 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 5 1
A switchOperator() 0 18 6
A getMessage() 0 3 1
A concreteValidate() 0 24 5
A getDateTimeObject() 0 3 1
1
<?php
2
3
/**
4
 * Linna Filter
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types = 1);
11
12
namespace Linna\Filter\Rules;
13
14
use DateTime;
15
use UnexpectedValueException;
16
17
/**
18
 * Compare two dates using >, <, >=, <=, = operators.
19
 */
20
class DateCompare extends AbstractDate implements RuleValidateInterface
21
{
22
    /**
23
     * @var array Rule properties
24
     */
25
    public static $config = [
26
        'class' => 'DateCompare',
27
        'full_class' => __CLASS__,
28
        'alias' => ['datecompare', 'datcmp', 'dc'],
29
        'args_count' => 3,
30
        'args_type' => ['string', 'string', 'string'],
31
        'has_validate' => true,
32
        //'has_sanitize' => false
33
    ];
34
35
    /**
36
     * @var string Valid date.
37
     */
38
    private $date;
39
40
    /**
41
     * @var DateTime Valid date in DateTime object.
42
     */
43
    private $dateTimeObject;
44
45
    /**
46
     * @var string Error message
47
     */
48
    private $message = '';
49
50
    /**
51
     * Validate.
52
     *
53
     * @return bool
54
     */
55 33
    public function validate(): bool
56
    {
57 33
        $args = func_get_args();
58
59 33
        return $this->concreteValidate($args[0], $args[1], $args[2], $args[3]);
60
    }
61
62
    /**
63
     * Concrete validate.
64
     *
65
     * @param string $received
66
     * @param string $operator
67
     * @param string $format
68
     * @param string $compare
69
     *
70
     * @return bool
71
     */
72 33
    private function concreteValidate(string $received, string $operator, string $format, string $compare): bool
73
    {
74 33
        $dateReceived = DateTime::createFromFormat($format, $received);
75 33
        $dateCompare = DateTime::createFromFormat($format, $compare);
76
77 33
        if (!($dateReceived && $dateCompare)) {
78 1
            $this->message = "Received date is not in expected format {$format}";
79 1
            return true;
80
        }
81
82 32
        if ($this->dateHaveNoTime($format)) {
83 17
            $dateReceived->setTime(0, 0, 0);
84 17
            $dateCompare->setTime(0, 0, 0);
85
        }
86
87 32
        if ($this->switchOperator($operator, $dateReceived, $dateCompare)) {
88 15
            $this->date = $dateReceived->format($format);
89 15
            $this->dateTimeObject = $dateReceived;
90 15
            return false;
91
        }
92
93 16
        $this->message = "Received date is not {$operator} {$compare}";
94
95 16
        return true;
96
    }
97
98
    /**
99
     * Return DateTime object.
100
     *
101
     * @return DateTime
102
     */
103 15
    public function getDateTimeObject(): DateTime
104
    {
105 15
        return $this->dateTimeObject;
106
    }
107
108
    /**
109
     * Perform correct operation from passed operator.
110
     *
111
     * @param string   $operator
112
     * @param DateTime $dateReceived
113
     * @param DateTime $dateCompare
114
     *
115
     * @return bool
116
     *
117
     * @throws UnexpectedValueException if unknown operator is provided.
118
     */
119 32
    private function switchOperator(string $operator, DateTime &$dateReceived, DateTime &$dateCompare): bool
120
    {
121 32
        $received = $dateReceived->format(DateTime::ATOM);
122 32
        $compare = $dateCompare->format(DateTime::ATOM);
123
124
        switch ($operator) {
125 32
            case '>': //greater than
126 6
                return $received > $compare;
127 26
            case '<': //less than
128 6
                return $received < $compare;
129 20
            case '>=': //greater than or equal
130 6
                return $received >= $compare;
131 14
            case '<=': //less than or equal
132 6
                return $received <= $compare;
133 8
            case '=': //equal
134 7
                return $received === $compare;
135
            default:
136 1
                throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted >, <, >=, <=, =");
137
        }
138
    }
139
140
    /**
141
     * Return error message.
142
     *
143
     * @return string Error message
144
     */
145
    public function getMessage(): string
146
    {
147
        return $this->message;
148
    }
149
}
150