Skip to content

Commit

Permalink
Merge branch 'master' into 224-tolerate-incorrect-page
Browse files Browse the repository at this point in the history
  • Loading branch information
samdark committed Dec 22, 2024
2 parents 4584ad8 + 636c4a7 commit d994016
Show file tree
Hide file tree
Showing 24 changed files with 558 additions and 524 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
"maglnet/composer-require-checker": "^4.7",
"nyholm/psr7": "^1.3",
"phpunit/phpunit": "^10.5",
"rector/rector": "^1.1.1",
"rector/rector": "^2.0.3",
"roave/infection-static-analysis-plugin": "^1.34",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^5.25",
Expand Down
1 change: 0 additions & 1 deletion config/widgets-themes.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
'itemTag()' => ['li'],
'itemAttributes()' => [['class' => 'page-item']],
'linkAttributes()' => [['class' => 'page-link']],
'currentItemClass()' => ['active'],
'disabledItemClass()' => ['disabled'],
],
InputPageSize::class => [
Expand Down
108 changes: 68 additions & 40 deletions src/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@
use Yiisoft\Yii\DataView\Exception\DataReaderNotSetException;
use Yiisoft\Yii\DataView\PageSize\InputPageSize;
use Yiisoft\Yii\DataView\PageSize\PageSizeContext;
use Yiisoft\Yii\DataView\PageSize\PageSizeControlInterface;
use Yiisoft\Yii\DataView\PageSize\PageSizeWidgetInterface;
use Yiisoft\Yii\DataView\PageSize\SelectPageSize;
use Yiisoft\Yii\DataView\Pagination\KeysetPagination;
use Yiisoft\Yii\DataView\Pagination\OffsetPagination;
use Yiisoft\Yii\DataView\Pagination\PaginationContext;
use Yiisoft\Yii\DataView\Pagination\PaginationWidgetInterface;
use Yiisoft\Yii\DataView\Pagination\PaginatorNotSupportedException;

use function array_key_exists;
use function array_slice;
Expand All @@ -49,7 +51,6 @@
use function in_array;
use function is_array;
use function is_int;
use function is_string;

/**
* @psalm-type UrlArguments = array<string,scalar|Stringable|null>
Expand Down Expand Up @@ -91,8 +92,8 @@ abstract class BaseListView extends Widget
*/
private ?string $pageSizeTag = 'div';
private array $pageSizeAttributes = [];
private ?string $pageSizeTemplate = 'Results per page {control}';
private PageSizeControlInterface|null $pageSizeControl = null;
private ?string $pageSizeTemplate = 'Results per page {widget}';
private PageSizeWidgetInterface|null $pageSizeWidget = null;

/**
* A name for {@see CategorySource} used with translator ({@see TranslatorInterface}) by default.
Expand Down Expand Up @@ -127,7 +128,7 @@ abstract class BaseListView extends Widget

private array $offsetPaginationConfig = [];
private array $keysetPaginationConfig = [];
private string|OffsetPagination|KeysetPagination|null $pagination = null;
private PaginationWidgetInterface|null $paginationWidget = null;
protected ?ReadableDataInterface $dataReader = null;
private string $toolbar = '';

Expand Down Expand Up @@ -539,7 +540,7 @@ final public function pageSizeAttributes(array $values): static
*
* The following tokens will be replaced with the corresponding values:
*
* - `{control}` — page size control.
* - `{widget}` — page size widget.
*/
final public function pageSizeTemplate(?string $template): static
{
Expand All @@ -548,17 +549,17 @@ final public function pageSizeTemplate(?string $template): static
return $new;
}

final public function pageSizeControl(?PageSizeControlInterface $widget): static
final public function pageSizeWidget(?PageSizeWidgetInterface $widget): static
{
$new = clone $this;
$new->pageSizeControl = $widget;
$new->pageSizeWidget = $widget;
return $new;
}

public function pagination(string|KeysetPagination|OffsetPagination|null $pagination): static
public function paginationWidget(PaginationWidgetInterface|null $widget): static
{
$new = clone $this;
$new->pagination = $pagination;
$new->paginationWidget = $widget;
return $new;
}

Expand Down Expand Up @@ -783,48 +784,67 @@ protected function getOverrideOrderFields(): array

private function renderPagination(): string
{
$preparedDataReader = $this->preparedDataReader;
if (!$preparedDataReader instanceof PaginatorInterface || !$preparedDataReader->isPaginationRequired()) {
$dataReader = $this->preparedDataReader;
if (!$dataReader instanceof PaginatorInterface || !$dataReader->isPaginationRequired()) {
return '';
}

if (is_string($this->pagination)) {
return $this->pagination;
}

if ($this->pagination === null) {
if ($preparedDataReader instanceof OffsetPaginator) {
$pagination = OffsetPagination::widget(config: $this->offsetPaginationConfig);
} elseif ($preparedDataReader instanceof KeysetPaginator) {
$pagination = KeysetPagination::widget(config: $this->keysetPaginationConfig);
if ($this->paginationWidget === null) {
if ($dataReader instanceof OffsetPaginator) {
$widget = OffsetPagination::widget(config: $this->offsetPaginationConfig);
} elseif ($dataReader instanceof KeysetPaginator) {
$widget = KeysetPagination::widget(config: $this->keysetPaginationConfig);
} else {
return '';
}
try {
$widget = $widget->withPaginator($dataReader);
} catch (PaginatorNotSupportedException) {
return '';
}
} else {
$pagination = $this->pagination;
$widget = $this->paginationWidget->withPaginator($dataReader);
}

if ($pagination instanceof OffsetPagination && $preparedDataReader instanceof OffsetPaginator) {
$pagination = $pagination->paginator($preparedDataReader);
} elseif ($pagination instanceof KeysetPagination && $preparedDataReader instanceof KeysetPaginator) {
$pagination = $pagination->paginator($preparedDataReader);
if ($this->urlCreator === null) {
$nextUrlPattern = '#page=' . PaginationContext::URL_PLACEHOLDER;
$previousUrlPattern = '#previous-page=' . PaginationContext::URL_PLACEHOLDER;
$defaultUrl = '#';
} else {
return '';
}

if ($this->urlCreator !== null) {
$pagination = $pagination->urlCreator($this->urlCreator);
$pageSize = $this->getPageSizeValueForUrl($dataReader);
$sort = $this->getSortValueForUrl($dataReader);
$nextUrlPattern = call_user_func_array(
$this->urlCreator,
UrlParametersFactory::create(
PageToken::next(PaginationContext::URL_PLACEHOLDER),
$pageSize,
$sort,
$this->urlConfig
),
);
$previousUrlPattern = call_user_func_array(
$this->urlCreator,
UrlParametersFactory::create(
PageToken::previous(PaginationContext::URL_PLACEHOLDER),
$pageSize,
$sort,
$this->urlConfig
),
);
$defaultUrl = call_user_func_array(
$this->urlCreator,
UrlParametersFactory::create(null, $pageSize, $sort, $this->urlConfig),
);
}

$context = new PaginationContext(
$this->getOverrideOrderFields(),
$nextUrlPattern,
$previousUrlPattern,
$defaultUrl,
);

return $pagination
->context($context)
->defaultPageSize($this->getDefaultPageSize())
->urlConfig($this->urlConfig)
->render();
return $widget->withContext($context)->render();
}

private function renderPageSize(): string
Expand All @@ -838,7 +858,7 @@ private function renderPageSize(): string
return '';
}

if ($this->pageSizeControl === null) {
if ($this->pageSizeWidget === null) {
if ($this->pageSizeConstraint === false || is_int($this->pageSizeConstraint)) {
$widget = InputPageSize::widget();
} elseif (is_array($this->pageSizeConstraint)) {
Expand All @@ -847,7 +867,7 @@ private function renderPageSize(): string
return '';
}
} else {
$widget = $this->pageSizeControl;
$widget = $this->pageSizeWidget;
}

if ($this->urlCreator === null) {
Expand Down Expand Up @@ -877,11 +897,11 @@ private function renderPageSize(): string
$urlPattern,
$defaultUrl,
);
$control = $widget->withContext($context)->render();
$renderedWidget = $widget->withContext($context)->render();

$content = $this->translator->translate(
$this->pageSizeTemplate,
['control' => $control],
['widget' => $renderedWidget],
$this->translationCategory,
);

Expand Down Expand Up @@ -1005,6 +1025,14 @@ private function createDefaultTranslator(): Translator
return $translator;
}

private function getPageSizeValueForUrl(PaginatorInterface $paginator): ?string
{
$pageSize = $paginator->getPageSize();
return $pageSize === $this->getDefaultPageSize()
? null
: (string) $pageSize;
}

private function getSortValueForUrl(PaginatorInterface $paginator): ?string
{
$sort = $this->getSort($paginator);
Expand Down
2 changes: 1 addition & 1 deletion src/PageSize/InputPageSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Yiisoft\Html\Html;
use Yiisoft\Widget\Widget;

final class InputPageSize extends Widget implements PageSizeControlInterface
final class InputPageSize extends Widget implements PageSizeWidgetInterface
{
use PageSizeContextTrait;

Expand Down
2 changes: 1 addition & 1 deletion src/PageSize/PageSizeContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use LogicException;

/**
* @psalm-require-implements PageSizeControlInterface
* @psalm-require-implements PageSizeWidgetInterface
*/
trait PageSizeContextTrait
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Stringable;

interface PageSizeControlInterface extends Stringable
interface PageSizeWidgetInterface extends Stringable
{
public function withContext(PageSizeContext $context): static;

Expand Down
2 changes: 1 addition & 1 deletion src/PageSize/SelectPageSize.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use function count;
use function is_array;

final class SelectPageSize extends Widget implements PageSizeControlInterface
final class SelectPageSize extends Widget implements PageSizeWidgetInterface
{
use PageSizeContextTrait;

Expand Down
Loading

0 comments on commit d994016

Please sign in to comment.