-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpaginator.php
More file actions
92 lines (79 loc) · 2.13 KB
/
Copy pathpaginator.php
File metadata and controls
92 lines (79 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php namespace Feather\Components\Pagination;
use URI;
use HTML;
use Request;
use Feather\Components\Foundation\Component;
class Paginator extends \Laravel\Paginator {
/**
* Overload the constructor so that we can get instances of it with the facades.
*
* @param array $results
* @param int $page
* @param int $total
* @param int $per_page
* @param int $last
* @return void
*/
public function __construct($results = null, $page = null, $total = null, $per_page = null, $last = null)
{
$this->page = $page;
$this->last = $last;
$this->total = $total;
$this->results = $results;
$this->per_page = $per_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)
{
$page = static::page($total, $per_page);
$last = ceil($total / $per_page);
return new static($results, $page, $total, $per_page, $last);
}
/**
* Get the current page from the last segment of the URI.
*
* @param int $total
* @param int $per_page
* @return int
*/
public static function page($total, $per_page)
{
if(!is_numeric($page = substr(URI::$segments[count(URI::$segments) - 1], 1)))
{
$page = 1;
}
// 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.
if (is_numeric($page) and $page > ($last = ceil($total / $per_page)))
{
return ($last > 0) ? $last : 1;
}
return $page;
}
/**
* Create a HTML page link.
*
* @param int $page
* @param string $text
* @param string $class
* @return string
*/
protected function link($page, $text, $class)
{
$page = "p{$page}";
if(preg_match('/p([0-9]+)/', $page))
{
$uri = preg_replace('/\/p([0-9]+)/', '', URI::current(), 1);
}
return HTML::link($uri . '/' . $page, $text, compact('class'), Request::secure());
}
}