Passed
Push — master ( 5c696b...71a920 )
by Magnar Ovedal
02:25
created

Date::getMin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice\Constraint;
6
7
use DateTimeInterface;
8
use InvalidArgumentException;
9
10
final class Date
11
{
12
    /**
13
     * @var DateTimeInterface|null Minimum date.
14
     */
15
    private $min;
16
17
    /**
18
     * @var DateTimeInterface|null Maximum date.
19
     */
20
    private $max;
21
22
    /**
23
     * @var int Constraint weight.
24
     */
25
    private $weight;
26
27
    /**
28
     * @param DateTimeInterface|null $min Minimum date.
29
     * @param DateTimeInterface|null $max Maximum date.
30
     * @param int $weight Constraint weight.
31
     */
32 7
    public function __construct(?DateTimeInterface $min, ?DateTimeInterface $max = null, int $weight = 1)
33
    {
34 7
        if ($min !== null && $max !== null && $max < $min) {
35 1
            throw new InvalidArgumentException('Max cannot be smaller than min.');
36
        }
37
38 6
        $this->min = $min;
39 6
        $this->max = $max;
40 6
        $this->weight = $weight;
41 6
    }
42
43
    /**
44
     * @return DateTimeInterface|null Minimum date.
45
     */
46 1
    public function getMin(): ?DateTimeInterface
47
    {
48 1
        return $this->min;
49
    }
50
51
    /**
52
     * @return DateTimeInterface|null Maximum date.
53
     */
54 1
    public function getMax(): ?DateTimeInterface
55
    {
56 1
        return $this->max;
57
    }
58
59
    /**
60
     * @return int Constraint weight.
61
     */
62 1
    public function getWeight(): int
63
    {
64 1
        return $this->weight;
65
    }
66
67
    /**
68
     * Check whether the date is in compliance with the constraint.
69
     *
70
     * @param DateTimeInterface $date Date to check.
71
     * @return bool Whether the date is in compliance with the constraint.
72
     */
73 4
    public function test(DateTimeInterface $date): bool
74
    {
75 4
        if (null !== $this->min && $date < $this->min) {
76 1
            return false;
77
        }
78
79 3
        if (null !== $this->max && $this->max < $date) {
80 1
            return false;
81
        }
82
83 2
        return true;
84
    }
85
}
86