ValidationError::getValue()   A
last analyzed

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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Validator\Exceptions;
6
7
final 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 164
    public static function fromCheckErrors(string $name, $value, array $checkErrors): self
30
    {
31
        /** @var array<array{string, array<string, mixed>}> $violations */
32 164
        $violations = [];
33 164
        foreach ($checkErrors as $checkError) {
34 164
            if ($checkError instanceof CompositeCheckError) {
35 9
                foreach ($checkError->getNestedErrors() as $validationError) {
36 9
                    $violations = [...$violations, ...$validationError->getViolatedRestrictions()];
37
                }
38
            } else {
39 164
                $violations[] = [$checkError->getName(), $checkError->getParams()];
40
            }
41
        }
42
43 164
        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 174
    public function __construct(string $name, $value, array $violatedRestrictions)
51
    {
52 174
        parent::__construct('Validation error');
53 174
        $this->name = $name;
54 174
        $this->value = $value;
55 174
        $this->violatedRestrictions = $violatedRestrictions;
56
    }
57
58
    /**
59
     * @return string
60
     */
61 16
    public function getName(): string
62
    {
63 16
        return $this->name;
64
    }
65
66
    /**
67
     * @return mixed
68
     */
69 164
    public function getValue()
70
    {
71 164
        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 170
    public function getViolatedRestrictions(): array
78
    {
79 170
        return $this->violatedRestrictions;
80
    }
81
}
82