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

ExcludingValidatorChain::getMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
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