Passed
Push — master ( 2513f1...fadf79 )
by Sebastian
03:14
created

DateMin::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 3
nop 3
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
crap 4.0119
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 DateTime Valid date.
23
     */
24
    private $date;
25
    
26
    /**
27
     * Validate.
28
     *
29
     * @return bool
30
     */
31 6
    public function validate($received, string $format, string $min): bool
32
    {
33 6
        $dateReceived = DateTime::createFromFormat($format, $received);
34 6
        $dateMin = DateTime::createFromFormat($format, $min);
35
        
36 6
        if (!($dateMin && $dateReceived)) {
37
            return true;
38
        }
39
        
40 6
        $dateMin->setTime(0, 0, 0);
41 6
        $dateReceived->setTime(0, 0, 0);
42
            
43 6
        if ($dateMin->format('Ymd') <= $dateReceived->format('Ymd')) {
44 3
            $this->date = $dateReceived;
45 3
            return false;
46
        }
47
        
48 3
        return true;
49
    }
50
    
51
    /**
52
     * Sanitize.
53
     *
54
     * @param mixed $value
55
     */
56
    public function sanitize(&$value)
57
    {
58
        $value = $this->date;
59
    }
60
}
61