1. <?php
  2. class MessageQueuePager {
  3. private $queueData;
  4. private $itemsPerPage;
  5. private $currentPage;
  6. public function __construct(array $queueData, int $itemsPerPage = 10, int $currentPage = 1) {
  7. $this->queueData = $queueData;
  8. $this->itemsPerPage = $itemsPerPage;
  9. $this->currentPage = $currentPage;
  10. }
  11. public function getPaginatedData(): array {
  12. // Calculate start and end indices
  13. $startIndex = ($this->currentPage - 1) * $this->itemsPerPage;
  14. $endIndex = $startIndex + $this->itemsPerPage;
  15. // Handle edge cases where the page goes beyond the data length
  16. $endIndex = min($endIndex, count($this->queueData));
  17. // Return the paginated data
  18. return array_slice($this->queueData, $startIndex, $endIndex - $startIndex);
  19. }
  20. public function getTotalPages(): int {
  21. return ceil(count($this->queueData) / $this->itemsPerPage);
  22. }
  23. public function getCurrentPage(): int {
  24. return $this->currentPage;
  25. }
  26. public function setCurrentPage(int $page): void {
  27. $this->currentPage = max(1, min($page, $this->getTotalPages())); // Ensure page is within valid range
  28. }
  29. public function isDryRunModeEnabled(): bool {
  30. return isset($_GET['dry_run']) && $_GET['dry_run'] === 'true';
  31. }
  32. public function getDryRunOutput(): string {
  33. if ($this->isDryRunModeEnabled()) {
  34. $data = $this->getPaginatedData();
  35. $totalPages = $this->getTotalPages();
  36. $currentPage = $this->getCurrentPage();
  37. return json_encode([
  38. 'data' => $data,
  39. 'total_pages' => $totalPages,
  40. 'current_page' => $currentPage
  41. ]);
  42. }
  43. return ''; // Return empty string if not in dry run mode.
  44. }
  45. }
  46. //Example Usage (replace with your actual queue data)
  47. $queueData = ['Message 1', 'Message 2', 'Message 3', 'Message 4', 'Message 5', 'Message 6', 'Message 7', 'Message 8', 'Message 9', 'Message 10', 'Message 11', 'Message 12'];
  48. $itemsPerPage = 3;
  49. //Dry run example
  50. if(isset($_GET['dry_run']) && $_GET['dry_run'] === 'true'){
  51. echo $instance->getDryRunOutput();
  52. exit;
  53. }
  54. $pager = new MessageQueuePager($queueData, $itemsPerPage);
  55. $currentPage = isset($_GET['page']) ? (int)$_GET['page'] : 1; // Get page number from URL, default to 1
  56. $pager->setCurrentPage($currentPage);
  57. $paginatedData = $pager->getPaginatedData();
  58. $totalPages = $pager->getTotalPages();
  59. ?>
  60. <!DOCTYPE html>
  61. <html>
  62. <head>
  63. <title>Message Queue Paginated Results</title>
  64. </head>
  65. <body>
  66. <h1>Message Queue</h1>
  67. <ul>
  68. <?php foreach ($paginatedData as $message): ?>
  69. <li><?php echo $message; ?></li>
  70. <?php endforeach; ?>
  71. </ul>
  72. <p>
  73. Page: <?php echo $currentPage; ?> of <?php echo $totalPages; ?>
  74. <a href="?page=<?php echo $currentPage - 1; ?>">Previous</a> |
  75. <a href="?page=<?php echo $currentPage + 1; ?>">Next</a>
  76. </p>
  77. <?php if($pager->isDryRunModeEnabled()): ?>
  78. <p>Dry Run Mode Enabled. Output is JSON.</p>
  79. <?php endif; ?>
  80. <script>
  81. //You can add JavaScript here for more advanced pagination features.
  82. </script>
  83. </body>
  84. </html>

Add your comment