Completed
Push — master ( d33ed4...3917d5 )
by Sebastian
03:14
created

DateCompare   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 85
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
B validate() 0 21 5
B switchOperator() 0 18 6
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 Linna\Filter\AbstractDate;
15
use DateTime;
16
use UnexpectedValueException;
17
18
/**
19
 * Compare two dates using >, <, >=, <=, = operators.
20
 */
21
class DateCompare extends AbstractDate
22
{
23
    /**
24
     * @var array Arguments expected.
25
     */
26
    private $arguments = ['string', 'string', 'string'];
0 ignored issues
show
introduced by
The private property $arguments is not used, and could be removed.
Loading history...
27
28
    /**
29
     * @var string Valid date.
30
     */
31
    private $date;
32
33
    /**
34
     *
35
     * @var DateTime Valid date in DateTime object.
36
     */
37
    private $dateTimeObject;
38
    
39
    /**
40
     * Validate.
41
     *
42
     * @return bool
43
     */
44 33
    public function validate($received, string $operator, string $format, string $compare): bool
45
    {
46 33
        $dateReceived = DateTime::createFromFormat($format, $received);
47 33
        $dateCompare = DateTime::createFromFormat($format, $compare);
48
49 33
        if (!($dateReceived && $dateCompare)) {
50 1
            return true;
51
        }
52
        
53 32
        if ($this->dateHaveNoTime($format)) {
54 17
            $dateReceived->setTime(0, 0, 0);
55 17
            $dateCompare->setTime(0, 0, 0);
56
        }
57
58 32
        if ($this->switchOperator($operator, $dateReceived, $dateCompare)) {
59 15
            $this->date = $dateReceived->format($format);
60 15
            $this->dateTimeObject = $dateReceived;
61 15
            return false;
62
        }
63
64 16
        return true;
65
    }
66
67
    /**
68
     * Return DateTime object.
69
     *
70
     * @return DateTime
71
     */
72 15
    public function getDateTimeObject(): DateTime
73
    {
74 15
        return $this->dateTimeObject;
75
    }
76
    
77
    /**
78
     * Perform correct operation from passed operator.
79
     *
80
     * @param string $operator
81
     * @param DateTime $dateReceived
82
     * @param DateTime $dateCompare
83
     *
84
     * @return bool
85
     *
86
     * @throws UnexpectedValueException if unknown operator is provided.
87
     */
88 32
    private function switchOperator(string $operator, DateTime &$dateReceived, DateTime &$dateCompare): bool
89
    {
90 32
        $received = (int) $dateReceived->format('YmdHis');
91 32
        $compare = (int) $dateCompare->format('YmdHis');
92
        
93
        switch ($operator) {
94 32
            case '>': //greater than
95 6
                return $received > $compare;
96 26
            case '<': //less than
97 6
                return $received < $compare;
98 20
            case '>=': //greater than or equal
99 6
                return $received >= $compare;
100 14
            case '<=': //less than or equal
101 6
                return $received <= $compare;
102 8
            case '=': //equal
103 7
                return $received === $compare;
104
            default:
105 1
                throw new UnexpectedValueException("Unknown comparson operator ({$operator}). Permitted >, <, >=, <=, =");
106
        }
107
    }
108
}
109