PageRenderTime 470ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Nette/Paginator.php

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