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