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

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