/** * Inject a "self" link * * @param LinkCollectionAwareInterface $resource */ protected function injectSelfLink(LinkCollectionAwareInterface $resource) { if ($this->renderLinks) { if (!$resource->getLinks()->has('self')) { $self = new Link('self'); $resourceIdentifier = $resource instanceof BackendResource ? $resource->getIdentifier() : $this->getIdentifier(); $params = $this->getRouteMatch()->getParams(); $params['resource'] = $resourceIdentifier; if ($resource instanceof BackendResource) { $params['id'] = $resource->getId(); } $self->setRoute($this->getRouteMatch()->getMatchedRouteName()); $self->setRouteParams($params); $this->renderUrlForLink($self); $resource->getLinks()->add($self); } } }
/** * Generates Links and add them to a Link Collection for pagination * using Link Relation Specification and a provided Resource Event * * @see http://www.iana.org/assignments/link-relations/link-relations.xhtml * @param ResourceEvent $e */ public function composeLinksFromResourceEvent(ResourceEvent $e) { $params = $e->getParams() instanceof \ArrayObject ? $e->getParams()->getArrayCopy() : $e->getParams(); $params = ArrayUtils::merge($e->getRouteMatch()->getParams(), $params); $params = ArrayUtils::merge(array('resource' => $e->getResourceIdentifier()), $params); $route = $e->getRouteMatch()->getMatchedRouteName(); // Calculate last page $lastPage = round($this->getTotalItems() / $this->getPageSize()); if ($lastPage <= 1) { $lastPage = false; } $page = $this->getPage() + 1; $nextPage = $page < $lastPage ? $page + 1 : false; $prevPage = $page > 1 ? $page - 1 : false; if ($page > 1) { $first = new Link('first'); $first->setRoute($route); $first->setRouteParams($params); $this->getLinks()->add($first); } if ($nextPage) { $next = new Link('next'); $next->setRoute($route); $next->setRouteParams($params); $next->setRouteOptions(array('query' => array('page' => $nextPage))); $this->getLinks()->add($next); } if ($prevPage) { $prev = new Link('prev'); $prev->setRoute($route); $prev->setRouteParams($params); $prev->setRouteOptions(array('query' => array('page' => $prevPage))); $this->getLinks()->add($prev); } if ($lastPage) { $last = new Link('last'); $last->setRoute($route); $last->setRouteParams($params); $last->setRouteOptions(array('query' => array('page' => $lastPage))); $this->getLinks()->add($last); } }