Namespaces

  • Nette
    • Application
    • Caching
    • Collections
    • Config
    • Forms
    • IO
    • Loaders
    • Mail
    • Reflection
    • Security
    • Templates
    • Web
  • None
  • PHP

Classes

  • ArrayTools
  • Callback
  • Component
  • ComponentContainer
  • Configurator
  • DateTime
  • Debug
  • Environment
  • Framework
  • FreezableObject
  • GenericRecursiveIterator
  • Image
  • ImageMagick
  • InstanceFilterIterator
  • Object
  • ObjectMixin
  • Paginator
  • RecursiveComponentIterator
  • ServiceLocator
  • SmartCachingIterator
  • String
  • Tools

Interfaces

  • IComponent
  • IComponentContainer
  • IDebuggable
  • IServiceLocator
  • ITranslator

Exceptions

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