Completed
Push — master ( fb105d...b1139a )
by Sebastian
07:05
created

DateMin::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 3
dl 0
loc 18
ccs 11
cts 11
cp 1
crap 4
rs 9.2
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
16
/**
17
 * Check if given date is equal or grater than a date.
18
 */
19
class DateMin
20
{
21
    /**
22
     * @var array Arguments expected.
23
     */
24
    private $arguments = ['string', 'string'];
0 ignored issues
show
introduced by
The private property $arguments is not used, and could be removed.
Loading history...
25
    
26
    /**
27
     * @var DateTime Valid date.
28
     */
29
    private $date;
30
    
31
    /**
32
     * Validate.
33
     *
34
     * @return bool
35
     */
36 7
    public function validate($received, string $format, string $min): bool
37
    {
38 7
        $dateReceived = DateTime::createFromFormat($format, $received);
39 7
        $dateMin = DateTime::createFromFormat($format, $min);
40
        
41 7
        if (!($dateMin && $dateReceived)) {
42 1
            return true;
43
        }
44
        
45 6
        $dateMin->setTime(0, 0, 0);
46 6
        $dateReceived->setTime(0, 0, 0);
47
        
48 6
        if ($dateMin->format('Ymd') <= $dateReceived->format('Ymd')) {
49 3
            $this->date = $dateReceived;
50 3
            return false;
51
        }
52
        
53 3
        return true;
54
    }
55
    
56
    /**
57
     * Sanitize.
58
     *
59
     * @param mixed $value
60
     */
61 3
    public function sanitize(&$value): void
62
    {
63 3
        $value = $this->date;
64 3
    }
65
}
66