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