Passed
Push — main ( cf8b8d...c2f1dd )
by Fractal
02:39
created

ValidationException::fromConstraintViolations()   A

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 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FRZB\Component\RequestMapper\Exception;
6
7
use FRZB\Component\RequestMapper\Data\ErrorInterface as Error;
8
use FRZB\Component\RequestMapper\Data\ValidationError;
9
use JetBrains\PhpStorm\Pure;
10
use Symfony\Component\Validator\ConstraintViolationInterface as ConstraintViolation;
11
use Symfony\Component\Validator\ConstraintViolationListInterface as ConstraintViolationList;
12
13
final class ValidationException extends \LogicException
14
{
15
    public const DEFAULT_MESSAGE = 'Validation error';
16
17
    /** @var Error[] */
18
    private array $errors;
19
20 4
    #[Pure]
21
    private function __construct(Error ...$errors)
22
    {
23 4
        parent::__construct(self::DEFAULT_MESSAGE);
24 4
        $this->errors = $errors;
25 4
    }
26
27
    /** @noinspection PhpParamsInspection */
28 2
    public static function fromConstraintViolationList(ConstraintViolationList $violationList): self
29
    {
30 2
        return self::fromConstraintViolations(...$violationList);
31
    }
32
33 2
    public static function fromConstraintViolations(ConstraintViolation ...$violations): self
34
    {
35 2
        return self::fromErrors(...array_map(static fn (ConstraintViolation $violation) => ValidationError::fromConstraint($violation), $violations));
36
    }
37
38 4
    #[Pure]
39
    public static function fromErrors(Error ...$errors): self
40
    {
41 4
        return new self(...$errors);
42
    }
43
44
    /** @return Error[] */
45 3
    public function getErrors(): array
46
    {
47 3
        return $this->errors;
48
    }
49
}
50