Issues (31)

Exception/ValidationException.php (1 issue)

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\Deprecated;
10
use JetBrains\PhpStorm\Immutable;
11
use JetBrains\PhpStorm\Pure;
12
use Symfony\Component\Validator\ConstraintViolationInterface as ConstraintViolation;
13
use Symfony\Component\Validator\ConstraintViolationListInterface as ConstraintViolationList;
14
15
#[Immutable]
16
final class ValidationException extends \LogicException
17
{
18
    public const DEFAULT_MESSAGE = 'Validation Exception';
19
20 6
    /** @var Error[] */
21
    public readonly array $errors;
22
23 6
    #[Pure]
24 6
    private function __construct(Error ...$errors)
25
    {
26
        parent::__construct(self::DEFAULT_MESSAGE);
27
        $this->errors = $errors;
0 ignored issues
show
The property errors is declared read-only in FRZB\Component\RequestMa...ion\ValidationException.
Loading history...
28 2
    }
29
30 2
    /** @noinspection PhpParamsInspection */
31
    public static function fromConstraintViolationList(ConstraintViolationList $violationList): self
32
    {
33 2
        return self::fromConstraintViolations(...$violationList);
34
    }
35 2
36
    public static function fromConstraintViolations(ConstraintViolation ...$violations): self
37
    {
38 6
        return self::fromErrors(...array_map(static fn (ConstraintViolation $violation) => ValidationError::fromConstraint($violation), $violations));
39
    }
40
41 6
    #[Pure]
42
    public static function fromErrors(Error ...$errors): self
43
    {
44
        return new self(...$errors);
45 3
    }
46
47 3
    /** @return Error[] */
48
    #[Deprecated('PHP has readonly properties now', 'public readonly property $errors', '8.1')]
49
    public function getErrors(): array
50
    {
51
        return $this->errors;
52
    }
53
}
54