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

DateMax   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 13
cts 13
cp 1
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 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 $max): bool
37
    {
38 7
        $dateReceived = DateTime::createFromFormat($format, $received);
39 7
        $dateMax = DateTime::createFromFormat($format, $max);
40
        
41 7
        if (!($dateMax && $dateReceived)) {
42 1
            return true;
43
        }
44
        
45 6
        $dateMax->setTime(0, 0, 0);
46 6
        $dateReceived->setTime(0, 0, 0);
47
        
48 6
        if ($dateMax->format('Ymd') >= $dateReceived->format('Ymd')) {
49 4
            $this->date = $dateReceived;
50 4
            return false;
51
        }
52
        
53 2
        return true;
54
    }
55
    
56
    /**
57
     * Sanitize.
58
     *
59
     * @param mixed $value
60
     */
61 4
    public function sanitize(&$value):void
62
    {
63 4
        $value = $this->date;
64 4
    }
65
}
66