Source for file Paginator.php

Documentation is available at Paginator.php

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