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

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\EventListener\ExceptionListener;
  14. use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
  15. use ApiPlatform\Util\ErrorFormatGuesser;
  16. use ApiPlatform\Validator\Exception\ValidationException;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  19. use Symfony\Component\Serializer\SerializerInterface;
  20. /**
  21.  * Handles validation errors.
  22.  * TODO: remove this class.
  23.  *
  24.  * @deprecated
  25.  *
  26.  * @author Kévin Dunglas <dunglas@gmail.com>
  27.  */
  28. final class ValidationExceptionListener
  29. {
  30.     public function __construct(private readonly SerializerInterface $serializer, private readonly array $errorFormats, private readonly array $exceptionToStatus = [], private readonly ?ExceptionListener $exceptionListener null)
  31.     {
  32.     }
  33.     /**
  34.      * Returns a list of violations normalized in the Hydra format.
  35.      */
  36.     public function onKernelException(ExceptionEvent $event): void
  37.     {
  38.         // API Platform 3.2 handles every exception through the exception listener so we just skip this one
  39.         if ($this->exceptionListener) {
  40.             return;
  41.         }
  42.         trigger_deprecation('api-platform''3.2'sprintf('The class "%s" is deprecated and will be removed in 4.x.'__CLASS__));
  43.         $exception $event->getThrowable();
  44.         if (!$exception instanceof ConstraintViolationListAwareExceptionInterface && !$exception instanceof FilterValidationException) {
  45.             return;
  46.         }
  47.         $exceptionClass $exception::class;
  48.         $statusCode Response::HTTP_UNPROCESSABLE_ENTITY;
  49.         foreach ($this->exceptionToStatus as $class => $status) {
  50.             if (is_a($exceptionClass$classtrue)) {
  51.                 $statusCode $status;
  52.                 break;
  53.             }
  54.         }
  55.         $format ErrorFormatGuesser::guessErrorFormat($event->getRequest(), $this->errorFormats);
  56.         $context = [];
  57.         if ($exception instanceof ValidationException && ($errorTitle $exception->getErrorTitle())) {
  58.             $context['title'] = $errorTitle;
  59.         }
  60.         $event->setResponse(new Response(
  61.             $this->serializer->serialize($exception instanceof ConstraintViolationListAwareExceptionInterface $exception->getConstraintViolationList() : $exception$format['key'], $context),
  62.             $statusCode,
  63.             [
  64.                 'Content-Type' => sprintf('%s; charset=utf-8'$format['value'][0]),
  65.                 'X-Content-Type-Options' => 'nosniff',
  66.                 'X-Frame-Options' => 'deny',
  67.             ]
  68.         ));
  69.     }
  70. }