vendor/shopware/core/Framework/Api/EventListener/JsonRequestTransformerListener.php line 24

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Api\EventListener;
  3. use Shopware\Core\Framework\Log\Package;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpKernel\Event\RequestEvent;
  6. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. /**
  9.  * @deprecated tag:v6.5.0 - reason:becomes-internal - EventSubscribers will become internal in v6.5.0
  10.  */
  11. #[Package('core')]
  12. class JsonRequestTransformerListener implements EventSubscriberInterface
  13. {
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             KernelEvents::REQUEST => ['onRequest'128],
  18.         ];
  19.     }
  20.     public function onRequest(RequestEvent $event): void
  21.     {
  22.         if ($event->getRequest()->getContent() && mb_stripos($event->getRequest()->headers->get('Content-Type'''), 'application/json') === 0) {
  23.             $data json_decode($event->getRequest()->getContent(), true);
  24.             if (json_last_error() !== \JSON_ERROR_NONE) {
  25.                 throw new BadRequestHttpException('The JSON payload is malformed.');
  26.             }
  27.             $event->getRequest()->request->replace(\is_array($data) ? $data : []);
  28.         }
  29.     }
  30. }