itemCount = $item_count; $this->setTerms(__("%s result"), __("%s results")); $page = $_GET["page"] ?? null; $page = (int) $page; if($page > 0) { $this->setPage($page); } } public function setItemCount(int $count): bool { $this->itemCount = $count; $this->currentPage = max(min($this->currentPage, $this->getPageCount()), 1); return true; } public function setTerms(string $single, string $plural): bool { $this->single = $single; $this->plural = $plural; return true; } public function getOffset(): int { return $this->pageSize * ($this->currentPage - 1); } public function slice(array $array): array { return array_slice($array, $this->getOffset(), $this->pageSize); } private function from(): int { return $this->getOffset() + 1; } private function to(): int { return min($this->getOffset() + $this->pageSize, $this->itemCount); } private function getCurrentPage(): int { return $this->currentPage; } private function getPageCount(): int { return (int) ceil($this->itemCount / $this->pageSize) ?: 1; } public function getPageText(): string { return __("Page %d of %d", $this->getCurrentPage(), $this->getPageCount()); } private function hasPreviousPage(): bool { return $this->currentPage > 1; } private function hasNextPage(): bool { return $this->currentPage < $this->getPageCount(); } private function urlForPage(int $page_number): string { $query = $_GET; $query = array_merge($query, $this->parameters); $query["page"] = $page_number; return $_SERVER["REQUEST_SCHEME"] . "://" . $_SERVER["SERVER_NAME"] . parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH) ."?". http_build_query($query); } public function getText(): string { $count_text = n__($this->single, $this->plural, $this->itemCount, number_format($this->itemCount)); if($this->getPageCount() <= 1) { return sprintf("Showing %s", $count_text); } return sprintf("Showing %s-%s of %s", number_format($this->from()), number_format($this->to()), $count_text ); } public function requiresPagination(): bool { return $this->getPageCount() > 1; } /** * @param array $options container_class Pagination container CSS class (default: pagination justify-content-center)
* item_class Item container CSS class (default: page-item)
* next Text for next link (default: Next)
* previous Text for previous link (default: Previous) * @return string */ public function build(array $options = []): string { $container_class = $options["container_class"] ?? "pagination justify-content-center"; $item_class = $options["item_class"] ?? "page-item"; $first_text = ''; $previous_text = ''; $next_text = ''; $last_text = ''; $current_page = $this->getCurrentPage(); $min_page = max($current_page - $this->width, 1); $max_page = min($current_page + $this->width, $this->getPageCount()); $previous_page = $current_page - 1; $next_page = $current_page + 1; $output = ""; // Page count is 1 or less if(!$this->requiresPagination()) { //return ""; } // Start building output $get_page_link_html = function(string $text, int $page) use ($item_class) { if($this->isJsHandled) { return "
  • {$text}
  • "; } return "
  • urlForPage($page)}\" data-page=\"{$page}\" data-ajax-load>{$text}
  • "; }; $get_disabled_link_html = static function(string $text) use ($item_class) { return "
  • {$text}
  • "; }; $get_active_link_html = function() use ($item_class) { return "
  • {$this->getCurrentPage()}
  • "; }; $output .= ""; $output .= '
    ' . $this->getText() . ' / ' . $this->getPageText() . '
    '; return $output; } public function render(): void { echo $this->build(); } public function addParameter(string $name, ?string $value) { if(!$value) return; $this->parameters[$name] = $value; } public function setPage(?int $page) { if($page < 1) $page = 1; $this->currentPage = $page; } public function setPageSize(int $size) { $this->pageSize = $size; } /** * @param bool $js_handled */ public function setIsJsHandled(bool $js_handled): void { $this->isJsHandled = $js_handled; } public function getPageSize(): int { return $this->pageSize; } }