PaginatedCollection.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /*
  3. * File: PaginatedCollection.php
  4. * Category: Collection
  5. * Author: M. Goldenbaum
  6. * Created: 16.03.18 03:13
  7. * Updated: -
  8. *
  9. * Description:
  10. * -
  11. */
  12. namespace Webklex\PHPIMAP\Support;
  13. use Illuminate\Pagination\LengthAwarePaginator;
  14. use Illuminate\Support\Collection;
  15. use Illuminate\Pagination\Paginator;
  16. /**
  17. * Class PaginatedCollection
  18. *
  19. * @package Webklex\PHPIMAP\Support
  20. */
  21. class PaginatedCollection extends Collection {
  22. /**
  23. * Number of total entries
  24. *
  25. * @var int $total
  26. */
  27. protected $total;
  28. /**
  29. * Paginate the current collection.
  30. * @param int $per_page
  31. * @param int|null $page
  32. * @param string $page_name
  33. * @param boolean $prepaginated
  34. *
  35. * @return LengthAwarePaginator
  36. */
  37. public function paginate($per_page = 15, $page = null, $page_name = 'page', $prepaginated = false) {
  38. $page = $page ?: Paginator::resolveCurrentPage($page_name);
  39. $total = $this->total ? $this->total : $this->count();
  40. $results = !$prepaginated && $total ? $this->forPage($page, $per_page) : $this->all();
  41. return $this->paginator($results, $total, $per_page, $page, [
  42. 'path' => Paginator::resolveCurrentPath(),
  43. 'pageName' => $page_name,
  44. ]);
  45. }
  46. /**
  47. * Create a new length-aware paginator instance.
  48. * @param array $items
  49. * @param int $total
  50. * @param int $per_page
  51. * @param int|null $current_page
  52. * @param array $options
  53. *
  54. * @return LengthAwarePaginator
  55. */
  56. protected function paginator($items, $total, $per_page, $current_page, array $options) {
  57. return new LengthAwarePaginator($items, $total, $per_page, $current_page, $options);
  58. }
  59. /**
  60. * Get and set the total amount
  61. * @param null $total
  62. *
  63. * @return int|null
  64. */
  65. public function total($total = null) {
  66. if($total === null) {
  67. return $this->total;
  68. }
  69. return $this->total = $total;
  70. }
  71. }