1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (http://nette.org)
5: *
6: * Copyright (c) 2004, 2011 David Grudl (http://davidgrudl.com)
7: *
8: * For the full copyright and license information, please view
9: * the file license.txt that was distributed with this source code.
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 NPaginator extends NObject
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|NULL */
45: private $itemCount;
46:
47:
48:
49: /**
50: * Sets current page number.
51: * @param int
52: * @return NPaginator 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|NULL
87: */
88: public function getLastPage()
89: {
90: return $this->itemCount === NULL ? NULL : $this->base + max(0, $this->getPageCount() - 1);
91: }
92:
93:
94:
95: /**
96: * Sets first page (base) number.
97: * @param int
98: * @return NPaginator 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: $index = max(0, $this->page - $this->base);
126: return $this->itemCount === NULL ? $index : min($index, max(0, $this->getPageCount() - 1));
127: }
128:
129:
130:
131: /**
132: * Is the current page the first one?
133: * @return bool
134: */
135: public function isFirst()
136: {
137: return $this->getPageIndex() === 0;
138: }
139:
140:
141:
142: /**
143: * Is the current page the last one?
144: * @return bool
145: */
146: public function isLast()
147: {
148: return $this->itemCount === NULL ? FALSE : $this->getPageIndex() >= $this->getPageCount() - 1;
149: }
150:
151:
152:
153: /**
154: * Returns the total number of pages.
155: * @return int|NULL
156: */
157: public function getPageCount()
158: {
159: return $this->itemCount === NULL ? NULL : (int) ceil($this->itemCount / $this->itemsPerPage);
160: }
161:
162:
163:
164: /**
165: * Sets the number of items to display on a single page.
166: * @param int
167: * @return NPaginator provides a fluent interface
168: */
169: public function setItemsPerPage($itemsPerPage)
170: {
171: $this->itemsPerPage = max(1, (int) $itemsPerPage);
172: return $this;
173: }
174:
175:
176:
177: /**
178: * Returns the number of items to display on a single page.
179: * @return int
180: */
181: public function getItemsPerPage()
182: {
183: return $this->itemsPerPage;
184: }
185:
186:
187:
188: /**
189: * Sets the total number of items.
190: * @param int (or NULL as infinity)
191: * @return NPaginator provides a fluent interface
192: */
193: public function setItemCount($itemCount)
194: {
195: $this->itemCount = ($itemCount === FALSE || $itemCount === NULL) ? NULL : max(0, (int) $itemCount);
196: return $this;
197: }
198:
199:
200:
201: /**
202: * Returns the total number of items.
203: * @return int|NULL
204: */
205: public function getItemCount()
206: {
207: return $this->itemCount;
208: }
209:
210:
211:
212: /**
213: * Returns the absolute index of the first item on current page.
214: * @return int
215: */
216: public function getOffset()
217: {
218: return $this->getPageIndex() * $this->itemsPerPage;
219: }
220:
221:
222:
223: /**
224: * Returns the absolute index of the first item on current page in countdown paging.
225: * @return int|NULL
226: */
227: public function getCountdownOffset()
228: {
229: return $this->itemCount === NULL
230: ? NULL
231: : max(0, $this->itemCount - ($this->getPageIndex() + 1) * $this->itemsPerPage);
232: }
233:
234:
235:
236: /**
237: * Returns the number of items on current page.
238: * @return int|NULL
239: */
240: public function getLength()
241: {
242: return $this->itemCount === NULL
243: ? $this->itemsPerPage
244: : min($this->itemsPerPage, $this->itemCount - $this->getPageIndex() * $this->itemsPerPage);
245: }
246:
247: }
248: