Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Jan 9, 2024
1 parent 6e376f3 commit a2b4ce8
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 16 deletions.
30 changes: 29 additions & 1 deletion src/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ abstract class BaseListView extends Widget
protected array $urlArguments = [];
protected array $urlQueryParameters = [];

private UrlParameterProviderInterface|null $urlParameterProvider;

public function __construct(
TranslatorInterface|null $translator = null,
private UrlGeneratorInterface|null $urlGenerator = null,
Expand All @@ -72,6 +74,13 @@ public function __construct(
$this->translator = $translator ?? $this->createDefaultTranslator();
}

final public function urlParameterProvider(?UrlParameterProviderInterface $provider): static
{
$new = clone $this;
$new->urlParameterProvider = $provider;
return $new;
}

/**
* Renders the data models.
*
Expand Down Expand Up @@ -427,7 +436,26 @@ private function renderPagination(): string
return $this->pagination;
}

return ($this->pagination ?? OffsetPagination::widget())
$pagination = $this->pagination ??
$dataReader instanceof KeysetPaginator ? KeysetPagination::widget() : OffsetPagination::widget();

$pageSize = $this->urlParameterProvider?->get(
$pagination->getPageSizeParameterName(),
$pagination->getPageSizeParameterPlace()
);
if ($pageSize !== null) {
$dataReader = $dataReader->withPageSize((int)$pageSize);
}

$page = $this->urlParameterProvider?->get(
$pagination->getPageParameterName(),
$pagination->getPageParameterPlace()
);
if ($page !== null) {
$dataReader = $dataReader->withNextPageToken($page);
}

return $pagination
->paginator($dataReader)
->render();
}
Expand Down
49 changes: 35 additions & 14 deletions src/BasePagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,17 @@ abstract class BasePagination extends Widget
private $urlCreator;
private string $pageParameterName = 'page';
private string $pageSizeParameterName = 'pagesize';

/**
* @psalm-var UrlParameterPlace::*
*/
private int $pageParameterPlace = UrlParameterPlace::QUERY;

/**
* @psalm-var UrlParameterPlace::*
*/
private int $pageSizeParameterPlace = UrlParameterPlace::QUERY;

private array $queryParameters = [];

private array $attributes = [];
Expand All @@ -37,9 +46,34 @@ abstract class BasePagination extends Widget
private string $menuClass = 'pagination';
private string $menuItemContainerClass = 'page-item';
private string $menuItemLinkClass = 'page-link';
private array $pageConfig = [];
private PaginatorInterface|null $paginator = null;

public function getPageParameterName(): string
{
return $this->pageParameterName;
}

public function getPageSizeParameterName(): string
{
return $this->pageSizeParameterName;
}

/**
* @psalm-return UrlParameterPlace::*
*/
public function getPageParameterPlace(): int
{
return $this->pageParameterPlace;
}

/**
* @psalm-return UrlParameterPlace::*
*/
public function getPageSizeParameterPlace(): int
{
return $this->pageSizeParameterPlace;
}

/**
* @psalm-param UrlCreator|null $urlCreator
*/
Expand Down Expand Up @@ -248,19 +282,6 @@ public function menuItemContainerClass(string $value): static
return $new;
}

/**
* Return a new instance with page config for arguments or query parameters in url.
*
* @param array $value The page config for arguments or query parameters in url.
*/
public function pageConfig(array $value): static
{
$new = clone $this;
$new->pageConfig = $value;

return $new;
}

/**
* Return a new instance with name of argument or query parameter for page.
*
Expand Down
13 changes: 13 additions & 0 deletions src/UrlParameterProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\DataView;

interface UrlParameterProviderInterface
{
/**
* @psalm-param UrlParameterPlace::* $place
*/
public function get(string $name, int $place): ?string;
}
29 changes: 29 additions & 0 deletions src/YiiRouter/UrlParameterProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\DataView\YiiRouter;

use Yiisoft\Router\CurrentRoute;
use Yiisoft\Yii\DataView\UrlParameterPlace;
use Yiisoft\Yii\DataView\UrlParameterProviderInterface;

final class UrlParameterProvider implements UrlParameterProviderInterface
{
public function __construct(private CurrentRoute $currentRoute)
{
}

public function get(string $name, int $place): ?string
{
switch ($place) {
case UrlParameterPlace::PATH:
return $this->currentRoute->getArgument($name);
case UrlParameterPlace::QUERY:
$value = $_GET[$name] ?? null;
return is_string($value) ? $value : null;
default:
return null;
}
}
}
1 change: 0 additions & 1 deletion tests/Pagination/ImmutableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public function render(): string
$this->assertNotSame($basePagination, $basePagination->labelPreviousPage());
$this->assertNotSame($basePagination, $basePagination->menuClass(''));
$this->assertNotSame($basePagination, $basePagination->menuItemContainerClass(''));
$this->assertNotSame($basePagination, $basePagination->pageConfig([]));
$this->assertNotSame($basePagination, $basePagination->pageParameterName(''));
$this->assertNotSame($basePagination, $basePagination->pageSizeParameterName('next'));
$this->assertNotSame(
Expand Down

0 comments on commit a2b4ce8

Please sign in to comment.