Beispiel #1
0
 /**
  * Prepare event parameters
  *
  * Merges any event parameters set in the resources with arguments passed
  * to a resource method, and passes them to the `prepareArgs` method of the
  * event manager.
  *
  * @param $name
  * @param array $args
  * @return ResourceEvent
  */
 protected function prepareEvent($name, array $args)
 {
     $event = new ResourceEvent($name, $this, $this->prepareEventParams($args));
     $event->setQueryParams($this->getQueryParams());
     $event->setRouteMatch($this->getRouteMatch());
     $event->setResourceIdentifier($this->getIdentifier());
     return $event;
 }
Beispiel #2
0
 /**
  * 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);
     }
 }