/** * Transforms root of visitor with additional data based on the representation. * * @param PaginatedRepresentation $representation * @param JsonApiSerializationVisitor $visitor * @param Context $context * * @return mixed */ protected function transformRoot(PaginatedRepresentation $representation, JsonApiSerializationVisitor $visitor, Context $context) { // serialize items $data = $context->accept($representation->getItems()); $root = $visitor->getRoot(); $root['meta'] = array('page' => $representation->getPage(), 'limit' => $representation->getLimit(), 'pages' => $representation->getPages(), 'total' => $representation->getTotal()); $root['links'] = array('first' => $this->getUriForPage(1), 'last' => $this->getUriForPage($representation->getPages()), 'next' => $representation->hasNextPage() ? $this->getUriForPage($representation->getNextPage()) : null, 'previous' => $representation->hasPreviousPage() ? $this->getUriForPage($representation->getPreviousPage()) : null); $visitor->setRoot($root); return $data; }
/** * @param JsonApiSerializationVisitor $visitor * @param Pagerfanta $pagerfanta * @param array $type * @param Context $context * @return Pagerfanta */ public function serializePagerfanta(JsonApiSerializationVisitor $visitor, Pagerfanta $pagerfanta, array $type, Context $context) { $request = $this->requestStack->getCurrentRequest(); $pagerfanta->setNormalizeOutOfRangePages(true); $pagerfanta->setAllowOutOfRangePages(true); $pagerfanta->setMaxPerPage($request->get('page[limit]', $this->paginationOptions['limit'], true)); $pagerfanta->setCurrentPage($request->get('page[number]', 1, true)); $results = $pagerfanta->getCurrentPageResults(); if ($results instanceof \ArrayIterator) { $results = $results->getArrayCopy(); } $data = $context->accept($results); $root = $visitor->getRoot(); $root['meta'] = array('page' => $pagerfanta->getCurrentPage(), 'limit' => $pagerfanta->getMaxPerPage(), 'pages' => $pagerfanta->getNbPages(), 'total' => $pagerfanta->getNbResults()); $root['links'] = array('first' => $this->getUriForPage(1), 'last' => $this->getUriForPage($pagerfanta->getNbPages()), 'prev' => $pagerfanta->hasPreviousPage() ? $this->getUriForPage($pagerfanta->getPreviousPage()) : null, 'next' => $pagerfanta->hasNextPage() ? $this->getUriForPage($pagerfanta->getNextPage()) : null); $visitor->setRoot($root); return $data; }