/Front_End/vendor/laravel/framework/src/Illuminate/Pagination/AbstractPaginator.php

https://gitlab.com/Sigpot/AirSpot · PHP · 522 lines · 204 code · 64 blank · 254 comment · 14 complexity · 3bcd5cec2f5fdef8ca4fa5530d638bdf MD5 · raw file

  1. <?php
  2. namespace Illuminate\Pagination;
  3. use Closure;
  4. use ArrayIterator;
  5. use Illuminate\Support\Str;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Contracts\Support\Htmlable;
  8. abstract class AbstractPaginator implements Htmlable
  9. {
  10. /**
  11. * All of the items being paginated.
  12. *
  13. * @var \Illuminate\Support\Collection
  14. */
  15. protected $items;
  16. /**
  17. * The number of items to be shown per page.
  18. *
  19. * @var int
  20. */
  21. protected $perPage;
  22. /**
  23. * The current page being "viewed".
  24. *
  25. * @var int
  26. */
  27. protected $currentPage;
  28. /**
  29. * The base path to assign to all URLs.
  30. *
  31. * @var string
  32. */
  33. protected $path = '/';
  34. /**
  35. * The query parameters to add to all URLs.
  36. *
  37. * @var array
  38. */
  39. protected $query = [];
  40. /**
  41. * The URL fragment to add to all URLs.
  42. *
  43. * @var string|null
  44. */
  45. protected $fragment = null;
  46. /**
  47. * The query string variable used to store the page.
  48. *
  49. * @var string
  50. */
  51. protected $pageName = 'page';
  52. /**
  53. * The current page resolver callback.
  54. *
  55. * @var \Closure
  56. */
  57. protected static $currentPathResolver;
  58. /**
  59. * The current page resolver callback.
  60. *
  61. * @var \Closure
  62. */
  63. protected static $currentPageResolver;
  64. /**
  65. * The default presenter resolver.
  66. *
  67. * @var \Closure
  68. */
  69. protected static $presenterResolver;
  70. /**
  71. * Determine if the given value is a valid page number.
  72. *
  73. * @param int $page
  74. * @return bool
  75. */
  76. protected function isValidPageNumber($page)
  77. {
  78. return $page >= 1 && filter_var($page, FILTER_VALIDATE_INT) !== false;
  79. }
  80. /**
  81. * Create a range of pagination URLs.
  82. *
  83. * @param int $start
  84. * @param int $end
  85. * @return string
  86. */
  87. public function getUrlRange($start, $end)
  88. {
  89. $urls = [];
  90. for ($page = $start; $page <= $end; $page++) {
  91. $urls[$page] = $this->url($page);
  92. }
  93. return $urls;
  94. }
  95. /**
  96. * Get a URL for a given page number.
  97. *
  98. * @param int $page
  99. * @return string
  100. */
  101. public function url($page)
  102. {
  103. if ($page <= 0) {
  104. $page = 1;
  105. }
  106. // If we have any extra query string key / value pairs that need to be added
  107. // onto the URL, we will put them in query string form and then attach it
  108. // to the URL. This allows for extra information like sortings storage.
  109. $parameters = [$this->pageName => $page];
  110. if (count($this->query) > 0) {
  111. $parameters = array_merge($this->query, $parameters);
  112. }
  113. return $this->path
  114. .(Str::contains($this->path, '?') ? '&' : '?')
  115. .http_build_query($parameters, '', '&')
  116. .$this->buildFragment();
  117. }
  118. /**
  119. * Get the URL for the previous page.
  120. *
  121. * @return string|null
  122. */
  123. public function previousPageUrl()
  124. {
  125. if ($this->currentPage() > 1) {
  126. return $this->url($this->currentPage() - 1);
  127. }
  128. }
  129. /**
  130. * Get / set the URL fragment to be appended to URLs.
  131. *
  132. * @param string|null $fragment
  133. * @return $this|string|null
  134. */
  135. public function fragment($fragment = null)
  136. {
  137. if (is_null($fragment)) {
  138. return $this->fragment;
  139. }
  140. $this->fragment = $fragment;
  141. return $this;
  142. }
  143. /**
  144. * Add a set of query string values to the paginator.
  145. *
  146. * @param array|string $key
  147. * @param string|null $value
  148. * @return $this
  149. */
  150. public function appends($key, $value = null)
  151. {
  152. if (is_array($key)) {
  153. return $this->appendArray($key);
  154. }
  155. return $this->addQuery($key, $value);
  156. }
  157. /**
  158. * Add an array of query string values.
  159. *
  160. * @param array $keys
  161. * @return $this
  162. */
  163. protected function appendArray(array $keys)
  164. {
  165. foreach ($keys as $key => $value) {
  166. $this->addQuery($key, $value);
  167. }
  168. return $this;
  169. }
  170. /**
  171. * Add a query string value to the paginator.
  172. *
  173. * @param string $key
  174. * @param string $value
  175. * @return $this
  176. */
  177. public function addQuery($key, $value)
  178. {
  179. if ($key !== $this->pageName) {
  180. $this->query[$key] = $value;
  181. }
  182. return $this;
  183. }
  184. /**
  185. * Build the full fragment portion of a URL.
  186. *
  187. * @return string
  188. */
  189. protected function buildFragment()
  190. {
  191. return $this->fragment ? '#'.$this->fragment : '';
  192. }
  193. /**
  194. * Get the slice of items being paginated.
  195. *
  196. * @return array
  197. */
  198. public function items()
  199. {
  200. return $this->items->all();
  201. }
  202. /**
  203. * Get the number of the first item in the slice.
  204. *
  205. * @return int
  206. */
  207. public function firstItem()
  208. {
  209. if (count($this->items) === 0) {
  210. return;
  211. }
  212. return ($this->currentPage - 1) * $this->perPage + 1;
  213. }
  214. /**
  215. * Get the number of the last item in the slice.
  216. *
  217. * @return int
  218. */
  219. public function lastItem()
  220. {
  221. if (count($this->items) === 0) {
  222. return;
  223. }
  224. return $this->firstItem() + $this->count() - 1;
  225. }
  226. /**
  227. * Get the number of items shown per page.
  228. *
  229. * @return int
  230. */
  231. public function perPage()
  232. {
  233. return $this->perPage;
  234. }
  235. /**
  236. * Get the current page.
  237. *
  238. * @return int
  239. */
  240. public function currentPage()
  241. {
  242. return $this->currentPage;
  243. }
  244. /**
  245. * Determine if there are enough items to split into multiple pages.
  246. *
  247. * @return bool
  248. */
  249. public function hasPages()
  250. {
  251. return ! ($this->currentPage() == 1 && ! $this->hasMorePages());
  252. }
  253. /**
  254. * Resolve the current request path or return the default value.
  255. *
  256. * @param string $default
  257. * @return string
  258. */
  259. public static function resolveCurrentPath($default = '/')
  260. {
  261. if (isset(static::$currentPathResolver)) {
  262. return call_user_func(static::$currentPathResolver);
  263. }
  264. return $default;
  265. }
  266. /**
  267. * Set the current request path resolver callback.
  268. *
  269. * @param \Closure $resolver
  270. * @return void
  271. */
  272. public static function currentPathResolver(Closure $resolver)
  273. {
  274. static::$currentPathResolver = $resolver;
  275. }
  276. /**
  277. * Resolve the current page or return the default value.
  278. *
  279. * @param string $pageName
  280. * @param int $default
  281. * @return int
  282. */
  283. public static function resolveCurrentPage($pageName = 'page', $default = 1)
  284. {
  285. if (isset(static::$currentPageResolver)) {
  286. return call_user_func(static::$currentPageResolver, $pageName);
  287. }
  288. return $default;
  289. }
  290. /**
  291. * Set the current page resolver callback.
  292. *
  293. * @param \Closure $resolver
  294. * @return void
  295. */
  296. public static function currentPageResolver(Closure $resolver)
  297. {
  298. static::$currentPageResolver = $resolver;
  299. }
  300. /**
  301. * Set the default Presenter resolver.
  302. *
  303. * @param \Closure $resolver
  304. * @return void
  305. */
  306. public static function presenter(Closure $resolver)
  307. {
  308. static::$presenterResolver = $resolver;
  309. }
  310. /**
  311. * Get the query string variable used to store the page.
  312. *
  313. * @return string
  314. */
  315. public function getPageName()
  316. {
  317. return $this->pageName;
  318. }
  319. /**
  320. * Set the query string variable used to store the page.
  321. *
  322. * @param string $name
  323. * @return $this
  324. */
  325. public function setPageName($name)
  326. {
  327. $this->pageName = $name;
  328. return $this;
  329. }
  330. /**
  331. * Set the base path to assign to all URLs.
  332. *
  333. * @param string $path
  334. * @return $this
  335. */
  336. public function setPath($path)
  337. {
  338. $this->path = $path;
  339. return $this;
  340. }
  341. /**
  342. * Get an iterator for the items.
  343. *
  344. * @return \ArrayIterator
  345. */
  346. public function getIterator()
  347. {
  348. return new ArrayIterator($this->items->all());
  349. }
  350. /**
  351. * Determine if the list of items is empty or not.
  352. *
  353. * @return bool
  354. */
  355. public function isEmpty()
  356. {
  357. return $this->items->isEmpty();
  358. }
  359. /**
  360. * Get the number of items for the current page.
  361. *
  362. * @return int
  363. */
  364. public function count()
  365. {
  366. return $this->items->count();
  367. }
  368. /**
  369. * Get the paginator's underlying collection.
  370. *
  371. * @return \Illuminate\Support\Collection
  372. */
  373. public function getCollection()
  374. {
  375. return $this->items;
  376. }
  377. /**
  378. * Set the paginator's underlying collection.
  379. *
  380. * @param \Illuminate\Support\Collection $collection
  381. * @return $this
  382. */
  383. public function setCollection(Collection $collection)
  384. {
  385. $this->items = $collection;
  386. return $this;
  387. }
  388. /**
  389. * Determine if the given item exists.
  390. *
  391. * @param mixed $key
  392. * @return bool
  393. */
  394. public function offsetExists($key)
  395. {
  396. return $this->items->has($key);
  397. }
  398. /**
  399. * Get the item at the given offset.
  400. *
  401. * @param mixed $key
  402. * @return mixed
  403. */
  404. public function offsetGet($key)
  405. {
  406. return $this->items->get($key);
  407. }
  408. /**
  409. * Set the item at the given offset.
  410. *
  411. * @param mixed $key
  412. * @param mixed $value
  413. * @return void
  414. */
  415. public function offsetSet($key, $value)
  416. {
  417. $this->items->put($key, $value);
  418. }
  419. /**
  420. * Unset the item at the given key.
  421. *
  422. * @param mixed $key
  423. * @return void
  424. */
  425. public function offsetUnset($key)
  426. {
  427. $this->items->forget($key);
  428. }
  429. /**
  430. * Render the contents of the paginator to HTML.
  431. *
  432. * @return string
  433. */
  434. public function toHtml()
  435. {
  436. return (string) $this->render();
  437. }
  438. /**
  439. * Make dynamic calls into the collection.
  440. *
  441. * @param string $method
  442. * @param array $parameters
  443. * @return mixed
  444. */
  445. public function __call($method, $parameters)
  446. {
  447. return call_user_func_array([$this->getCollection(), $method], $parameters);
  448. }
  449. /**
  450. * Render the contents of the paginator when casting to string.
  451. *
  452. * @return string
  453. */
  454. public function __toString()
  455. {
  456. return (string) $this->render();
  457. }
  458. }