예제 #1
0
 /**
  * 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);
     }
 }
예제 #2
0
 /**
  * {@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;
 }
예제 #3
0
 /**
  * @expectedException \UnexpectedValueException
  */
 public function testFilterInvalidArgument()
 {
     $this->rest->filter('invalid data', new NullFilter());
 }