html = $html; $this->lang = $lang; $this->request = $request; } public function make($results, $total, $per_page) { $page = Paginator::page($total, $per_page); $last_page = ceil($total / $per_page); return new Paginator($this->request, $this->html, $this->lang, $results, $page, $total, $per_page, $last_page); } } class Paginator { /** * The results for the current page. * * @var array */ public $results; /** * The total number of results. * * @var int */ public $total; /** * The current page. * * @var int */ public $page; /** * The number of items per page. * * @var int */ public $per_page; /** * The last page available for the result set. * * @var int */ public $last_page; /** * The language that should be used when generating page links. * * @var string */ public $language; /** * The values that should be appended to the end of the link query strings. * * @var array */ public $append = array(); /** * Create a new Paginator instance. * * @param array $results * @param int $page * @param int $total * @param int $per_page * @param int $last_page * @return void */ protected function __construct(Request $request, HTML $html, Lang_Factory $lang, $results, $page, $total, $per_page, $last_page) { $this->html = $html; $this->lang = $lang; $this->page = $page; $this->total = $total; $this->request = $request; $this->results = $results; $this->per_page = $per_page; $this->last_page = $last_page; } /** * Create a new Paginator instance. * * @param array $results * @param int $total * @param int $per_page * @return Paginator */ public static function make($results, $total, $per_page) { return new static($results, static::page($total, $per_page), $total, $per_page, ceil($total / $per_page)); } /** * Get the current page from the request query string. * * The page will be validated and adjusted if it is less than one or greater than the last page. * For example, if the current page is not an integer or less than one, one will be returned. * If the current page is greater than the last page, the last page will be returned. * * @param int $total * @param int $per_page * @return int */ public static function page($total, $per_page) { $page = IoC::container()->resolve('laravel.input')->get('page', 1); if (is_numeric($page) and $page > $last_page = ceil($total / $per_page)) { return ($last_page > 0) ? $last_page : 1; } return ($page < 1 or filter_var($page, FILTER_VALIDATE_INT) === false) ? 1 : $page; } /** * Create the HTML pagination links. * * @param int $adjacent * @return string */ public function links($adjacent = 3) { if ($this->last_page <= 1) return ''; // The hard-coded "7" is to account for all of the constant elements in a sliding range. // Namely: The the current page, the two ellipses, the two beginning pages, and the two ending pages. if ($this->last_page < 7 + ($adjacent * 2)) { $numbers = $this->range(1, $this->last_page); } else { $numbers = $this->slider($adjacent); } return '