Completed
Push — master ( eaf770...e2d746 )
by Sebastian
04:09
created

DateCompare::validate()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 14
nc 5
nop 4
dl 0
loc 24
ccs 15
cts 15
cp 1
crap 5
rs 9.4888
c 0
b 0
f 0
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 RuleInterface
21
{
22
    /**
23
     * @var array Arguments expected.
24
     */
25
    private $arguments = ['string', 'string', 'string'];
0 ignored issues
show
introduced by
The private property $arguments is not used, and could be removed.
Loading history...
26
27
    /**
28
     * @var string Valid date.
29
     */
30
    private $date;
31
32
    /**
33
     * @var DateTime Valid date in DateTime object.
34
     */
35
    private $dateTimeObject;
36
37
    /**
38
     * @var string Error message
39
     */
40
    private $message = '';
41
42
    /**
43
     * Validate.
44
     *
45
     * @param string $received
46
     * @param string $operator
47
     * @param string $format
48
     * @param string $compare
49
     *
50
     * @return bool
51
     */
52 33
    public function validate(string $received, string $operator, string $format, string $compare): bool
53
    {
54 33
        $dateReceived = DateTime::createFromFormat($format, $received);
55 33
        $dateCompare = DateTime::createFromFormat($format, $compare);
56
57 33
        if (!($dateReceived && $dateCompare)) {
58 1
            $this->message = "Received date is not in expected format {$format}";
59 1
            return true;
60
        }
61
62 32
        if ($this->dateHaveNoTime($format)) {
63 17
            $dateReceived->setTime(0, 0, 0);
64 17
            $dateCompare->setTime(0, 0, 0);
65
        }
66
67 32
        if ($this->switchOperator($operator, $dateReceived, $dateCompare)) {
68 15
            $this->date = $dateReceived->format($format);
69 15
            $this->dateTimeObject = $dateReceived;
70 15
            return false;
71
        }
72
73 16
        $this->message = "Received date is not {$operator} {$compare}";
74
75 16
        return true;
76
    }
77
78
    /**
79
     * Return DateTime object.
80
     *
81
     * @return DateTime
82
     */
83 15
    public function getDateTimeObject(): DateTime
84
    {
85 15
        return $this->dateTimeObject;
86
    }
87
88
    /**
89
     * Perform correct operation from passed operator.
90
     *
91
     * @param string   $operator
92
     * @param DateTime $dateReceived
93
     * @param DateTime $dateCompare
94
     *
95
     * @return bool
96
     *
97
     * @throws UnexpectedValueException if unknown operator is provided.
98
     */
99 32
    private function switchOperator(string $operator, DateTime &$dateReceived, DateTime &$dateCompare): bool
100
    {
101 32
        $received = $dateReceived->format(DateTime::ATOM);
102 32
        $compare = $dateCompare->format(DateTime::ATOM);
103
104
        switch ($operator) {
105 32
            case '>': //greater than
106 6
                return $received > $compare;
107 26
            case '<': //less than
108 6
                return $received < $compare;
109 20
            case '>=': //greater than or equal
110 6
                return $received >= $compare;
111 14
            case '<=': //less than or equal
112 6
                return $received <= $compare;
113 8
            case '=': //equal
114 7
                return $received === $compare;
115
            default:
116 1
                throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted >, <, >=, <=, =");
117
        }
118
    }
119
120
    /**
121
     * Return error message.
122
     *
123
     * @return string Error message
124
     */
125
    public function getMessage(): string
126
    {
127
        return $this->message;
128
    }
129
}
130