Passed
Push — master ( bc036e...f8a9bb )
by Smoren
02:19
created

ValidationError::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
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 $summary;
21
22
    /**
23
     * @param string $name
24
     * @param mixed $value
25
     * @param array<CheckError> $checkErrors
26
     *
27
     * @return self
28
     */
29 39
    public static function fromCheckErrors(string $name, $value, array $checkErrors): self
30
    {
31 39
        return new self($name, $value, array_map(
32 39
            fn (CheckError $error) => [$error->getName(), $error->getParams()],
33 39
            $checkErrors
34 39
        ));
35
    }
36
37
    /**
38
     * @param string $name
39
     * @param mixed $value
40
     * @param array<ValidationError> $validationErrors
41
     *
42
     * @return self
43
     */
44 3
    public static function fromValidationErrors(string $name, $value, array $validationErrors): self
45
    {
46 3
        $summary = [];
47
48 3
        foreach ($validationErrors as $error) {
49 3
            foreach ($error->getSummary() as $item) {
50 3
                $summary[] = $item;
51
            }
52
        }
53
54 3
        return new self($name, $value, $summary);
55
    }
56
57
    /**
58
     * @param mixed $value
59
     * @param array<array{string, array<string, mixed>}> $summary
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...
60
     */
61 42
    public function __construct(string $name, $value, array $summary)
62
    {
63 42
        parent::__construct('Validation error');
64 42
        $this->name = $name;
65 42
        $this->value = $value;
66 42
        $this->summary = $summary;
67
    }
68
69
    /**
70
     * @return string
71
     */
72
    public function getName(): string
73
    {
74
        return $this->name;
75
    }
76
77
    /**
78
     * @return mixed
79
     */
80 39
    public function getValue()
81
    {
82 39
        return $this->value;
83
    }
84
85
    /**
86
     * @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...
87
     */
88 39
    public function getSummary(): array
89
    {
90 39
        return $this->summary;
91
    }
92
}
93