Skip to content

Commit

Permalink
#210 Added the new pagination class
Browse files Browse the repository at this point in the history
  • Loading branch information
simba77 committed Mar 30, 2022
1 parent 19936dc commit 5c6b196
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 8 deletions.
14 changes: 6 additions & 8 deletions system/src/Http/Request.php
Expand Up @@ -113,17 +113,15 @@ private function filterVar(int|string $key, mixed $var, int $filter, mixed $opti

/**
* Getting a query string without the specified parameters.
*
* @param array $remove_params
* @return string
*/
public function getQueryString(array $remove_params = []): string
public function getQueryString(array $removeParams = [], array $additionalParams = []): string
{
$query_params = $this->getQueryParams();
if (! empty($remove_params)) {
$query_params = array_diff_key($query_params, array_flip($remove_params));
$queryParams = $this->getQueryParams();
if (! empty($removeParams)) {
$queryParams = array_diff_key($queryParams, array_flip($removeParams));
}
$str = http_build_query($query_params);
$queryParams = array_merge($queryParams, $additionalParams);
$str = http_build_query($queryParams);

return $this->getUri()->getPath() . (! empty($str) ? '?' . $str : '');
}
Expand Down
88 changes: 88 additions & 0 deletions system/src/Utility/Pagination.php
@@ -0,0 +1,88 @@
<?php

declare(strict_types=1);

namespace Johncms\Utility;

use JetBrains\PhpStorm\Pure;
use Johncms\Http\Request;
use Johncms\Settings\SiteSettings;
use Johncms\View\Render;

class Pagination
{
private int $total;
private string $pageParamName;
private int $perPage;
private int $currentPage;
private Request $request;
private \Compolomus\Pagination\Pagination $pagination;

public function __construct(int $total, ?int $perPage = null, string $pageParamName = 'page', ?int $currentPage = null)
{
$this->total = $total;
$this->pageParamName = $pageParamName;
$this->request = di(Request::class);
if (! $perPage) {
$this->perPage = di(SiteSettings::class)->getPerPage();
} else {
$this->perPage = $perPage;
}
$this->setCurrentPage($currentPage);
$this->pagination = new \Compolomus\Pagination\Pagination($this->currentPage, $this->perPage, $this->total, 2);
}

public function setCurrentPage(?int $currentPage = null): static
{
if ($currentPage) {
$this->currentPage = $currentPage;
} else {
$this->currentPage = $this->request->getQuery($this->pageParamName, 1, FILTER_VALIDATE_INT);
if ($this->currentPage < 1) {
$this->currentPage = 1;
}
}
return $this;
}

private function buildUrl(int $page): string
{
return $this->request->getQueryString(additionalParams: [$this->pageParamName => $page]);
}

#[Pure]
public function getOffset(): int
{
return $this->pagination->getOffset();
}

#[Pure]
public function getLimit(): int
{
$limit = $this->pagination->getLimit();
return min($this->total, $limit);
}

public function getPages(): array
{
$items = $this->pagination->get();
return array_map(function ($item) {
return [
'active' => $item === $this->currentPage,
'name' => $item,
'url' => is_numeric($item) ? $this->buildUrl($item) : '',
];
}, $items);
}

public function render(): string
{
$render = di(Render::class);
return $render->render(
'system::app/pagination',
[
'items' => $this->getPages(),
]
);
}
}

0 comments on commit 5c6b196

Please sign in to comment.