Passed
Push — master ( 9129d6...9f4504 )
by Smoren
02:40
created

ValidationError::fromCheckErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Smoren\Validator\Exceptions;
4
5
class ValidationError extends \DomainException
6
{
7
    /**
8
     * @var mixed
9
     */
10
    protected $value;
11
    /**
12
     * @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...
13
     */
14
    protected array $summary;
15
16
    /**
17
     * @param mixed $value
18
     * @param array<CheckError> $checkErrors
19
     * @return self
20
     */
21 14
    public static function fromCheckErrors($value, array $checkErrors): self
22
    {
23 14
        return new self($value, array_map(
24 14
            fn (CheckError $error) => [$error->getName(), $error->getParams()],
25 14
            $checkErrors
26 14
        ));
27
    }
28
29
    /**
30
     * @param mixed $value
31
     * @param array<ValidationError> $validationErrors
32
     * @return self
33
     */
34
    public static function fromValidationErrors($value, array $validationErrors): self
35
    {
36
        $summary = [];
37
38
        foreach ($validationErrors as $error) {
39
            foreach ($error->getSummary() as $item) {
40
                $summary[] = $item;
41
            }
42
        }
43
44
        return new self($value, $summary);
45
    }
46
47
    /**
48
     * @param mixed $value
49
     * @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...
50
     */
51 15
    public function __construct($value, array $summary)
52
    {
53 15
        parent::__construct('Validation error');
54 15
        $this->value = $value;
55 15
        $this->summary = $summary;
56
    }
57
58
    /**
59
     * @return mixed
60
     */
61 15
    public function getValue()
62
    {
63 15
        return $this->value;
64
    }
65
66
    /**
67
     * @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...
68
     */
69 15
    public function getSummary(): array
70
    {
71 15
        return $this->summary;
72
    }
73
}
74