Exemplo n.º 1
0
 /**
  * Builds and returns the pagination array.
  *
  * @access  public
  * @return  array
  */
 public function pagination()
 {
     if (empty($this->pagination)) {
         if (empty($this->request)) {
             throw new RuntimeException(vsprintf("%s(): A [ Request ] instance is required to generate the pagination array.", [__METHOD__]));
         }
         if (empty($this->urlBuilder)) {
             throw new RuntimeException(vsprintf("%s(): A [ URLBuilder ] instance is required to generate the pagination array.", [__METHOD__]));
         }
         $pagination = ['items' => $this->items, 'items_per_page' => $this->itemsPerPage, 'number_of_pages' => $this->pages];
         $params = $this->request->get();
         if ($this->currentPage > 1) {
             $pagination['first'] = $this->urlBuilder->current(array_merge($params, [$this->options['page_key'] => 1]));
             $pagination['previous'] = $this->urlBuilder->current(array_merge($params, [$this->options['page_key'] => $this->currentPage - 1]));
         }
         if ($this->currentPage < $this->pages) {
             $pagination['last'] = $this->urlBuilder->current(array_merge($params, [$this->options['page_key'] => $this->pages]));
             $pagination['next'] = $this->urlBuilder->current(array_merge($params, [$this->options['page_key'] => $this->currentPage + 1]));
         }
         if ($this->options['max_page_links'] !== 0) {
             if ($this->pages > $this->options['max_page_links']) {
                 $start = max($this->currentPage - ceil($this->options['max_page_links'] / 2), 0);
                 $end = $start + $this->options['max_page_links'];
                 if ($end > $this->pages) {
                     $end = $this->pages;
                 }
                 if ($start > $end - $this->options['max_page_links']) {
                     $start = $end - $this->options['max_page_links'];
                 }
             } else {
                 $start = 0;
                 $end = $this->pages;
             }
             $pagination['pages'] = [];
             for ($i = $start + 1; $i <= $end; $i++) {
                 $pagination['pages'][] = ['url' => $this->urlBuilder->current(array_merge($params, [$this->options['page_key'] => $i])), 'number' => $i, 'is_current' => $i == $this->currentPage];
             }
         }
         $this->pagination = $pagination;
     }
     return $this->pagination;
 }
 /**
  *
  */
 public function testToCurrentWithQueryParams()
 {
     $request = $this->getRequest();
     $request->shouldReceive('get')->once()->andReturn(['foo' => 'bar']);
     $urlBuilder = new URLBuilder($request, $this->getRoutes(), true);
     $this->assertEquals('http://example.org/foo/bar?foo=bar', $urlBuilder->current());
     $this->assertEquals('http://example.org/foo/bar?bar=foo', $urlBuilder->current(['bar' => 'foo']));
 }