/** * Handles string responses. * * @param GetResponseForControllerResultEvent $event The event to handle */ public function onKernelView(GetResponseForControllerResultEvent $event) { $response = $event->getControllerResult(); $request = $event->getRequest(); if ($response instanceof RestResponse) { $format = $request->getRequestFormat(null); if (null === $format) { $accepts = $request->getAcceptableContentTypes(); $format = $request->getFormat($accepts[0]); } $newResponse = new Response($this->rest->formatOutput($response->getData(), $format), 200, array("Content-Type" => $request->getMimeType($format))); $event->setResponse($newResponse); } }
/** * {@inheritDoc} */ public function save($element) { $class = $this->apiResource->getClass(); if (!$element instanceof $class) { throw new \InvalidArgumentException(sprintf('Resource %s só aceita objetos da classe %s', $this->apiResource->getCurrentPath(), $class)); } $data = $this->rest->formatOutput($element, $this->apiResource->getFormat()); if ($element instanceof ElementInterface) { // mais rápido e fácil $id = $element->getId(); } else { // mapeia as propriedades e retorna uma classe com os metadados $metadata = $this->elementMetadataFactory->getMetadata($class); $id = $metadata->getIdentifier($element); } $response = $this->execute($id ? 'PUT' : 'POST', $this->apiResource->getCurrentPath($id), $data); if (!$id) { return $this->rest->createObject($response, $this->apiResource->getClass(), $this->apiResource->getFormat()); } return $element; }
/** * @dataProvider filterProvider * * @param FilterInterface $filter */ public function testFilter($filter) { $r = $this->rest->filter($this->data, $filter); $flat = $this->rest->formatOutput($r, 'json'); $this->assertContains('joao', $flat); $this->assertContains('johannes', $flat); $this->assertNotContains('maria', $flat); $this->assertNotContains('carlos', $flat); if ($filter instanceof TotalizableInterface) { $this->assertContains('2', $flat); } }