1: <?php
2:
3: /**
4: * This file is part of the Nette Framework.
5: *
6: * Copyright (c) 2004, 2010 David Grudl (http://davidgrudl.com)
7: *
8: * This source file is subject to the "Nette license", and/or
9: * GPL license. For more information please see http://nette.org
10: * @package Nette
11: */
12:
13:
14:
15: /**
16: * Paginating math.
17: *
18: * @author David Grudl
19: *
20: * @property int $page
21: * @property-read int $firstPage
22: * @property-read int $lastPage
23: * @property int $base
24: * @property-read int $pageCount
25: * @property int $itemsPerPage
26: * @property int $itemCount
27: * @property-read int $offset
28: * @property-read int $countdownOffset
29: * @property-read int $length
30: * @property-read bool $first
31: * @property-read bool $last
32: */
33: class Paginator extends Object
34: {
35: /** @var int */
36: private $base = 1;
37:
38: /** @var int */
39: private $itemsPerPage = 1;
40:
41: /** @var int */
42: private $page;
43:
44: /** @var int */
45: private $itemCount = 0;
46:
47:
48:
49: /**
50: * Sets current page number.
51: * @param int
52: * @return Paginator provides a fluent interface
53: */
54: public function setPage($page)
55: {
56: $this->page = (int) $page;
57: return $this;
58: }
59:
60:
61:
62: /**
63: * Returns current page number.
64: * @return int
65: */
66: public function getPage()
67: {
68: return $this->base + $this->getPageIndex();
69: }
70:
71:
72:
73: /**
74: * Returns first page number.
75: * @return int
76: */
77: public function getFirstPage()
78: {
79: return $this->base;
80: }
81:
82:
83:
84: /**
85: * Returns last page number.
86: * @return int
87: */
88: public function getLastPage()
89: {
90: return $this->base + max(0, $this->getPageCount() - 1);
91: }
92:
93:
94:
95: /**
96: * Sets first page (base) number.
97: * @param int
98: * @return Paginator provides a fluent interface
99: */
100: public function setBase($base)
101: {
102: $this->base = (int) $base;
103: return $this;
104: }
105:
106:
107:
108: /**
109: * Returns first page (base) number.
110: * @return int
111: */
112: public function getBase()
113: {
114: return $this->base;
115: }
116:
117:
118:
119: /**
120: * Returns zero-based page number.
121: * @return int
122: */
123: protected function getPageIndex()
124: {
125: return min(max(0, $this->page - $this->base), max(0, $this->getPageCount() - 1));
126: }
127:
128:
129:
130: /**
131: * Is the current page the first one?
132: * @return bool
133: */
134: public function isFirst()
135: {
136: return $this->getPageIndex() === 0;
137: }
138:
139:
140:
141: /**
142: * Is the current page the last one?
143: * @return bool
144: */
145: public function isLast()
146: {
147: return $this->getPageIndex() >= $this->getPageCount() - 1;
148: }
149:
150:
151:
152: /**
153: * Returns the total number of pages.
154: * @return int
155: */
156: public function getPageCount()
157: {
158: return (int) ceil($this->itemCount / $this->itemsPerPage);
159: }
160:
161:
162:
163: /**
164: * Sets the number of items to display on a single page.
165: * @param int
166: * @return Paginator provides a fluent interface
167: */
168: public function setItemsPerPage($itemsPerPage)
169: {
170: $this->itemsPerPage = max(1, (int) $itemsPerPage);
171: return $this;
172: }
173:
174:
175:
176: /**
177: * Returns the number of items to display on a single page.
178: * @return int
179: */
180: public function getItemsPerPage()
181: {
182: return $this->itemsPerPage;
183: }
184:
185:
186:
187: /**
188: * Sets the total number of items.
189: * @param int (or FALSE as infinity)
190: * @return Paginator provides a fluent interface
191: */
192: public function setItemCount($itemCount)
193: {
194: $this->itemCount = $itemCount === FALSE ? PHP_INT_MAX : max(0, (int) $itemCount);
195: return $this;
196: }
197:
198:
199:
200: /**
201: * Returns the total number of items.
202: * @return int
203: */
204: public function getItemCount()
205: {
206: return $this->itemCount;
207: }
208:
209:
210:
211: /**
212: * Returns the absolute index of the first item on current page.
213: * @return int
214: */
215: public function getOffset()
216: {
217: return $this->getPageIndex() * $this->itemsPerPage;
218: }
219:
220:
221:
222: /**
223: * Returns the absolute index of the first item on current page in countdown paging.
224: * @return int
225: */
226: public function getCountdownOffset()
227: {
228: return max(0, $this->itemCount - ($this->getPageIndex() + 1) * $this->itemsPerPage);
229: }
230:
231:
232:
233: /**
234: * Returns the number of items on current page.
235: * @return int
236: */
237: public function getLength()
238: {
239: return min($this->itemsPerPage, $this->itemCount - $this->getPageIndex() * $this->itemsPerPage);
240: }
241:
242: }
243: