Passed
Push — master ( 85531b...08a7b5 )
by Magnar Ovedal
04:34 queued 02:05
created

DigitRule::getMessage()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 37
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 22
nc 5
nop 2
dl 0
loc 37
ccs 20
cts 20
cp 1
crap 5
rs 9.2568
c 0
b 0
f 0
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