PageRenderTime 28ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

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