src/EventSubscriber/Location/LocationPostSerializerSubscriber.php line 29

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: SUSAN MEDINA
  5.  * Date: 26/04/2019
  6.  * Time: 11:33 AM
  7.  */
  8. namespace App\EventSubscriber\Location;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\ViewEvent;
  11. use ApiPlatform\Core\EventListener\EventPriorities;
  12. use Symfony\Component\HttpKernel\KernelEvents;
  13. use App\Services\LocationService;
  14. class LocationPostSerializerSubscriber implements EventSubscriberInterface
  15. {
  16.     private $locationService;
  17.     public function __construct(LocationService $locationService)
  18.     {
  19.         $this->locationService $locationService;
  20.     }
  21.     /**
  22.      * @param ViewEvent $event
  23.      */
  24.     public function onKernelView(ViewEvent $event)
  25.     {
  26.         $request $event->getRequest();
  27.         $locationResult $event->getControllerResult();
  28.         $routes = array(
  29.             'api_locations_get_collection',
  30.             'api_locations_post_collection',
  31.             'api_locations_get_item',
  32.             'api_locations_put_item',
  33.             'api_vendors_locations_get_subresource'
  34.         );
  35.         $route $request->attributes->get('_route');
  36.         if (!in_array($route$routes)) {
  37.             return;
  38.         }
  39.         
  40.         $locationResult json_decode($locationResulttrue);
  41.         if ('api_locations_get_collection' === $route ||
  42.             'api_vendors_locations_get_subresource' === $route) {
  43.             foreach ($locationResult as &$location) {
  44.                 $location $this->locationService->formatNormalize($location$route);
  45.             }
  46.         } else {
  47.             $locationResult $this->locationService->formatNormalize($locationResult$route);
  48.         }
  49.         $locationResult json_encode($locationResult);
  50.         $event->setControllerResult($locationResult);
  51.     }
  52.     public static function getSubscribedEvents()
  53.     {
  54.         return [
  55.             KernelEvents::VIEW => ['onKernelView'EventPriorities::POST_SERIALIZE]
  56.         ];
  57.     }
  58. }