|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stadly\PasswordPolice\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use StableSort\StableSort; |
|
9
|
|
|
use Stadly\PasswordPolice\Constraint\Count; |
|
10
|
|
|
use Stadly\PasswordPolice\Password; |
|
11
|
|
|
use Stadly\PasswordPolice\Policy; |
|
12
|
|
|
|
|
13
|
|
|
final class Length implements RuleInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Count[] Rule constraints. |
|
17
|
|
|
*/ |
|
18
|
|
|
private $constraints; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param int $min Minimum password length. |
|
22
|
|
|
* @param int|null $max Maximum password length. |
|
23
|
|
|
* @param int $weight Constraint weight. |
|
24
|
|
|
*/ |
|
25
|
7 |
|
public function __construct(int $min = 8, ?int $max = null, int $weight = 1) |
|
26
|
|
|
{ |
|
27
|
7 |
|
$this->addConstraint($min, $max, $weight); |
|
28
|
5 |
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param int $min Minimum password length. |
|
32
|
|
|
* @param int|null $max Maximum password length. |
|
33
|
|
|
* @param int $weight Constraint weight. |
|
34
|
|
|
* @return $this |
|
35
|
|
|
*/ |
|
36
|
2 |
|
public function addConstraint(int $min = 1, ?int $max = null, int $weight = 1): self |
|
37
|
|
|
{ |
|
38
|
2 |
|
$this->constraints[] = new Count($min, $max, $weight); |
|
39
|
|
|
|
|
40
|
|
|
StableSort::usort($this->constraints, function (Count $a, Count $b): int { |
|
41
|
2 |
|
return $b->getWeight() <=> $a->getWeight(); |
|
42
|
2 |
|
}); |
|
43
|
|
|
|
|
44
|
2 |
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Check whether a password is in compliance with the rule. |
|
49
|
|
|
* |
|
50
|
|
|
* @param Password|string $password Password to check. |
|
51
|
|
|
* @return bool Whether the password is in compliance with the rule. |
|
52
|
|
|
*/ |
|
53
|
5 |
|
public function test($password): bool |
|
54
|
|
|
{ |
|
55
|
5 |
|
$count = $this->getCount((string)$password); |
|
56
|
5 |
|
$constraint = $this->getViolation($count); |
|
57
|
|
|
|
|
58
|
5 |
|
return $constraint === null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Enforce that a password is in compliance with the rule. |
|
63
|
|
|
* |
|
64
|
|
|
* @param Password|string $password Password that must adhere to the rule. |
|
65
|
|
|
* @throws RuleException If the password does not adhrere to the rule. |
|
66
|
|
|
*/ |
|
67
|
7 |
|
public function enforce($password): void |
|
68
|
|
|
{ |
|
69
|
7 |
|
$count = $this->getCount((string)$password); |
|
70
|
7 |
|
$constraint = $this->getViolation($count); |
|
71
|
|
|
|
|
72
|
7 |
|
if ($constraint !== null) { |
|
73
|
6 |
|
throw new RuleException($this, $this->getMessage($constraint, $count)); |
|
74
|
|
|
} |
|
75
|
1 |
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param int $count Number of characters. |
|
79
|
|
|
* @return Count|null Constraint violated by the count. |
|
80
|
|
|
*/ |
|
81
|
12 |
|
private function getViolation(int $count): ?Count |
|
82
|
|
|
{ |
|
83
|
12 |
|
foreach ($this->constraints as $constraint) { |
|
84
|
12 |
|
if (!$constraint->test($count)) { |
|
85
|
12 |
|
return $constraint; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
4 |
|
return null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $password Password to count characters in. |
|
94
|
|
|
* @return int Number of characters. |
|
95
|
|
|
*/ |
|
96
|
12 |
|
private function getCount(string $password): int |
|
97
|
|
|
{ |
|
98
|
12 |
|
return mb_strlen($password); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param Count $constraint Constraint that is violated. |
|
103
|
|
|
* @param int $count Count that violates the constraint. |
|
104
|
|
|
* @return string Message explaining the violation. |
|
105
|
|
|
*/ |
|
106
|
6 |
|
private function getMessage(Count $constraint, int $count): string |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
6 |
|
$translator = Policy::getTranslator(); |
|
109
|
|
|
|
|
110
|
6 |
|
if ($constraint->getMax() === null) { |
|
111
|
2 |
|
return $translator->trans( |
|
112
|
|
|
'There must be at least one character.|'. |
|
113
|
2 |
|
'There must be at least %count% characters.', |
|
114
|
2 |
|
['%count%' => $constraint->getMin()] |
|
115
|
|
|
); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
4 |
|
if ($constraint->getMax() === 0) { |
|
119
|
1 |
|
return $translator->trans( |
|
120
|
1 |
|
'There must be no characters.' |
|
121
|
|
|
); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
3 |
|
if ($constraint->getMin() === 0) { |
|
125
|
1 |
|
return $translator->trans( |
|
126
|
|
|
'There must be at most one character.|'. |
|
127
|
1 |
|
'There must be at most %count% characters.', |
|
128
|
1 |
|
['%count%' => $constraint->getMax()] |
|
129
|
|
|
); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
2 |
|
if ($constraint->getMin() === $constraint->getMax()) { |
|
133
|
1 |
|
return $translator->trans( |
|
134
|
|
|
'There must be exactly one character.|'. |
|
135
|
1 |
|
'There must be exactly %count% characters.', |
|
136
|
1 |
|
['%count%' => $constraint->getMin()] |
|
137
|
|
|
); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
1 |
|
return $translator->trans( |
|
141
|
1 |
|
'There must be between %min% and %max% characters.', |
|
142
|
1 |
|
['%min%' => $constraint->getMin(), '%max%' => $constraint->getMax()] |
|
143
|
|
|
); |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.