Passed
Push — master ( f56605...d778ec )
by Smoren
02:21
created

CompositeCheckError::equalTo()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 3
nc 3
nop 1
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 12
rs 10
c 1
b 1
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Exceptions;
6
7
final class CompositeCheckError extends CheckError
8
{
9
    /**
10
     * @var array<ValidationError>
11
     */
12
    protected array $nestedErrors;
13
14
    /**
15
     * @param string $name
16
     * @param mixed $value
17
     * @param array<ValidationError> $errors
18
     */
19 6
    public function __construct(string $name, $value, array $errors)
20
    {
21 6
        parent::__construct($name, $value, []);
22 6
        $this->nestedErrors = $errors;
23
    }
24
25
    /**
26
     * @return array<ValidationError>
27
     */
28 6
    public function getNestedErrors(): array
29
    {
30 6
        return $this->nestedErrors;
31
    }
32
33
    /**
34
     * @param CheckError $error
35
     *
36
     * @return bool
37
     */
38
    public function equalTo(CheckError $error): bool
39
    {
40
        return $error instanceof CompositeCheckError
41
            && parent::equalTo($error)
42
            && $this->hasSameNestedErrorsWith($error);
43
    }
44
45
    /**
46
     * @param CompositeCheckError $error
47
     *
48
     * @return bool
49
     */
50
    protected function hasSameNestedErrorsWith(CompositeCheckError $error): bool
51
    {
52
        return (
53
            array_map(fn ($e) => $e->getViolatedRestrictions(), $this->nestedErrors) ===
54
            array_map(fn ($e) => $e->getViolatedRestrictions(), $error->nestedErrors)
55
        );
56
    }
57
}
58