for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
declare(strict_types=1);
namespace FRZB\Component\RequestMapper\Exception;
use Fp\Collections\ArrayList;
use FRZB\Component\RequestMapper\Data\ErrorInterface as Error;
use FRZB\Component\RequestMapper\Data\ValidationError;
use JetBrains\PhpStorm\Pure;
use Symfony\Component\Validator\ConstraintViolationInterface as ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationListInterface as ConstraintViolationList;
final class ValidationException extends \LogicException
{
public const DEFAULT_MESSAGE = 'Validation error';
#[Pure]
private function __construct(
private readonly ArrayList $errors,
) {
parent::__construct(self::DEFAULT_MESSAGE);
}
/** @noinspection PhpParamsInspection */
public static function fromConstraintViolationList(ConstraintViolationList $violationList): self
return self::fromConstraintViolations(...$violationList);
public static function fromConstraintViolations(ConstraintViolation ...$violations): self
return new self(ArrayList::collect($violations)->map(ValidationError::fromConstraint(...)));
public static function fromErrors(Error ...$errors): self
return new self(ArrayList::collect($errors));
public function getErrors(): ArrayList
return $this->errors->toArrayList();
public function getFormattedErrors(): array
return $this->errors
->map(static fn (Error $error) => [$error->getField() => $error->getMessage()])
->reduce(static fn (array $prev, array $next) => [...$prev, ...$next])
->getOrElse([])
;