Paginator.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. namespace Meramo\Mrmbepages\Paginator;
  3. use TYPO3\CMS\Core\Utility\GeneralUtility;
  4. use TYPO3\CMS\Extbase\Utility\DebuggerUtility;
  5. class Paginator
  6. {
  7. /**
  8. * @var array $data
  9. */
  10. protected $data;
  11. /**
  12. * @var int $totalRecords
  13. */
  14. protected $totalRecords;
  15. /**
  16. * @var int $itemsPerPage
  17. */
  18. protected $itemsPerPage;
  19. /**
  20. * @var int @offSet
  21. */
  22. protected $offSet;
  23. /**
  24. * @var int $totalPages
  25. */
  26. protected $totalPages;
  27. /**
  28. * @var int $currentPage
  29. */
  30. protected $currentPage;
  31. /**
  32. * @var $nextPage
  33. */
  34. protected $nextPage;
  35. /**
  36. * @var $previousPage
  37. */
  38. protected $previousPage;
  39. /**
  40. * @var $paginatedData
  41. */
  42. protected $paginatedData;
  43. public function __construct(){}
  44. public function setData(array $data) {
  45. $this->data = $data;
  46. }
  47. public function getData()
  48. {
  49. return $this->data;
  50. }
  51. public function getTotalRecords(): int
  52. {
  53. $this->totalRecords = count($this->getData());
  54. return $this->totalRecords;
  55. }
  56. public function setItemsPerPage(int $itemsPerPage)
  57. {
  58. $this->itemsPerPage = $itemsPerPage;
  59. }
  60. public function getItemsPerPage(): int
  61. {
  62. return $this->itemsPerPage;
  63. }
  64. public function getTotalPages()
  65. {
  66. $this->totalPages = ceil($this->totalRecords / $this->itemsPerPage);
  67. return $this->totalPages;
  68. }
  69. public function getCurrentPage(): int
  70. {
  71. if($this->currentPage < 1) $this->currentPage = 1;
  72. if($this->currentPage > $this->totalPages) $this->currentPage = $this->totalPages;
  73. return (int) $this->currentPage;
  74. }
  75. public function setCurrentPage($currentPage)
  76. {
  77. $this->currentPage = $currentPage;
  78. }
  79. public function getNextPage():int
  80. {
  81. if($this->getCurrentPage() < $this->getTotalPages()) {
  82. $this->nextPage = (int) ($this->getCurrentPage() + 1);
  83. } else $this->nextPage = $this->getTotalPages();
  84. return $this->nextPage;
  85. }
  86. public function getPreviousPage()
  87. {
  88. if($this->currentPage > 1) {
  89. $this->previousPage = (int) $this->currentPage - 1;
  90. }
  91. return $this->previousPage;
  92. }
  93. public function getOffSet(): int
  94. {
  95. return $this->offSet = ((int) $this->currentPage -1) * $this->pageSize;
  96. }
  97. public function getPaginatedData()
  98. {
  99. $offset = ((int) $this->getCurrentPage() -1) * $this->itemsPerPage;
  100. $this->paginatedData = array_slice($this->data, $offset, $this->itemsPerPage);
  101. return $this->paginatedData;
  102. }
  103. }