Passed
Push — master ( 1e3d3d...d1e56b )
by Smoren
02:30
created

ValidationError::fromValidationErrors()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 3
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Exceptions;
6
7
class ValidationError extends \DomainException
8
{
9
    /**
10
     * @var string
11
     */
12
    protected string $name;
13
    /**
14
     * @var mixed
15
     */
16
    protected $value;
17
    /**
18
     * @var array<array{string, array<string, mixed>}>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array{string, array<string, mixed>}> at position 4 could not be parsed: Expected ':' at position 4, but found 'string'.
Loading history...
19
     */
20
    protected array $violatedRestrictions;
21
22
    /**
23
     * @param string $name
24
     * @param mixed $value
25
     * @param array<CheckError> $checkErrors
26
     *
27
     * @return self
28
     */
29 159
    public static function fromCheckErrors(string $name, $value, array $checkErrors): self
30
    {
31
        /** @var array<array{string, array<string, mixed>}> $violations */
32 159
        $violations = [];
33 159
        foreach ($checkErrors as $checkError) {
34 159
            if ($checkError instanceof CompositeCheckError) {
35 6
                foreach ($checkError->getNestedErrors() as $validationError) {
36 6
                    $violations = [...$violations, ...$validationError->getViolatedRestrictions()];
37
                }
38
            } else {
39 159
                $violations[] = [$checkError->getName(), $checkError->getParams()];
40
            }
41
        }
42
43 159
        return new self($name, $value, $violations);
44
    }
45
46
    /**
47
     * @param mixed $value
48
     * @param array<array{string, array<string, mixed>}> $violatedRestrictions
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array{string, array<string, mixed>}> at position 4 could not be parsed: Expected ':' at position 4, but found 'string'.
Loading history...
49
     */
50 168
    public function __construct(string $name, $value, array $violatedRestrictions)
51
    {
52 168
        parent::__construct('Validation error');
53 168
        $this->name = $name;
54 168
        $this->value = $value;
55 168
        $this->violatedRestrictions = $violatedRestrictions;
56
    }
57
58
    /**
59
     * @return string
60
     */
61 15
    public function getName(): string
62
    {
63 15
        return $this->name;
64
    }
65
66
    /**
67
     * @return mixed
68
     */
69 162
    public function getValue()
70
    {
71 162
        return $this->value;
72
    }
73
74
    /**
75
     * @return array<array{string, array<string, mixed>}>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<array{string, array<string, mixed>}> at position 4 could not be parsed: Expected ':' at position 4, but found 'string'.
Loading history...
76
     */
77 165
    public function getViolatedRestrictions(): array
78
    {
79 165
        return $this->violatedRestrictions;
80
    }
81
}
82