AbstractPaginator.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. <?php
  2. namespace Illuminate\Pagination;
  3. use Closure;
  4. use Illuminate\Contracts\Support\Htmlable;
  5. use Illuminate\Support\Arr;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Str;
  8. use Illuminate\Support\Traits\ForwardsCalls;
  9. use Illuminate\Support\Traits\Tappable;
  10. /**
  11. * @mixin \Illuminate\Support\Collection
  12. */
  13. abstract class AbstractPaginator implements Htmlable
  14. {
  15. use ForwardsCalls, Tappable;
  16. /**
  17. * All of the items being paginated.
  18. *
  19. * @var \Illuminate\Support\Collection
  20. */
  21. protected $items;
  22. /**
  23. * The number of items to be shown per page.
  24. *
  25. * @var int
  26. */
  27. protected $perPage;
  28. /**
  29. * The current page being "viewed".
  30. *
  31. * @var int
  32. */
  33. protected $currentPage;
  34. /**
  35. * The base path to assign to all URLs.
  36. *
  37. * @var string
  38. */
  39. protected $path = '/';
  40. /**
  41. * The query parameters to add to all URLs.
  42. *
  43. * @var array
  44. */
  45. protected $query = [];
  46. /**
  47. * The URL fragment to add to all URLs.
  48. *
  49. * @var string|null
  50. */
  51. protected $fragment;
  52. /**
  53. * The query string variable used to store the page.
  54. *
  55. * @var string
  56. */
  57. protected $pageName = 'page';
  58. /**
  59. * The number of links to display on each side of current page link.
  60. *
  61. * @var int
  62. */
  63. public $onEachSide = 3;
  64. /**
  65. * The paginator options.
  66. *
  67. * @var array
  68. */
  69. protected $options;
  70. /**
  71. * The current path resolver callback.
  72. *
  73. * @var \Closure
  74. */
  75. protected static $currentPathResolver;
  76. /**
  77. * The current page resolver callback.
  78. *
  79. * @var \Closure
  80. */
  81. protected static $currentPageResolver;
  82. /**
  83. * The query string resolver callback.
  84. *
  85. * @var \Closure
  86. */
  87. protected static $queryStringResolver;
  88. /**
  89. * The view factory resolver callback.
  90. *
  91. * @var \Closure
  92. */
  93. protected static $viewFactoryResolver;
  94. /**
  95. * The default pagination view.
  96. *
  97. * @var string
  98. */
  99. public static $defaultView = 'pagination::tailwind';
  100. /**
  101. * The default "simple" pagination view.
  102. *
  103. * @var string
  104. */
  105. public static $defaultSimpleView = 'pagination::simple-tailwind';
  106. /**
  107. * Determine if the given value is a valid page number.
  108. *
  109. * @param int $page
  110. * @return bool
  111. */
  112. protected function isValidPageNumber($page)
  113. {
  114. return $page >= 1 && filter_var($page, FILTER_VALIDATE_INT) !== false;
  115. }
  116. /**
  117. * Get the URL for the previous page.
  118. *
  119. * @return string|null
  120. */
  121. public function previousPageUrl()
  122. {
  123. if ($this->currentPage() > 1) {
  124. return $this->url($this->currentPage() - 1);
  125. }
  126. }
  127. /**
  128. * Create a range of pagination URLs.
  129. *
  130. * @param int $start
  131. * @param int $end
  132. * @return array
  133. */
  134. public function getUrlRange($start, $end)
  135. {
  136. return collect(range($start, $end))->mapWithKeys(function ($page) {
  137. return [$page => $this->url($page)];
  138. })->all();
  139. }
  140. /**
  141. * Get the URL for a given page number.
  142. *
  143. * @param int $page
  144. * @return string
  145. */
  146. public function url($page)
  147. {
  148. if ($page <= 0) {
  149. $page = 1;
  150. }
  151. // If we have any extra query string key / value pairs that need to be added
  152. // onto the URL, we will put them in query string form and then attach it
  153. // to the URL. This allows for extra information like sortings storage.
  154. $parameters = [$this->pageName => $page];
  155. if (count($this->query) > 0) {
  156. $parameters = array_merge($this->query, $parameters);
  157. }
  158. return $this->path()
  159. .(Str::contains($this->path(), '?') ? '&' : '?')
  160. .Arr::query($parameters)
  161. .$this->buildFragment();
  162. }
  163. /**
  164. * Get / set the URL fragment to be appended to URLs.
  165. *
  166. * @param string|null $fragment
  167. * @return $this|string|null
  168. */
  169. public function fragment($fragment = null)
  170. {
  171. if (is_null($fragment)) {
  172. return $this->fragment;
  173. }
  174. $this->fragment = $fragment;
  175. return $this;
  176. }
  177. /**
  178. * Add a set of query string values to the paginator.
  179. *
  180. * @param array|string|null $key
  181. * @param string|null $value
  182. * @return $this
  183. */
  184. public function appends($key, $value = null)
  185. {
  186. if (is_null($key)) {
  187. return $this;
  188. }
  189. if (is_array($key)) {
  190. return $this->appendArray($key);
  191. }
  192. return $this->addQuery($key, $value);
  193. }
  194. /**
  195. * Add an array of query string values.
  196. *
  197. * @param array $keys
  198. * @return $this
  199. */
  200. protected function appendArray(array $keys)
  201. {
  202. foreach ($keys as $key => $value) {
  203. $this->addQuery($key, $value);
  204. }
  205. return $this;
  206. }
  207. /**
  208. * Add all current query string values to the paginator.
  209. *
  210. * @return $this
  211. */
  212. public function withQueryString()
  213. {
  214. if (isset(static::$queryStringResolver)) {
  215. return $this->appends(call_user_func(static::$queryStringResolver));
  216. }
  217. return $this;
  218. }
  219. /**
  220. * Add a query string value to the paginator.
  221. *
  222. * @param string $key
  223. * @param string $value
  224. * @return $this
  225. */
  226. protected function addQuery($key, $value)
  227. {
  228. if ($key !== $this->pageName) {
  229. $this->query[$key] = $value;
  230. }
  231. return $this;
  232. }
  233. /**
  234. * Build the full fragment portion of a URL.
  235. *
  236. * @return string
  237. */
  238. protected function buildFragment()
  239. {
  240. return $this->fragment ? '#'.$this->fragment : '';
  241. }
  242. /**
  243. * Load a set of relationships onto the mixed relationship collection.
  244. *
  245. * @param string $relation
  246. * @param array $relations
  247. * @return $this
  248. */
  249. public function loadMorph($relation, $relations)
  250. {
  251. $this->getCollection()->loadMorph($relation, $relations);
  252. return $this;
  253. }
  254. /**
  255. * Load a set of relationship counts onto the mixed relationship collection.
  256. *
  257. * @param string $relation
  258. * @param array $relations
  259. * @return $this
  260. */
  261. public function loadMorphCount($relation, $relations)
  262. {
  263. $this->getCollection()->loadMorphCount($relation, $relations);
  264. return $this;
  265. }
  266. /**
  267. * Get the slice of items being paginated.
  268. *
  269. * @return array
  270. */
  271. public function items()
  272. {
  273. return $this->items->all();
  274. }
  275. /**
  276. * Get the number of the first item in the slice.
  277. *
  278. * @return int
  279. */
  280. public function firstItem()
  281. {
  282. return count($this->items) > 0 ? ($this->currentPage - 1) * $this->perPage + 1 : null;
  283. }
  284. /**
  285. * Get the number of the last item in the slice.
  286. *
  287. * @return int
  288. */
  289. public function lastItem()
  290. {
  291. return count($this->items) > 0 ? $this->firstItem() + $this->count() - 1 : null;
  292. }
  293. /**
  294. * Transform each item in the slice of items using a callback.
  295. *
  296. * @param callable $callback
  297. * @return $this
  298. */
  299. public function through(callable $callback)
  300. {
  301. $this->items->transform($callback);
  302. return $this;
  303. }
  304. /**
  305. * Get the number of items shown per page.
  306. *
  307. * @return int
  308. */
  309. public function perPage()
  310. {
  311. return $this->perPage;
  312. }
  313. /**
  314. * Determine if there are enough items to split into multiple pages.
  315. *
  316. * @return bool
  317. */
  318. public function hasPages()
  319. {
  320. return $this->currentPage() != 1 || $this->hasMorePages();
  321. }
  322. /**
  323. * Determine if the paginator is on the first page.
  324. *
  325. * @return bool
  326. */
  327. public function onFirstPage()
  328. {
  329. return $this->currentPage() <= 1;
  330. }
  331. /**
  332. * Determine if the paginator is on the last page.
  333. *
  334. * @return bool
  335. */
  336. public function onLastPage()
  337. {
  338. return ! $this->hasMorePages();
  339. }
  340. /**
  341. * Get the current page.
  342. *
  343. * @return int
  344. */
  345. public function currentPage()
  346. {
  347. return $this->currentPage;
  348. }
  349. /**
  350. * Get the query string variable used to store the page.
  351. *
  352. * @return string
  353. */
  354. public function getPageName()
  355. {
  356. return $this->pageName;
  357. }
  358. /**
  359. * Set the query string variable used to store the page.
  360. *
  361. * @param string $name
  362. * @return $this
  363. */
  364. public function setPageName($name)
  365. {
  366. $this->pageName = $name;
  367. return $this;
  368. }
  369. /**
  370. * Set the base path to assign to all URLs.
  371. *
  372. * @param string $path
  373. * @return $this
  374. */
  375. public function withPath($path)
  376. {
  377. return $this->setPath($path);
  378. }
  379. /**
  380. * Set the base path to assign to all URLs.
  381. *
  382. * @param string $path
  383. * @return $this
  384. */
  385. public function setPath($path)
  386. {
  387. $this->path = $path;
  388. return $this;
  389. }
  390. /**
  391. * Set the number of links to display on each side of current page link.
  392. *
  393. * @param int $count
  394. * @return $this
  395. */
  396. public function onEachSide($count)
  397. {
  398. $this->onEachSide = $count;
  399. return $this;
  400. }
  401. /**
  402. * Get the base path for paginator generated URLs.
  403. *
  404. * @return string|null
  405. */
  406. public function path()
  407. {
  408. return $this->path;
  409. }
  410. /**
  411. * Resolve the current request path or return the default value.
  412. *
  413. * @param string $default
  414. * @return string
  415. */
  416. public static function resolveCurrentPath($default = '/')
  417. {
  418. if (isset(static::$currentPathResolver)) {
  419. return call_user_func(static::$currentPathResolver);
  420. }
  421. return $default;
  422. }
  423. /**
  424. * Set the current request path resolver callback.
  425. *
  426. * @param \Closure $resolver
  427. * @return void
  428. */
  429. public static function currentPathResolver(Closure $resolver)
  430. {
  431. static::$currentPathResolver = $resolver;
  432. }
  433. /**
  434. * Resolve the current page or return the default value.
  435. *
  436. * @param string $pageName
  437. * @param int $default
  438. * @return int
  439. */
  440. public static function resolveCurrentPage($pageName = 'page', $default = 1)
  441. {
  442. if (isset(static::$currentPageResolver)) {
  443. return (int) call_user_func(static::$currentPageResolver, $pageName);
  444. }
  445. return $default;
  446. }
  447. /**
  448. * Set the current page resolver callback.
  449. *
  450. * @param \Closure $resolver
  451. * @return void
  452. */
  453. public static function currentPageResolver(Closure $resolver)
  454. {
  455. static::$currentPageResolver = $resolver;
  456. }
  457. /**
  458. * Resolve the query string or return the default value.
  459. *
  460. * @param string|array|null $default
  461. * @return string
  462. */
  463. public static function resolveQueryString($default = null)
  464. {
  465. if (isset(static::$queryStringResolver)) {
  466. return (static::$queryStringResolver)();
  467. }
  468. return $default;
  469. }
  470. /**
  471. * Set with query string resolver callback.
  472. *
  473. * @param \Closure $resolver
  474. * @return void
  475. */
  476. public static function queryStringResolver(Closure $resolver)
  477. {
  478. static::$queryStringResolver = $resolver;
  479. }
  480. /**
  481. * Get an instance of the view factory from the resolver.
  482. *
  483. * @return \Illuminate\Contracts\View\Factory
  484. */
  485. public static function viewFactory()
  486. {
  487. return call_user_func(static::$viewFactoryResolver);
  488. }
  489. /**
  490. * Set the view factory resolver callback.
  491. *
  492. * @param \Closure $resolver
  493. * @return void
  494. */
  495. public static function viewFactoryResolver(Closure $resolver)
  496. {
  497. static::$viewFactoryResolver = $resolver;
  498. }
  499. /**
  500. * Set the default pagination view.
  501. *
  502. * @param string $view
  503. * @return void
  504. */
  505. public static function defaultView($view)
  506. {
  507. static::$defaultView = $view;
  508. }
  509. /**
  510. * Set the default "simple" pagination view.
  511. *
  512. * @param string $view
  513. * @return void
  514. */
  515. public static function defaultSimpleView($view)
  516. {
  517. static::$defaultSimpleView = $view;
  518. }
  519. /**
  520. * Indicate that Tailwind styling should be used for generated links.
  521. *
  522. * @return void
  523. */
  524. public static function useTailwind()
  525. {
  526. static::defaultView('pagination::tailwind');
  527. static::defaultSimpleView('pagination::simple-tailwind');
  528. }
  529. /**
  530. * Indicate that Bootstrap 4 styling should be used for generated links.
  531. *
  532. * @return void
  533. */
  534. public static function useBootstrap()
  535. {
  536. static::defaultView('pagination::bootstrap-4');
  537. static::defaultSimpleView('pagination::simple-bootstrap-4');
  538. }
  539. /**
  540. * Indicate that Bootstrap 3 styling should be used for generated links.
  541. *
  542. * @return void
  543. */
  544. public static function useBootstrapThree()
  545. {
  546. static::defaultView('pagination::default');
  547. static::defaultSimpleView('pagination::simple-default');
  548. }
  549. /**
  550. * Get an iterator for the items.
  551. *
  552. * @return \ArrayIterator
  553. */
  554. #[\ReturnTypeWillChange]
  555. public function getIterator()
  556. {
  557. return $this->items->getIterator();
  558. }
  559. /**
  560. * Determine if the list of items is empty.
  561. *
  562. * @return bool
  563. */
  564. public function isEmpty()
  565. {
  566. return $this->items->isEmpty();
  567. }
  568. /**
  569. * Determine if the list of items is not empty.
  570. *
  571. * @return bool
  572. */
  573. public function isNotEmpty()
  574. {
  575. return $this->items->isNotEmpty();
  576. }
  577. /**
  578. * Get the number of items for the current page.
  579. *
  580. * @return int
  581. */
  582. #[\ReturnTypeWillChange]
  583. public function count()
  584. {
  585. return $this->items->count();
  586. }
  587. /**
  588. * Get the paginator's underlying collection.
  589. *
  590. * @return \Illuminate\Support\Collection
  591. */
  592. public function getCollection()
  593. {
  594. return $this->items;
  595. }
  596. /**
  597. * Set the paginator's underlying collection.
  598. *
  599. * @param \Illuminate\Support\Collection $collection
  600. * @return $this
  601. */
  602. public function setCollection(Collection $collection)
  603. {
  604. $this->items = $collection;
  605. return $this;
  606. }
  607. /**
  608. * Get the paginator options.
  609. *
  610. * @return array
  611. */
  612. public function getOptions()
  613. {
  614. return $this->options;
  615. }
  616. /**
  617. * Determine if the given item exists.
  618. *
  619. * @param mixed $key
  620. * @return bool
  621. */
  622. #[\ReturnTypeWillChange]
  623. public function offsetExists($key)
  624. {
  625. return $this->items->has($key);
  626. }
  627. /**
  628. * Get the item at the given offset.
  629. *
  630. * @param mixed $key
  631. * @return mixed
  632. */
  633. #[\ReturnTypeWillChange]
  634. public function offsetGet($key)
  635. {
  636. return $this->items->get($key);
  637. }
  638. /**
  639. * Set the item at the given offset.
  640. *
  641. * @param mixed $key
  642. * @param mixed $value
  643. * @return void
  644. */
  645. #[\ReturnTypeWillChange]
  646. public function offsetSet($key, $value)
  647. {
  648. $this->items->put($key, $value);
  649. }
  650. /**
  651. * Unset the item at the given key.
  652. *
  653. * @param mixed $key
  654. * @return void
  655. */
  656. #[\ReturnTypeWillChange]
  657. public function offsetUnset($key)
  658. {
  659. $this->items->forget($key);
  660. }
  661. /**
  662. * Render the contents of the paginator to HTML.
  663. *
  664. * @return string
  665. */
  666. public function toHtml()
  667. {
  668. return (string) $this->render();
  669. }
  670. /**
  671. * Make dynamic calls into the collection.
  672. *
  673. * @param string $method
  674. * @param array $parameters
  675. * @return mixed
  676. */
  677. public function __call($method, $parameters)
  678. {
  679. return $this->forwardCallTo($this->getCollection(), $method, $parameters);
  680. }
  681. /**
  682. * Render the contents of the paginator when casting to a string.
  683. *
  684. * @return string
  685. */
  686. public function __toString()
  687. {
  688. return (string) $this->render();
  689. }
  690. }