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

DateMax   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
dl 0
loc 40
ccs 10
cts 13
cp 0.7692
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 0 18 4
A sanitize() 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
16
/**
17
 * Check if given date is equal or less than a date.
18
 */
19
class DateMax
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 $max): bool
32
    {
33 6
        $dateReceived = DateTime::createFromFormat($format, $received);
34 6
        $dateMax = DateTime::createFromFormat($format, $max);
35
        
36 6
        if (!($dateMax && $dateReceived)) {
37
            return true;
38
        }
39
        
40 6
        $dateMax->setTime(0, 0, 0);
41 6
        $dateReceived->setTime(0, 0, 0);
42
            
43 6
        if ($dateMax->format('Ymd') >= $dateReceived->format('Ymd')) {
44 4
            $this->date = $dateReceived;
45 4
            return false;
46
        }
47
        
48 2
        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