|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Stadly\PasswordPolice\Rule; |
|
6
|
|
|
|
|
7
|
|
|
use Stadly\PasswordPolice\RuleException; |
|
8
|
|
|
use Symfony\Component\Translation\Translator; |
|
9
|
|
|
use Throwable; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Exception thrown when a length rule could not be enforced. |
|
13
|
|
|
*/ |
|
14
|
|
|
final class LengthException extends RuleException |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var int Number of characters. |
|
18
|
|
|
*/ |
|
19
|
|
|
private $count; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @param Length $rule Rule that could not be enforced. |
|
25
|
|
|
* @param int $count Number of characters. |
|
26
|
|
|
* @param Translator $translator For translating messages. |
|
27
|
|
|
* @param Throwable $previous Previous exception, used for exception chaining. |
|
28
|
|
|
*/ |
|
29
|
5 |
|
public function __construct(Length $rule, int $count, Translator $translator, ?Throwable $previous = null) |
|
30
|
|
|
{ |
|
31
|
5 |
|
$this->count = $count; |
|
32
|
|
|
|
|
33
|
5 |
|
if ($rule->getMax() === null) { |
|
34
|
1 |
|
$message = $translator->transChoice( |
|
35
|
|
|
'There must be at least one character.|'. |
|
36
|
1 |
|
'There must be at least %count% characters.', |
|
37
|
1 |
|
$rule->getMin() |
|
38
|
|
|
); |
|
39
|
4 |
|
} elseif ($rule->getMax() === 0) { |
|
40
|
1 |
|
$message = $translator->trans( |
|
41
|
1 |
|
'There must be no characters.' |
|
42
|
|
|
); |
|
43
|
3 |
|
} elseif ($rule->getMin() === 0) { |
|
44
|
1 |
|
$message = $translator->transChoice( |
|
45
|
|
|
'There must be at most one character.|'. |
|
46
|
1 |
|
'There must be at most %count% characters.', |
|
47
|
1 |
|
$rule->getMax() |
|
48
|
|
|
); |
|
49
|
2 |
|
} elseif ($rule->getMin() === $rule->getMax()) { |
|
50
|
1 |
|
$message = $translator->transChoice( |
|
51
|
|
|
'There must be exactly one character.|'. |
|
52
|
1 |
|
'There must be exactly %count% characters.', |
|
53
|
1 |
|
$rule->getMin() |
|
54
|
|
|
); |
|
55
|
|
|
} else { |
|
56
|
1 |
|
$message = $translator->trans( |
|
57
|
1 |
|
'There must be between %min% and %max% characters.', |
|
58
|
1 |
|
['%min%' => $rule->getMin(), '%max%' => $rule->getMax()] |
|
59
|
|
|
); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
5 |
|
parent::__construct($rule, $message, $previous); |
|
63
|
5 |
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
public function getCount(): int |
|
66
|
|
|
{ |
|
67
|
1 |
|
return $this->count; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|