vendor/knplabs/knp-components/src/Knp/Component/Pager/Pagination/SlidingPagination.php line 12

Open in your IDE?
  1. <?php
  2. namespace Knp\Component\Pager\Pagination;
  3. use Closure;
  4. /**
  5.  * @todo: find a way to avoid exposing private member setters
  6.  *
  7.  * Sliding pagination
  8.  */
  9. final class SlidingPagination extends AbstractPagination
  10. {
  11.     /**
  12.      * Pagination page range
  13.      */
  14.     private int $range 5;
  15.     /**
  16.      * Closure which is executed to render pagination
  17.      */
  18.     public ?Closure $renderer null;
  19.     public function setPageRange(int $range): void
  20.     {
  21.         $this->range abs($range);
  22.     }
  23.     /**
  24.      * Renders the pagination
  25.      */
  26.     public function __toString(): string
  27.     {
  28.         $data $this->getPaginationData();
  29.         $output 'override in order to render a template';
  30.         if ($this->renderer instanceof Closure) {
  31.             $output call_user_func($this->renderer$data);
  32.         }
  33.         return $output;
  34.     }
  35.     public function getPaginationData(): array
  36.     {
  37.         $pageCount = (int) ceil($this->totalCount $this->numItemsPerPage);
  38.         $current $this->currentPageNumber;
  39.         if ($this->range $pageCount) {
  40.             $this->range $pageCount;
  41.         }
  42.         $delta ceil($this->range 2);
  43.         if ($current $delta $pageCount $this->range) {
  44.             $pages range($pageCount $this->range 1$pageCount);
  45.         } else {
  46.             if ($current $delta 0) {
  47.                 $delta $current;
  48.             }
  49.             $offset $current $delta;
  50.             $pages range($offset 1$offset $this->range);
  51.         }
  52.         $viewData = [
  53.             'last' => $pageCount,
  54.             'current' => $current,
  55.             'numItemsPerPage' => $this->numItemsPerPage,
  56.             'first' => 1,
  57.             'pageCount' => $pageCount,
  58.             'totalCount' => $this->totalCount,
  59.         ];
  60.         $viewData array_merge($viewData$this->paginatorOptions$this->customParameters);
  61.         if ($current 0) {
  62.             $viewData['previous'] = $current 1;
  63.         }
  64.         if ($current <= $pageCount) {
  65.             $viewData['next'] = $current 1;
  66.         }
  67.         $viewData['pagesInRange'] = $pages;
  68.         $viewData['firstPageInRange'] = min($pages);
  69.         $viewData['lastPageInRange']  = max($pages);
  70.         if ($this->getItems() !== null) {
  71.             $viewData['currentItemCount'] = $this->count();
  72.             $viewData['firstItemNumber'] = (($current 1) * $this->numItemsPerPage) + 1;
  73.             $viewData['lastItemNumber'] = $viewData['firstItemNumber'] + $viewData['currentItemCount'] - 1;
  74.         }
  75.         return $viewData;
  76.     }
  77. }