diff --git a/src/BaseListView.php b/src/BaseListView.php index 31febf5a0..d0c89ed4d 100644 --- a/src/BaseListView.php +++ b/src/BaseListView.php @@ -431,8 +431,11 @@ private function prepareDataReaderByParams( if (!$this->enableMultiSort) { $order = array_slice($order, 0, 1, true); } - $this->prepareOrder($order); - $dataReader = $dataReader->withSort($sortObject->withOrder($order)); + $dataReader = $dataReader->withSort( + $sortObject->withOrder( + $this->prepareOrder($order) + ) + ); } } @@ -445,9 +448,11 @@ private function prepareDataReaderByParams( /** * @psalm-param TOrder $order + * @psalm-return TOrder */ - protected function prepareOrder(array &$order): void + protected function prepareOrder(array $order): array { + return []; } /** @@ -777,7 +782,7 @@ public function pageSizeConstraint(array|int|bool $pageSizeConstraint): static * * @psalm-return array */ - protected function getOverrideOrderFields(): array + protected function getOrderProperties(): array { return []; } @@ -838,7 +843,6 @@ private function renderPagination(): string } $context = new PaginationContext( - $this->getOverrideOrderFields(), $nextUrlPattern, $previousUrlPattern, $defaultUrl, @@ -1046,7 +1050,7 @@ private function getSortValueForUrl(PaginatorInterface $paginator): ?string } $order = []; - $overrideOrderFields = array_flip($this->getOverrideOrderFields()); + $overrideOrderFields = array_flip($this->getOrderProperties()); foreach ($sort->getOrder() as $name => $value) { $key = array_key_exists($name, $overrideOrderFields) ? $overrideOrderFields[$name] diff --git a/src/Column/Base/HeaderContext.php b/src/Column/Base/HeaderContext.php index 24acbba98..e60001e11 100644 --- a/src/Column/Base/HeaderContext.php +++ b/src/Column/Base/HeaderContext.php @@ -27,13 +27,13 @@ final class HeaderContext /** * @internal * - * @psalm-param array $overrideOrderFields + * @psalm-param array $orderProperties * @psalm-param UrlCreator|null $urlCreator */ public function __construct( private readonly ?Sort $originalSort, private readonly ?Sort $sort, - private readonly array $overrideOrderFields, + private readonly array $orderProperties, private readonly ?string $sortableHeaderClass, private readonly string|Stringable $sortableHeaderPrepend, private readonly string|Stringable $sortableHeaderAppend, @@ -67,8 +67,13 @@ public function translate(string|Stringable $id): string public function prepareSortable(Cell $cell, string $property): array { $originalProperty = $property; - $property = $this->overrideOrderFields[$property] ?? $property; - if ($this->sort === null || $this->originalSort === null || !$this->sort->hasFieldInConfig($property)) { + $property = $this->orderProperties[$property] ?? ''; + if ( + $property === '' + || $this->sort === null + || $this->originalSort === null + || !$this->sort->hasFieldInConfig($property) + ) { return [$cell, null, '', '']; } diff --git a/src/Column/DataColumnRenderer.php b/src/Column/DataColumnRenderer.php index eb79982ee..5869cf4a4 100644 --- a/src/Column/DataColumnRenderer.php +++ b/src/Column/DataColumnRenderer.php @@ -33,7 +33,7 @@ /** * @psalm-import-type FilterEmptyCallable from DataColumn */ -final class DataColumnRenderer implements FilterableColumnRendererInterface, OverrideOrderFieldsColumnInterface +final class DataColumnRenderer implements FilterableColumnRendererInterface, SortableColumnInterface { /** * @var bool|callable @@ -271,18 +271,15 @@ private function checkColumn(ColumnInterface $column): void } } - public function getOverrideOrderFields(ColumnInterface $column): array + public function getOrderProperties(ColumnInterface $column): array { $this->checkColumn($column); /** @var DataColumn $column This annotation is for IDE only */ - if ($column->property === null - || $column->field === null - || $column->property === $column->field - ) { + if ($column->property === null) { return []; } - return [$column->property => $column->field]; + return [$column->property => $column->field ?? $column->property]; } } diff --git a/src/Column/OverrideOrderFieldsColumnInterface.php b/src/Column/OverrideOrderFieldsColumnInterface.php deleted file mode 100644 index 70a21d8d3..000000000 --- a/src/Column/OverrideOrderFieldsColumnInterface.php +++ /dev/null @@ -1,13 +0,0 @@ - - */ - public function getOverrideOrderFields(ColumnInterface $column): array; -} diff --git a/src/Column/SortableColumnInterface.php b/src/Column/SortableColumnInterface.php new file mode 100644 index 000000000..7f2b0ff1e --- /dev/null +++ b/src/Column/SortableColumnInterface.php @@ -0,0 +1,16 @@ + + */ + public function getOrderProperties(ColumnInterface $column): array; +} diff --git a/src/GridView.php b/src/GridView.php index 5059b4140..50773a615 100644 --- a/src/GridView.php +++ b/src/GridView.php @@ -7,7 +7,6 @@ use Closure; use Psr\Container\ContainerInterface; use Stringable; -use Yiisoft\Arrays\ArrayHelper; use Yiisoft\Data\Paginator\PaginatorInterface; use Yiisoft\Data\Reader\ReadableDataInterface; use Yiisoft\Data\Reader\Sort; @@ -26,9 +25,10 @@ use Yiisoft\Yii\DataView\Column\ColumnInterface; use Yiisoft\Yii\DataView\Column\ColumnRendererInterface; use Yiisoft\Yii\DataView\Column\FilterableColumnRendererInterface; -use Yiisoft\Yii\DataView\Column\OverrideOrderFieldsColumnInterface; +use Yiisoft\Yii\DataView\Column\SortableColumnInterface; use Yiisoft\Yii\DataView\Filter\Factory\IncorrectValueException; +use function array_key_exists; use function call_user_func; use function call_user_func_array; use function is_callable; @@ -565,7 +565,7 @@ protected function renderItems(array $items, ValidationResult $filterValidationR $headerContext = new HeaderContext( $this->getSort($dataReader), $this->getSort($this->preparedDataReader), - $this->getOverrideOrderFields(), + $this->getOrderProperties(), $this->sortableHeaderClass, $this->sortableHeaderPrepend, $this->sortableHeaderAppend, @@ -700,32 +700,38 @@ protected function makeFilters(): array return [$filters, $validationResult]; } - protected function prepareOrder(array &$order): void + protected function prepareOrder(array $order): array { $columns = $this->getColumns(); $renderers = $this->getColumnRenderers(); + + $result = []; foreach ($columns as $i => $column) { - if ($renderers[$i] instanceof OverrideOrderFieldsColumnInterface) { - foreach ($renderers[$i]->getOverrideOrderFields($column) as $from => $to) { - $order = ArrayHelper::renameKey($order, $from, $to); + if ($renderers[$i] instanceof SortableColumnInterface) { + foreach ($renderers[$i]->getOrderProperties($column) as $from => $to) { + if (array_key_exists($from, $order)) { + $result[$to] = $order[$from]; + } } } } + + return $result; } - protected function getOverrideOrderFields(): array + protected function getOrderProperties(): array { $columns = $this->getColumns(); $renderers = $this->getColumnRenderers(); - $overrideOrderFields = []; + $orderProperties = []; foreach ($columns as $i => $column) { - if ($renderers[$i] instanceof OverrideOrderFieldsColumnInterface) { - $overrideOrderFields[] = $renderers[$i]->getOverrideOrderFields($column); + if ($renderers[$i] instanceof SortableColumnInterface) { + $orderProperties[] = $renderers[$i]->getOrderProperties($column); } } - return array_merge(...$overrideOrderFields); + return array_merge(...$orderProperties); } /** diff --git a/src/Pagination/PaginationContext.php b/src/Pagination/PaginationContext.php index 42f1cd55c..d5c7e03b9 100644 --- a/src/Pagination/PaginationContext.php +++ b/src/Pagination/PaginationContext.php @@ -12,11 +12,8 @@ final class PaginationContext /** * @internal - * - * @param array $overrideOrderFields */ public function __construct( - public readonly array $overrideOrderFields, public readonly string $nextUrlPattern, public readonly string $previousUrlPattern, public readonly string $defaultUrl,