/** * Build Pages Range * * @param Next\Paginate\Paginator $paginator * Paginator Object * * @return array * Range of pages */ public function buildRange(Paginator $paginator) { $currentPage = $paginator->getCurrentPage(); $total = count($paginator); /** * Lower Bound * * Cannot be lower than one, otherwise we'll have negative pages */ $lowerBound = $currentPage - $this->before; $lowerBound = $lowerBound < 1 ? 1 : $lowerBound; /** * Upper Bound * * Cannot be higher than total of records, otherwise we'll have * offset errors */ $upperBound = $currentPage + $this->after; $upperBound = $upperBound > $total ? $total : $upperBound; return range($lowerBound, $upperBound); }
/** * Build Pages Range * * @param Next\Paginate\Paginator $paginator * Paginator Object * * @return array * Range of pages */ public function buildRange(Paginator $paginator) { $itemsPerPage = $paginator->getItemsPerPage(); $currentPage = $paginator->getCurrentPage(); $total = count($paginator); if ($itemsPerPage > $total) { $itemsPerPage = $total; } $delta = ceil($itemsPerPage / 2); if ($currentPage - $delta > $total - $itemsPerPage) { $lowerBound = $total - $itemsPerPage + 1; $upperBound = $total; } else { // We're getting close to end, let's change a little bit if ($currentPage - $delta < 0) { $delta = $currentPage; } $offset = $currentPage - $delta; $lowerBound = $offset + 1; $upperBound = $offset + $itemsPerPage; } return range($lowerBound, $upperBound); }