|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stadly\PasswordPolice\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Stadly\PasswordPolice\Constraint\CountConstraint; |
|
8
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
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
|
14 |
|
public function __construct(int $min = 1, ?int $max = null, int $weight = 1) |
|
18
|
|
|
{ |
|
19
|
14 |
|
parent::__construct('0123456789', $min, $max, $weight); |
|
20
|
12 |
|
} |
|
21
|
|
|
|
|
22
|
5 |
|
protected function getMessage(CountConstraint $constraint, int $count, TranslatorInterface $translator): string |
|
23
|
|
|
{ |
|
24
|
5 |
|
if ($constraint->getMax() === null) { |
|
25
|
1 |
|
return $translator->trans( |
|
26
|
|
|
'The password must contain at least one digit.|' . |
|
27
|
1 |
|
'The password must contain at least %count% digits.', |
|
28
|
1 |
|
['%count%' => $constraint->getMin()] |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
4 |
|
if ($constraint->getMax() === 0) { |
|
33
|
1 |
|
return $translator->trans( |
|
34
|
1 |
|
'The password cannot contain digits.' |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
3 |
|
if ($constraint->getMin() === 0) { |
|
39
|
1 |
|
return $translator->trans( |
|
40
|
|
|
'The password must contain at most one digit.|' . |
|
41
|
1 |
|
'The password must contain at most %count% digits.', |
|
42
|
1 |
|
['%count%' => $constraint->getMax()] |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
if ($constraint->getMin() === $constraint->getMax()) { |
|
47
|
1 |
|
return $translator->trans( |
|
48
|
|
|
'The password must contain exactly one digit.|' . |
|
49
|
1 |
|
'The password must contain exactly %count% digits.', |
|
50
|
1 |
|
['%count%' => $constraint->getMin()] |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
1 |
|
return $translator->trans( |
|
55
|
1 |
|
'The password must contain between %min% and %max% digits.', |
|
56
|
1 |
|
['%min%' => $constraint->getMin(), '%max%' => $constraint->getMax()] |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|