blob: 68d4b4efa1f67df79ffc95ac38b81268dfbf148b (
plain) (
blame)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
<?php
namespace Drupal\Core\Pager;
/**
* A value object that represents a pager.
*/
class Pager {
/**
* The total number of items .
*
* @var int
*/
protected $totalItems;
/**
* The total number of pages.
*
* @var int
*/
protected $totalPages;
/**
* The current page of the pager.
*
* @var int
*/
protected $currentPage;
/**
* The maximum number of items per page.
*
* @var int
*/
protected $limit;
/**
* Pager constructor.
*
* @param int $totalItems
* The total number of items.
* @param int $limit
* The maximum number of items per page.
* @param int $currentPage
* The current page.
*/
public function __construct($totalItems, $limit, $currentPage = 0) {
$this->totalItems = $totalItems;
$this->limit = $limit;
$this->setTotalPages($totalItems, $limit);
$this->setCurrentPage($currentPage);
}
/**
* Sets the current page to a valid value within range.
*
* If a page that does not correspond to the actual range of the result set
* was provided, this function will set the closest page actually within
* the result set.
*
* @param int $currentPage
* (optional) The current page.
*/
protected function setCurrentPage($currentPage = 0) {
$this->currentPage = max(0, min($currentPage, $this->getTotalPages() - 1));
}
/**
* Sets the total number of pages.
*
* @param int $totalItems
* The total number of items.
* @param int $limit
* The maximum number of items per page.
*/
protected function setTotalPages($totalItems, $limit) {
$this->totalPages = (int) ceil($totalItems / $limit);
}
/**
* Gets the total number of items.
*
* @return int
* The total number of items.
*/
public function getTotalItems() {
return $this->totalItems;
}
/**
* Gets the total number of pages.
*
* @return int
* The total number of pages.
*/
public function getTotalPages() {
return $this->totalPages;
}
/**
* Gets the current page.
*
* @return int
* The current page.
*/
public function getCurrentPage() {
return $this->currentPage;
}
/**
* Gets the maximum number of items per page.
*
* @return int
* The maximum number of items per page.
*/
public function getLimit() {
return $this->limit;
}
}
|