1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Stadly\PasswordPolice\Rule; |
6
|
|
|
|
7
|
|
|
use Stadly\PasswordPolice\Constraint\CountConstraint; |
8
|
|
|
use Stadly\PasswordPolice\Policy; |
9
|
|
|
|
10
|
|
|
final class DigitRule extends CharacterClassRule |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param int $min Minimum number of digits. |
14
|
|
|
* @param int|null $max Maximum number of digits. |
15
|
|
|
* @param int $weight Constraint weight. |
16
|
|
|
*/ |
17
|
7 |
|
public function __construct(int $min = 1, ?int $max = null, int $weight = 1) |
18
|
|
|
{ |
19
|
7 |
|
parent::__construct('0123456789', $min, $max, $weight); |
20
|
5 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* {@inheritDoc} |
24
|
|
|
*/ |
25
|
5 |
|
protected function getMessage(CountConstraint $constraint, int $count): string |
26
|
|
|
{ |
27
|
5 |
|
$translator = Policy::getTranslator(); |
28
|
|
|
|
29
|
5 |
|
if ($constraint->getMax() === null) { |
30
|
1 |
|
return $translator->trans( |
31
|
|
|
'There must be at least one digit.|'. |
32
|
1 |
|
'There must be at least %count% digits.', |
33
|
1 |
|
['%count%' => $constraint->getMin()] |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
if ($constraint->getMax() === 0) { |
38
|
1 |
|
return $translator->trans( |
39
|
1 |
|
'There must be no digits.' |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
3 |
|
if ($constraint->getMin() === 0) { |
44
|
1 |
|
return $translator->trans( |
45
|
|
|
'There must be at most one digit.|'. |
46
|
1 |
|
'There must be at most %count% digits.', |
47
|
1 |
|
['%count%' => $constraint->getMax()] |
48
|
|
|
); |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
if ($constraint->getMin() === $constraint->getMax()) { |
52
|
1 |
|
return $translator->trans( |
53
|
|
|
'There must be exactly one digit.|'. |
54
|
1 |
|
'There must be exactly %count% digits.', |
55
|
1 |
|
['%count%' => $constraint->getMin()] |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
return $translator->trans( |
60
|
1 |
|
'There must be between %min% and %max% digits.', |
61
|
1 |
|
['%min%' => $constraint->getMin(), '%max%' => $constraint->getMax()] |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|