Пример #1
0
 /**
  * Generate HAL links for a paginated collection
  *
  * @param  Collection $halCollection
  * @return boolean
  */
 protected function injectPaginationLinks(Collection $halCollection)
 {
     $collection = $halCollection->getCollection();
     $page = $halCollection->getPage();
     $pageSize = $halCollection->getPageSize();
     $route = $halCollection->getCollectionRoute();
     $params = $halCollection->getCollectionRouteParams();
     $options = $halCollection->getCollectionRouteOptions();
     $collection->setItemCountPerPage($pageSize);
     $collection->setCurrentPageNumber($page);
     $count = count($collection);
     if (!$count) {
         return true;
     }
     if ($page < 1 || $page > $count) {
         return new ApiProblem(409, 'Invalid page provided');
     }
     $links = $halCollection->getLinks();
     $next = $page < $count ? $page + 1 : false;
     $prev = $page > 1 ? $page - 1 : false;
     // self link
     $link = new Link('self');
     $link->setRoute($route);
     $link->setRouteParams($params);
     $link->setRouteOptions(ArrayUtils::merge($options, ['query' => ['page' => $page]]));
     $links->add($link, true);
     // first link
     $link = new Link('first');
     $link->setRoute($route);
     $link->setRouteParams($params);
     $link->setRouteOptions(ArrayUtils::merge($options, ['query' => ['page' => null]]));
     $links->add($link);
     // last link
     $link = new Link('last');
     $link->setRoute($route);
     $link->setRouteParams($params);
     $link->setRouteOptions(ArrayUtils::merge($options, ['query' => ['page' => $count]]));
     $links->add($link);
     // prev link
     if ($prev) {
         $link = new Link('prev');
         $link->setRoute($route);
         $link->setRouteParams($params);
         $link->setRouteOptions(ArrayUtils::merge($options, ['query' => ['page' => $prev]]));
         $links->add($link);
     }
     // next link
     if ($next) {
         $link = new Link('next');
         $link->setRoute($route);
         $link->setRouteParams($params);
         $link->setRouteOptions(ArrayUtils::merge($options, ['query' => ['page' => $next]]));
         $links->add($link);
     }
     return true;
 }
Пример #2
0
 private function createPaginationLink($relation, Collection $halCollection, $page = null)
 {
     $options = ArrayUtils::merge($halCollection->getCollectionRouteOptions(), ['query' => ['page' => $page]]);
     return Link::factory(['rel' => $relation, 'route' => ['name' => $halCollection->getCollectionRoute(), 'params' => $halCollection->getCollectionRouteParams(), 'options' => $options]]);
 }