Packages

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

Classes

  • NArrayTools
  • NCallback
  • NComponent
  • NComponentContainer
  • NConfigurator
  • NDateTime53
  • NDebug
  • NEnvironment
  • NFramework
  • NFreezableObject
  • NGenericRecursiveIterator
  • NImage
  • NImageMagick
  • NInstanceFilterIterator
  • NObject
  • NObjectMixin
  • NPaginator
  • NRecursiveComponentIterator
  • NServiceLocator
  • NSmartCachingIterator
  • NString
  • NTools

Interfaces

  • IComponent
  • IComponentContainer
  • IDebuggable
  • IServiceLocator
  • ITranslator

Exceptions

  • NAmbiguousServiceException
  • Overview
  • Package
  • 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:  * @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:  * @package Nette
 33:  */
 34: class NPaginator extends NObject
 35: {
 36:     /** @var int */
 37:     private $base = 1;
 38: 
 39:     /** @var int */
 40:     private $itemsPerPage = 1;
 41: 
 42:     /** @var int */
 43:     private $page;
 44: 
 45:     /** @var int */
 46:     private $itemCount = 0;
 47: 
 48: 
 49: 
 50:     /**
 51:      * Sets current page number.
 52:      * @param  int
 53:      * @return NPaginator  provides a fluent interface
 54:      */
 55:     public function setPage($page)
 56:     {
 57:         $this->page = (int) $page;
 58:         return $this;
 59:     }
 60: 
 61: 
 62: 
 63:     /**
 64:      * Returns current page number.
 65:      * @return int
 66:      */
 67:     public function getPage()
 68:     {
 69:         return $this->base + $this->getPageIndex();
 70:     }
 71: 
 72: 
 73: 
 74:     /**
 75:      * Returns first page number.
 76:      * @return int
 77:      */
 78:     public function getFirstPage()
 79:     {
 80:         return $this->base;
 81:     }
 82: 
 83: 
 84: 
 85:     /**
 86:      * Returns last page number.
 87:      * @return int
 88:      */
 89:     public function getLastPage()
 90:     {
 91:         return $this->base + max(0, $this->getPageCount() - 1);
 92:     }
 93: 
 94: 
 95: 
 96:     /**
 97:      * Sets first page (base) number.
 98:      * @param  int
 99:      * @return NPaginator  provides a fluent interface
100:      */
101:     public function setBase($base)
102:     {
103:         $this->base = (int) $base;
104:         return $this;
105:     }
106: 
107: 
108: 
109:     /**
110:      * Returns first page (base) number.
111:      * @return int
112:      */
113:     public function getBase()
114:     {
115:         return $this->base;
116:     }
117: 
118: 
119: 
120:     /**
121:      * Returns zero-based page number.
122:      * @return int
123:      */
124:     protected function getPageIndex()
125:     {
126:         return min(max(0, $this->page - $this->base), 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->getPageIndex() >= $this->getPageCount() - 1;
149:     }
150: 
151: 
152: 
153:     /**
154:      * Returns the total number of pages.
155:      * @return int
156:      */
157:     public function getPageCount()
158:     {
159:         return (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 FALSE as infinity)
191:      * @return NPaginator  provides a fluent interface
192:      */
193:     public function setItemCount($itemCount)
194:     {
195:         $this->itemCount = $itemCount === FALSE ? PHP_INT_MAX : max(0, (int) $itemCount);
196:         return $this;
197:     }
198: 
199: 
200: 
201:     /**
202:      * Returns the total number of items.
203:      * @return int
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
226:      */
227:     public function getCountdownOffset()
228:     {
229:         return max(0, $this->itemCount - ($this->getPageIndex() + 1) * $this->itemsPerPage);
230:     }
231: 
232: 
233: 
234:     /**
235:      * Returns the number of items on current page.
236:      * @return int
237:      */
238:     public function getLength()
239:     {
240:         return min($this->itemsPerPage, $this->itemCount - $this->getPageIndex() * $this->itemsPerPage);
241:     }
242: 
243: }
244: 
Nette Framework 0.9.7 (for PHP 5.2) API documentation generated by ApiGen 2.3.0