/**
  * @param GetResponseForControllerResultEvent $event
  */
 public function onKernelView(GetResponseForControllerResultEvent $event)
 {
     $request = $event->getRequest();
     $document = $this->documentRepository->get($request->attributes->get('_definition'));
     switch ($document->getDefinition()->info->version) {
         case '2.0.0':
             $result = $event->getControllerResult();
             if (!$result instanceof Resource && !$result instanceof Document) {
                 $result = new Resource($result, $this->serializer);
             }
             if ($result instanceof Resource) {
                 $result = new Document($result);
             }
             if ($result instanceof Document) {
                 $result->addLink('self', $event->getRequest()->getPathInfo());
                 $event->setControllerResult($result->toArray());
             }
             break;
         default:
             //noop
     }
 }
Example #2
0
 /**
  * Add pagination links to a JSON-API response, based on input parameters
  * and the default parameters of this action.
  *
  * @param Document $document
  * @param JsonApiRequest $request
  * @param string $url The base URL to build pagination links with.
  * @param integer|boolean $total The total number of results (used to build
  *     a 'last' link), or just true if there are more results but how many
  *     is unknown ('last' link is ommitted).
  * @return void
  */
 protected function addPaginationLinks(Document $document, JsonApiRequest $request, $url, $total = true)
 {
     $input = [];
     if ($request->limit != $this->limit) {
         array_set($input, 'page.limit', $request->limit);
     }
     array_set($input, 'page.offset', 0);
     $document->addLink('first', $url . '?' . http_build_query($input));
     if ($request->offset > 0) {
         array_set($input, 'page.offset', max(0, $request->offset - $request->limit));
         $document->addLink('prev', $url . '?' . http_build_query($input));
     }
     if ($total === true || $request->offset + $request->limit < $total) {
         array_set($input, 'page.offset', $request->offset + $request->limit);
         $document->addLink('next', $url . '?' . http_build_query($input));
     }
     if ($total && $total !== true) {
         array_set($input, 'page.offset', $total - $request->limit);
         $document->addLink('last', $url . '?' . http_build_query($input));
     }
 }