vendor/api-platform/core/src/Symfony/Validator/EventListener/ValidationExceptionListener.php line 44

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Symfony\Validator\EventListener;
  12. use ApiPlatform\Exception\FilterValidationException;
  13. use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
  14. use ApiPlatform\Util\ErrorFormatGuesser;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  17. use Symfony\Component\Serializer\SerializerInterface;
  18. /**
  19.  * Handles validation errors.
  20.  *
  21.  * @author Kévin Dunglas <dunglas@gmail.com>
  22.  */
  23. final class ValidationExceptionListener
  24. {
  25.     private $serializer;
  26.     private $errorFormats;
  27.     private $exceptionToStatus;
  28.     public function __construct(SerializerInterface $serializer, array $errorFormats, array $exceptionToStatus = [])
  29.     {
  30.         $this->serializer $serializer;
  31.         $this->errorFormats $errorFormats;
  32.         $this->exceptionToStatus $exceptionToStatus;
  33.     }
  34.     /**
  35.      * Returns a list of violations normalized in the Hydra format.
  36.      */
  37.     public function onKernelException(ExceptionEvent $event): void
  38.     {
  39.         $exception method_exists($event'getThrowable') ? $event->getThrowable() : $event->getException(); // @phpstan-ignore-line
  40.         if (!$exception instanceof ConstraintViolationListAwareExceptionInterface && !$exception instanceof FilterValidationException) {
  41.             return;
  42.         }
  43.         $exceptionClass \get_class($exception);
  44.         $statusCode Response::HTTP_UNPROCESSABLE_ENTITY;
  45.         foreach ($this->exceptionToStatus as $class => $status) {
  46.             if (is_a($exceptionClass$classtrue)) {
  47.                 $statusCode $status;
  48.                 break;
  49.             }
  50.         }
  51.         $format ErrorFormatGuesser::guessErrorFormat($event->getRequest(), $this->errorFormats);
  52.         $event->setResponse(new Response(
  53.             $this->serializer->serialize($exception instanceof ConstraintViolationListAwareExceptionInterface $exception->getConstraintViolationList() : $exception$format['key']),
  54.             $statusCode,
  55.             [
  56.                 'Content-Type' => sprintf('%s; charset=utf-8'$format['value'][0]),
  57.                 'X-Content-Type-Options' => 'nosniff',
  58.                 'X-Frame-Options' => 'deny',
  59.             ]
  60.         ));
  61.     }
  62. }
  63. class_alias(ValidationExceptionListener::class, \ApiPlatform\Core\Bridge\Symfony\Validator\EventListener\ValidationExceptionListener::class);