Commit dac63a50 authored by Taylor Otwell's avatar Taylor Otwell

Refactored the paginator class for cleanliness.

parent 8d2eefe1
...@@ -30,13 +30,6 @@ class Paginator { ...@@ -30,13 +30,6 @@ class Paginator {
*/ */
public $per_page; public $per_page;
/**
* The last page number.
*
* @var int
*/
public $last_page;
/** /**
* The language that should be used when generating page links. * The language that should be used when generating page links.
* *
...@@ -44,13 +37,6 @@ class Paginator { ...@@ -44,13 +37,6 @@ class Paginator {
*/ */
public $language; public $language;
/**
* Indicates if HTTPS links should be generated.
*
* @var bool
*/
public $https = false;
/** /**
* Create a new Paginator instance. * Create a new Paginator instance.
* *
...@@ -62,7 +48,6 @@ class Paginator { ...@@ -62,7 +48,6 @@ class Paginator {
public function __construct($results, $total, $per_page) public function __construct($results, $total, $per_page)
{ {
$this->page = static::page($total, $per_page); $this->page = static::page($total, $per_page);
$this->last_page = ceil($total / $per_page);
$this->per_page = $per_page; $this->per_page = $per_page;
$this->results = $results; $this->results = $results;
$this->total = $total; $this->total = $total;
...@@ -86,7 +71,7 @@ class Paginator { ...@@ -86,7 +71,7 @@ class Paginator {
return $last_page; return $last_page;
} }
return (filter_var($page, FILTER_VALIDATE_INT) === false or $page < 1) ? 1 : $page; return ($page < 1 or filter_var($page, FILTER_VALIDATE_INT) === false) ? 1 : $page;
} }
/** /**
...@@ -97,7 +82,7 @@ class Paginator { ...@@ -97,7 +82,7 @@ class Paginator {
*/ */
public function links($adjacent = 3) public function links($adjacent = 3)
{ {
return ($this->last_page > 1) ? '<div class="pagination">'.$this->previous().$this->numbers($adjacent).$this->next().'</div>' : ''; return ($this->last_page() > 1) ? '<div class="pagination">'.$this->previous().$this->numbers($adjacent).$this->next().'</div>' : '';
} }
/** /**
...@@ -110,7 +95,7 @@ class Paginator { ...@@ -110,7 +95,7 @@ class Paginator {
*/ */
private function numbers($adjacent = 3) private function numbers($adjacent = 3)
{ {
return ($this->last_page < 7 + ($adjacent * 2)) ? $this->range(1, $this->last_page) : $this->slider($adjacent); return ($this->last_page() < 7 + ($adjacent * 2)) ? $this->range(1, $this->last_page()) : $this->slider($adjacent);
} }
/** /**
...@@ -125,13 +110,15 @@ class Paginator { ...@@ -125,13 +110,15 @@ class Paginator {
{ {
return $this->range(1, 2 + ($adjacent * 2)).$this->ending(); return $this->range(1, 2 + ($adjacent * 2)).$this->ending();
} }
elseif ($this->page >= $this->last_page - ($adjacent * 2)) elseif ($this->page >= $this->last_page() - ($adjacent * 2))
{ {
return $this->beginning().$this->range($this->last_page - 2 - ($adjacent * 2), $this->last_page); return $this->beginning().$this->range($this->last_page() - 2 - ($adjacent * 2), $this->last_page());
} }
else
{
return $this->beginning().$this->range($this->page - $adjacent, $this->page + $adjacent).$this->ending(); return $this->beginning().$this->range($this->page - $adjacent, $this->page + $adjacent).$this->ending();
} }
}
/** /**
* Generate the "previous" HTML link. * Generate the "previous" HTML link.
...@@ -140,7 +127,7 @@ class Paginator { ...@@ -140,7 +127,7 @@ class Paginator {
*/ */
public function previous() public function previous()
{ {
$text = Lang::line('pagination.previous')->get($this->language); $text = Lang::line('pagination.previous')->get();
return ($this->page > 1) ? $this->link($this->page - 1, $text, 'prev_page').' ' : HTML::span($text, array('class' => 'disabled prev_page')).' '; return ($this->page > 1) ? $this->link($this->page - 1, $text, 'prev_page').' ' : HTML::span($text, array('class' => 'disabled prev_page')).' ';
} }
...@@ -152,9 +139,9 @@ class Paginator { ...@@ -152,9 +139,9 @@ class Paginator {
*/ */
public function next() public function next()
{ {
$text = Lang::line('pagination.next')->get($this->language); $text = Lang::line('pagination.next')->get();
return ($this->page < $this->last_page) ? $this->link($this->page + 1, $text, 'next_page') : HTML::span($text, array('class' => 'disabled next_page')).' '; return ($this->page < $this->last_page()) ? $this->link($this->page + 1, $text, 'next_page') : HTML::span($text, array('class' => 'disabled next_page'));
} }
/** /**
...@@ -174,7 +161,7 @@ class Paginator { ...@@ -174,7 +161,7 @@ class Paginator {
*/ */
private function ending() private function ending()
{ {
return $this->dots().$this->range($this->last_page - 1, $this->last_page); return $this->dots().$this->range($this->last_page() - 1, $this->last_page());
} }
/** /**
...@@ -187,7 +174,7 @@ class Paginator { ...@@ -187,7 +174,7 @@ class Paginator {
*/ */
private function link($page, $text, $class) private function link($page, $text, $class)
{ {
return HTML::link(Request::uri().'?page='.$page, $text, array('class' => $class), $this->https); return HTML::link(Request::uri().'?page='.$page, $text, array('class' => $class), Request::is_secure());
} }
/** /**
...@@ -222,25 +209,24 @@ class Paginator { ...@@ -222,25 +209,24 @@ class Paginator {
} }
/** /**
* Set the language that should be used when generating pagination links. * Determine the last page number based on the total pages and per page limit.
* *
* @param string $language * @return int
* @return Paginator
*/ */
public function lang($language) private function last_page()
{ {
$this->language = $language; return ceil($this->total / $this->per_page);
return $this;
} }
/** /**
* Force the pagination links to use HTTPS. * Set the language that should be used when generating page links.
* *
* @param string $language
* @return Paginator * @return Paginator
*/ */
public function secure() public function lang($language)
{ {
$this->https = true; $this->language = $language;
return $this; return $this;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment