Completed
Push — master ( e6110c...e66582 )
by Alejandro
19s queued 15s
created

ExcludingValidatorChain   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 40
ccs 15
cts 15
cp 1
rs 10
c 1
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessages() 0 3 1
A isValid() 0 12 2
A __construct() 0 3 1
A __invoke() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Common\Validation;
6
7
use Laminas\Validator;
8
9
use function array_replace_recursive;
10
use function Functional\some;
11
12
class ExcludingValidatorChain implements Validator\ValidatorInterface
13
{
14
    private array $validators;
15
    private array $messages = [];
16
17 7
    public function __construct(Validator\ValidatorInterface ...$validators)
18
    {
19 7
        $this->validators = $validators;
20
    }
21
22
    /**
23
     * @param mixed $value
24
     */
25 4
    public function isValid($value): bool
26
    {
27 4
        return some(
28 4
            $this->validators,
29
            function (Validator\ValidatorInterface $validator) use ($value): bool {
30 4
                if ($validator->isValid($value)) {
31 3
                    return true;
32
                }
33
34 3
                $messages = $validator->getMessages();
35 3
                $this->messages = array_replace_recursive($this->messages, $messages);
36 3
                return false;
37 4
            },
38
        );
39
    }
40
41
    /**
42
     * @param mixed $value
43
     */
44 3
    public function __invoke($value): bool
45
    {
46 3
        return $this->isValid($value);
47
    }
48
49 1
    public function getMessages(): array
50
    {
51 1
        return $this->messages;
52
    }
53
}
54