/** * @param \JMS\SerializerBundle\Serializer\SerializerInterface $serializer * @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator * @param \Staffim\DTOBundle\Filterer\Filterer $filterer */ public function __construct(SerializerInterface $serializer, ValidatorInterface $validator, Filterer $filterer = null) { $this->validator = $validator; $this->serializer = $serializer; $this->filterer = $filterer; $this->serializationContext = new SerializationContext(); $this->serializationContext->setSerializeNull(true); }
/** * @param \JMS\SerializerBundle\Serializer\SerializerInterface $serializer * @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator * @param \Staffim\DTOBundle\Filterer\Filterer $filterer */ public function __construct(SerializerInterface $serializer, ValidatorInterface $validator, Filterer $filterer = null) { $this->validator = $validator; $this->serializer = $serializer; $this->filterer = $filterer; $this->serializationContext = new SerializationContext(); $this->serializationContext->setSerializeNull(true); $this->serializationContext->addExclusionStrategy(new HiddenFieldsExclusionStrategy()); }
/** * Renders the template and initializes a new response object with the * rendered template content. * * @param GetResponseForControllerResultEvent $event A GetResponseForControllerResultEvent instance */ public function onKernelView(GetResponseForControllerResultEvent $event) { if (!($render = $event->getRequest()->attributes->get('_render'))) { return; } $route = $event->getRequest()->attributes->get('_route'); $routeParams = $event->getRequest()->attributes->get('_route_params', []); $pagination = $event->getRequest()->attributes->get('_'); $data = $event->getControllerResult(); $response = new Response(); $response->setStatusCode($render->getCode()); // "The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line // after the header fields" — http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html. if (204 == $render->getCode()) { $content = null; } elseif ($data !== null) { if ($data instanceof ModelInterface) { $presentationData = $this->mapper->map($data); } elseif ($data instanceof ModelIteratorInterface) { $presentationData = new CollectionRepresentation($this->mapper->mapCollection($data)); if ($route && $data instanceof ModelCollection && ($pagination = $data->getPagination())) { $presentationData = new PaginatedRepresentation($presentationData, $route, $routeParams, $pagination->offset, $pagination->limit, $data->getCount()); } } else { $presentationData = $data; } $context = new SerializationContext(); $context->setSerializeNull(true); $context->setGroups($render->getGroups() ?: ['Default']); $content = $this->serializer->serialize($presentationData, $render->getFormat(), $context); $response->headers->set('Content-Type', 'application/json; charset=UTF-8'); $response->headers->set('Content-Length', strlen($content)); $response->setContent($content); } $event->setResponse($response); }