Example #1
0
 public function testSetCurrentPageShouldNormalizePageWhenOutOfRangePageAndIndicatingNormalizeOutOfRangePages()
 {
     $this->setAdapterNbResultsAny(100);
     $this->pagerfanta->setMaxPerPage(10);
     $this->pagerfanta->setAllowOutOfRangePages(false);
     $this->pagerfanta->setNormalizeOutOfRangePages(true);
     $this->pagerfanta->setCurrentPage(11);
     $this->assertSame(10, $this->pagerfanta->getCurrentPage());
 }
 /**
  * @param FilterInterface $filter
  * @param $searchParams
  * @param $page
  * @param $maxPerPage
  * @param Pagerfanta $pagerfanta
  * @return array
  */
 public function paginate($filter, $searchParams, $page, $maxPerPage, &$pagerfanta = null)
 {
     /** @var QueryBuilder $qb */
     $qb = $this->createQueryBuilder('x');
     $query = FilterBuilder::create()->setQueryBuilder($qb)->setFilter($filter)->buildQuery($searchParams)->getQuery();
     $adapter = new DoctrineORMAdapter($query, true, false);
     $pagerfanta = new Pagerfanta($adapter);
     $pagerfanta->setAllowOutOfRangePages(true)->setMaxPerPage($maxPerPage)->setCurrentPage($page);
     return iterator_to_array($pagerfanta->getCurrentPageResults());
 }
 /**
  * @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;
 }