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