|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stadly\PasswordPolice\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Symfony\Component\Translation\Translator; |
|
8
|
|
|
|
|
9
|
|
|
final class Digit extends CharacterClass |
|
10
|
|
|
{ |
|
11
|
7 |
|
public function __construct(int $min, ?int $max = null) |
|
12
|
|
|
{ |
|
13
|
7 |
|
parent::__construct('0123456789', $min, $max); |
|
14
|
4 |
|
} |
|
15
|
|
|
|
|
16
|
5 |
|
public function getMessage(Translator $translator): string |
|
17
|
|
|
{ |
|
18
|
5 |
|
if ($this->getMax() === null) { |
|
19
|
1 |
|
return $translator->transChoice( |
|
20
|
|
|
'There must be at least one digit.|'. |
|
21
|
1 |
|
'There must be at least %count% digits.', |
|
22
|
1 |
|
$this->getMin() |
|
23
|
|
|
); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
4 |
|
if ($this->getMax() === 0) { |
|
27
|
1 |
|
return $translator->trans( |
|
28
|
1 |
|
'There must be no digits.' |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
3 |
|
if ($this->getMin() === 0) { |
|
33
|
1 |
|
return $translator->transChoice( |
|
34
|
|
|
'There must be at most one digit.|'. |
|
35
|
1 |
|
'There must be at most %count% digits.', |
|
36
|
1 |
|
$this->getMax() |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
2 |
|
if ($this->getMin() === $this->getMax()) { |
|
41
|
1 |
|
return $translator->transChoice( |
|
42
|
|
|
'There must be exactly one digit.|'. |
|
43
|
1 |
|
'There must be exactly %count% digits.', |
|
44
|
1 |
|
$this->getMin() |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
return $translator->trans( |
|
49
|
1 |
|
'There must be between %min% and %max% digits.', |
|
50
|
1 |
|
['%min%' => $this->getMin(), '%max%' => $this->getMax()] |
|
51
|
|
|
); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|