Skip to content

Commit

Permalink
Use property names for "sort" parameter in pagination links (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Nov 16, 2024
1 parent b5108c4 commit 3394978
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 12 deletions.
18 changes: 18 additions & 0 deletions src/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
use Yiisoft\Data\Reader\OrderHelper;
use Yiisoft\Yii\DataView\Exception\DataReaderNotSetException;

use function array_slice;
use function extension_loaded;
use function is_array;
use function is_string;

Expand Down Expand Up @@ -646,6 +648,17 @@ protected function getDefaultPageSize(): int
return $this->defaultPageSize;
}

/**
* Returns order field names that should be replaced in URL sort argument.
* Format: `['field_name_in_url' => 'real_field_name']`.
*
* @psalm-return array<string, string>
*/
protected function getOverrideOrderFields(): array

Check warning on line 657 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "ProtectedVisibility": --- Original +++ New @@ @@ * * @psalm-return array<string, string> */ - protected function getOverrideOrderFields() : array + private function getOverrideOrderFields() : array { return []; }
{
return [];
}

private function renderPagination(): string
{
$preparedDataReader = $this->preparedDataReader;
Expand Down Expand Up @@ -681,7 +694,12 @@ private function renderPagination(): string
$pagination = $pagination->urlCreator($this->urlCreator);
}

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

return $pagination
->context($context)
->defaultPageSize($this->getDefaultPageSize())
->urlConfig($this->urlConfig)
->render();
Expand Down
40 changes: 39 additions & 1 deletion src/BasePagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
use InvalidArgumentException;
use Yiisoft\Data\Paginator\PageToken;
use Yiisoft\Data\Paginator\PaginatorInterface;
use Yiisoft\Data\Reader\OrderHelper;
use Yiisoft\Html\Html;
use Yiisoft\Widget\Widget;

use function array_key_exists;
use function call_user_func_array;

/**
* @psalm-import-type UrlCreator from BaseListView
*/
abstract class BasePagination extends Widget
{
private ?PaginationContext $context = null;

/**
* @psalm-var UrlCreator|null
*/
Expand Down Expand Up @@ -49,6 +53,13 @@ abstract class BasePagination extends Widget
private ?string $disabledItemClass = null;
private ?string $disabledLinkClass = null;

final public function context(?PaginationContext $context): static
{
$new = clone $this;
$new->context = $context;
return $new;
}

final public function listTag(?string $tag): static
{
if ($tag === '') {
Expand Down Expand Up @@ -211,7 +222,7 @@ protected function createUrl(PageToken $pageToken): string

$paginator = $this->getPaginator();
$pageSize = $paginator->getPageSize();
$sort = $paginator->isSortable() ? $paginator->getSort()?->getOrderAsString() : null;
$sort = $this->getSort($paginator, $this->context);

return call_user_func_array(
$this->urlCreator,
Expand All @@ -232,4 +243,31 @@ abstract protected function getItems(): array;
abstract protected function getPaginator(): PaginatorInterface;

abstract protected function isFirstPage(PageToken $token): bool;

private function getSort(PaginatorInterface $paginator, ?PaginationContext $context): ?string
{
if (!$paginator->isSortable()) {
return null;
}

$sort = $paginator->getSort();
if ($sort === null) {
return null;
}

if ($context === null) {
return $sort->getOrderAsString();
}

$order = [];
$overrideOrderFields = array_flip($context->overrideOrderFields);
foreach ($sort->getOrder() as $name => $value) {
$key = array_key_exists($name, $overrideOrderFields)
? $overrideOrderFields[$name]
: $name;
$order[$key] = $value;
}

return OrderHelper::arrayToString($order);
}
}
5 changes: 4 additions & 1 deletion src/Column/Base/HeaderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
use Yiisoft\Yii\DataView\UrlConfig;
use Yiisoft\Yii\DataView\UrlParametersFactory;

use function call_user_func_array;
use function count;

/**
* @psalm-import-type UrlCreator from BaseListView
*/
Expand All @@ -24,7 +27,7 @@ final class HeaderContext
/**
* @internal
*
* @psalm-param array<string,string> $overrideOrderFields
* @psalm-param array<string, string> $overrideOrderFields
* @psalm-param UrlCreator|null $urlCreator
*/
public function __construct(
Expand Down
26 changes: 16 additions & 10 deletions src/GridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,20 +545,11 @@ protected function renderItems(array $items, ValidationResult $filterValidationR
$blocks[] = Html::colgroup()->columns(...$tags)->render();
}

$overrideOrderFields = [];
foreach ($columns as $i => $column) {
if ($renderers[$i] instanceof OverrideOrderFieldsColumnInterface) {
$overrideOrderFields[] = $renderers[$i]->getOverrideOrderFields($column);
}
}

$overrideOrderFields = array_merge(...$overrideOrderFields);

if ($this->headerTableEnabled) {
$headerContext = new HeaderContext(
$this->getSort($dataReader),
$this->getSort($this->preparedDataReader),
$overrideOrderFields,
$this->getOverrideOrderFields(),
$this->sortableHeaderClass,
$this->sortableHeaderPrepend,
$this->sortableHeaderAppend,
Expand Down Expand Up @@ -702,6 +693,21 @@ protected function prepareOrder(array &$order): void
}
}

protected function getOverrideOrderFields(): array
{
$columns = $this->getColumns();
$renderers = $this->getColumnRenderers();

$overrideOrderFields = [];
foreach ($columns as $i => $column) {
if ($renderers[$i] instanceof OverrideOrderFieldsColumnInterface) {
$overrideOrderFields[] = $renderers[$i]->getOverrideOrderFields($column);
}
}

return array_merge(...$overrideOrderFields);
}

private function prepareBodyAttributes(array $attributes, DataContext $context): array
{
foreach ($attributes as $i => $attribute) {
Expand Down
18 changes: 18 additions & 0 deletions src/PaginationContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\DataView;

final class PaginationContext
{
/**
* @internal
*
* @param array<string, string> $overrideOrderFields
*/
public function __construct(
public readonly array $overrideOrderFields,
) {
}
}

0 comments on commit 3394978

Please sign in to comment.