PasswordValidator::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
ccs 0
cts 4
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
1
<?php
2
declare(strict_types=1);
3
4
namespace Porthou\Password;
5
6
class PasswordValidator
7
{
8
    /** @var Validator[] $validators */
9
    private $validators = [];
10
11
    public function __construct(iterable $validators = [])
12
    {
13
        foreach ($validators as $validator) {
14
            $this->addValidator($validator);
15
        }
16
    }
17
18
    /**
19
     * Adds a validator to the current PasswordValidator instance
20
     *
21
     * @param Validator $validator
22
     */
23
    public function addValidator(Validator $validator): void
24
    {
25
        $this->validators[] = $validator;
26
    }
27
28
    /**
29
     * Gets an array of the currently applied Validators
30
     *
31
     * @return Validator[]
32
     */
33
    public function getValidators(): array
34
    {
35
        return $this->validators;
36
    }
37
38
    /**
39
     * Validates the given password against the current rule set.
40
     *
41
     * @param string $password
42
     * @return bool True if the password is valid
43
     * @throws PasswordException If the password fails a validation attempt.
44
     */
45
    public function validate(string $password): bool
46
    {
47
        foreach ($this->validators as $validator) {
48
            $validator->validate($password);
49
        }
50
51
        return true;
52
    }
53
}
54