Skip to content

Commit

Permalink
Replace OverrideOrderFieldsColumnInterface to `SortableColumnInterf…
Browse files Browse the repository at this point in the history
…ace` (#231)
  • Loading branch information
vjik authored Jan 13, 2025
1 parent a1f120e commit b305e70
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 45 deletions.
16 changes: 10 additions & 6 deletions src/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
);
}
}

Expand All @@ -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 [];
}

/**
Expand Down Expand Up @@ -777,7 +782,7 @@ public function pageSizeConstraint(array|int|bool $pageSizeConstraint): static
*
* @psalm-return array<string, string>
*/
protected function getOverrideOrderFields(): array
protected function getOrderProperties(): array
{
return [];
}
Expand Down Expand Up @@ -838,7 +843,6 @@ private function renderPagination(): string
}

$context = new PaginationContext(
$this->getOverrideOrderFields(),
$nextUrlPattern,
$previousUrlPattern,
$defaultUrl,
Expand Down Expand Up @@ -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]
Expand Down
13 changes: 9 additions & 4 deletions src/Column/Base/HeaderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ final class HeaderContext
/**
* @internal
*
* @psalm-param array<string, string> $overrideOrderFields
* @psalm-param array<string, string> $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,
Expand Down Expand Up @@ -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, '', ''];
}

Expand Down
11 changes: 4 additions & 7 deletions src/Column/DataColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
}
}
13 changes: 0 additions & 13 deletions src/Column/OverrideOrderFieldsColumnInterface.php

This file was deleted.

16 changes: 16 additions & 0 deletions src/Column/SortableColumnInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\DataView\Column;

interface SortableColumnInterface
{
/**
* @return array The properties that can be sorted by this column. The array keys are the logical property names,
* and the array values are the corresponding field names.
*
* @psalm-return array<string,string>
*/
public function getOrderProperties(ColumnInterface $column): array;
}
30 changes: 18 additions & 12 deletions src/GridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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);
}

/**
Expand Down
3 changes: 0 additions & 3 deletions src/Pagination/PaginationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ final class PaginationContext

/**
* @internal
*
* @param array<string, string> $overrideOrderFields
*/
public function __construct(
public readonly array $overrideOrderFields,
public readonly string $nextUrlPattern,
public readonly string $previousUrlPattern,
public readonly string $defaultUrl,
Expand Down

0 comments on commit b305e70

Please sign in to comment.