/vendor/laravel/framework/src/Illuminate/Pagination/Paginator.php

https://bitbucket.org/larryg/powerhut · PHP · 414 lines · 154 code · 51 blank · 209 comment · 3 complexity · 9a2956f640247838fee0c35dffe102b5 MD5 · raw file

  1. <?php namespace Illuminate\Pagination;
  2. use Countable;
  3. use ArrayAccess;
  4. use ArrayIterator;
  5. use IteratorAggregate;
  6. use Illuminate\Support\Collection;
  7. class Paginator implements ArrayAccess, Countable, IteratorAggregate {
  8. /**
  9. * The pagination environment.
  10. *
  11. * @var \Illuminate\Pagination\Environment
  12. */
  13. protected $env;
  14. /**
  15. * The items being paginated.
  16. *
  17. * @var array
  18. */
  19. protected $items;
  20. /**
  21. * The total number of items.
  22. *
  23. * @var int
  24. */
  25. protected $total;
  26. /**
  27. * The amount of items to show per page.
  28. *
  29. * @var int
  30. */
  31. protected $perPage;
  32. /**
  33. * Get the current page for the request.
  34. *
  35. * @var int
  36. */
  37. protected $currentPage;
  38. /**
  39. * Get the last available page number.
  40. *
  41. * @return int
  42. */
  43. protected $lastPage;
  44. /**
  45. * The number of the first item in this range.
  46. *
  47. * @var int
  48. */
  49. protected $from;
  50. /**
  51. * The number of the last item in this range.
  52. *
  53. * @var int
  54. */
  55. protected $to;
  56. /**
  57. * All of the additional query string values.
  58. *
  59. * @var array
  60. */
  61. protected $query = array();
  62. /**
  63. * Create a new Paginator instance.
  64. *
  65. * @param \Illuminate\Pagination\Environment $env
  66. * @param array $items
  67. * @param int $total
  68. * @param int $perPage
  69. * @return void
  70. */
  71. public function __construct(Environment $env, array $items, $total, $perPage)
  72. {
  73. $this->env = $env;
  74. $this->total = $total;
  75. $this->items = $items;
  76. $this->perPage = $perPage;
  77. }
  78. /**
  79. * Setup the pagination context (current and last page).
  80. *
  81. * @return \Illuminate\Pagination\Paginator
  82. */
  83. public function setupPaginationContext()
  84. {
  85. $this->calculateCurrentAndLastPages();
  86. $this->calculateItemRanges();
  87. return $this;
  88. }
  89. /**
  90. * Calculate the current and last pages for this instance.
  91. *
  92. * @return void
  93. */
  94. protected function calculateCurrentAndLastPages()
  95. {
  96. $this->lastPage = ceil($this->total / $this->perPage);
  97. $this->currentPage = $this->calculateCurrentPage($this->lastPage);
  98. }
  99. /**
  100. * Calculate the first and last item number for this instance.
  101. *
  102. * @return void
  103. */
  104. protected function calculateItemRanges()
  105. {
  106. $this->from = $this->total ? ($this->currentPage - 1) * $this->perPage + 1 : 0;
  107. $this->to = min($this->total, $this->currentPage * $this->perPage);
  108. }
  109. /**
  110. * Get the current page for the request.
  111. *
  112. * @param int $lastPage
  113. * @return int
  114. */
  115. protected function calculateCurrentPage($lastPage)
  116. {
  117. $page = $this->env->getCurrentPage();
  118. // The page number will get validated and adjusted if it either less than one
  119. // or greater than the last page available based on the count of the given
  120. // items array. If it's greater than the last, we'll give back the last.
  121. if (is_numeric($page) and $page > $lastPage)
  122. {
  123. return $lastPage > 0 ? $lastPage : 1;
  124. }
  125. return $this->isValidPageNumber($page) ? (int) $page : 1;
  126. }
  127. /**
  128. * Determine if the given value is a valid page number.
  129. *
  130. * @param int $page
  131. * @return bool
  132. */
  133. protected function isValidPageNumber($page)
  134. {
  135. return $page >= 1 and filter_var($page, FILTER_VALIDATE_INT) !== false;
  136. }
  137. /**
  138. * Get the pagination links view.
  139. *
  140. * @return \Illuminate\View\View
  141. */
  142. public function links()
  143. {
  144. return $this->env->getPaginationView($this);
  145. }
  146. /**
  147. * Get a URL for a given page number.
  148. *
  149. * @param int $page
  150. * @return string
  151. */
  152. public function getUrl($page)
  153. {
  154. $parameters = array(
  155. $this->env->getPageName() => $page,
  156. );
  157. // If we have any extra query string key / value pairs that need to be added
  158. // onto the URL, we will put them in query string form and then attach it
  159. // to the URL. This allows for extra information like sortings storage.
  160. if (count($this->query) > 0)
  161. {
  162. $parameters = array_merge($parameters, $this->query);
  163. }
  164. return $this->env->getCurrentUrl().'?'.http_build_query($parameters, null, '&');
  165. }
  166. /**
  167. * Add a query string value to the paginator.
  168. *
  169. * @param string $key
  170. * @param string $value
  171. * @return \Illuminate\Pagination\Paginator
  172. */
  173. public function appends($key, $value = null)
  174. {
  175. if (is_array($key)) return $this->appendArray($key);
  176. return $this->addQuery($key, $value);
  177. }
  178. /**
  179. * Add an array of query string values.
  180. *
  181. * @param array $keys
  182. * @return \Illuminate\Pagination\Paginator
  183. */
  184. protected function appendArray(array $keys)
  185. {
  186. foreach ($keys as $key => $value)
  187. {
  188. $this->addQuery($key, $value);
  189. }
  190. return $this;
  191. }
  192. /**
  193. * Add a query string value to the paginator.
  194. *
  195. * @param string $key
  196. * @param string $value
  197. * @return \Illuminate\Pagination\Paginator
  198. */
  199. public function addQuery($key, $value)
  200. {
  201. $this->query[$key] = $value;
  202. return $this;
  203. }
  204. /**
  205. * Get the current page for the request.
  206. *
  207. * @return int
  208. */
  209. public function getCurrentPage()
  210. {
  211. return $this->currentPage;
  212. }
  213. /**
  214. * Get the last page that should be available.
  215. *
  216. * @return int
  217. */
  218. public function getLastPage()
  219. {
  220. return $this->lastPage;
  221. }
  222. /**
  223. * Get the number of the first item on the paginator.
  224. *
  225. * @return int
  226. */
  227. public function getFrom()
  228. {
  229. return $this->from;
  230. }
  231. /**
  232. * Get the number of the last item on the paginator.
  233. *
  234. * @return int
  235. */
  236. public function getTo()
  237. {
  238. return $this->to;
  239. }
  240. /**
  241. * Get the number of items to be displayed per page.
  242. *
  243. * @return int
  244. */
  245. public function getPerPage()
  246. {
  247. return $this->perPage;
  248. }
  249. /**
  250. * Get a collection instance containing the items.
  251. *
  252. * @return \Illuminate\Support\Collection
  253. */
  254. public function getCollection()
  255. {
  256. return new Collection($this->items);
  257. }
  258. /**
  259. * Get the items being paginated.
  260. *
  261. * @return array
  262. */
  263. public function getItems()
  264. {
  265. return $this->items;
  266. }
  267. /**
  268. * Get the total number of items in the collection.
  269. *
  270. * @return int
  271. */
  272. public function getTotal()
  273. {
  274. return $this->total;
  275. }
  276. /**
  277. * Set the base URL in use by the paginator.
  278. *
  279. * @param string $baseUrl
  280. * @return void
  281. */
  282. public function setBaseUrl($baseUrl)
  283. {
  284. $this->env->setBaseUrl($baseUrl);
  285. }
  286. /**
  287. * Get the pagination environment.
  288. *
  289. * @return \Illuminate\Pagination\Environment
  290. */
  291. public function getEnvironment()
  292. {
  293. return $this->env;
  294. }
  295. /**
  296. * Get an iterator for the items.
  297. *
  298. * @return ArrayIterator
  299. */
  300. public function getIterator()
  301. {
  302. return new ArrayIterator($this->items);
  303. }
  304. /**
  305. * Determine if the list of items is empty or not.
  306. *
  307. * @return bool
  308. */
  309. public function isEmpty()
  310. {
  311. return empty($this->items);
  312. }
  313. /**
  314. * Get the number of items for the current page.
  315. *
  316. * @return int
  317. */
  318. public function count()
  319. {
  320. return count($this->items);
  321. }
  322. /**
  323. * Determine if the given item exists.
  324. *
  325. * @param mixed $key
  326. * @return bool
  327. */
  328. public function offsetExists($key)
  329. {
  330. return array_key_exists($key, $this->items);
  331. }
  332. /**
  333. * Get the item at the given offset.
  334. *
  335. * @param mixed $key
  336. * @return mixed
  337. */
  338. public function offsetGet($key)
  339. {
  340. return $this->items[$key];
  341. }
  342. /**
  343. * Set the item at the given offset.
  344. *
  345. * @param mixed $key
  346. * @param mixed $value
  347. * @return void
  348. */
  349. public function offsetSet($key, $value)
  350. {
  351. $this->items[$key] = $value;
  352. }
  353. /**
  354. * Unset the item at the given key.
  355. *
  356. * @param mixed $key
  357. * @return void
  358. */
  359. public function offsetUnset($key)
  360. {
  361. unset($this->items[$key]);
  362. }
  363. }