Exemplo n.º 1
0
 public function testCanSetRouteOptionsSeparately()
 {
     $route = 'api/docs';
     $options = array('query' => 'version=1.1');
     $link = new Link('describedby');
     $link->setRoute($route);
     $link->setRouteOptions($options);
     $this->assertEquals($route, $link->getRoute());
     $this->assertEquals($options, $link->getRouteOptions());
 }
Exemplo n.º 2
0
 /**
  * Generate HAL links for a paginated collection
  *
  * @param  HalCollection $halCollection
  * @return array
  */
 protected function injectPaginationLinks(HalCollection $halCollection)
 {
     $collection = $halCollection->collection;
     $page = $halCollection->page;
     $pageSize = $halCollection->pageSize;
     $route = $halCollection->collectionRoute;
     $params = $halCollection->collectionRouteParams;
     $options = $halCollection->collectionRouteOptions;
     $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, array('query' => array('page' => $page))));
     $links->add($link, true);
     // first link
     $link = new Link('first');
     $link->setRoute($route);
     $link->setRouteParams($params);
     $link->setRouteOptions($options);
     $links->add($link);
     // last link
     $link = new Link('last');
     $link->setRoute($route);
     $link->setRouteParams($params);
     $link->setRouteOptions(ArrayUtils::merge($options, array('query' => array('page' => $count))));
     $links->add($link);
     // prev link
     if ($prev) {
         $link = new Link('prev');
         $link->setRoute($route);
         $link->setRouteParams($params);
         $link->setRouteOptions(ArrayUtils::merge($options, array('query' => array('page' => $prev))));
         $links->add($link);
     }
     // next link
     if ($next) {
         $link = new Link('next');
         $link->setRoute($route);
         $link->setRouteParams($params);
         $link->setRouteOptions(ArrayUtils::merge($options, array('query' => array('page' => $next))));
         $links->add($link);
     }
     return true;
 }