Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace OverrideOrderFieldsColumnInterface to SortableColumnInterface #231

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@

final public function ignoreMissingPage(bool $value): static
{
$new = clone $this;

Check warning on line 173 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ } public final function ignoreMissingPage(bool $value) : static { - $new = clone $this; + $new = $this; $new->ignoreMissingPage = $value; return $new; }
$new->ignoreMissingPage = $value;
return $new;
}
Expand Down Expand Up @@ -218,7 +218,7 @@

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

Check warning on line 221 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ } public final function urlParameterProvider(?UrlParameterProviderInterface $provider) : static { - $new = clone $this; + $new = $this; $new->urlParameterProvider = $provider; return $new; }
$new->urlParameterProvider = $provider;
return $new;
}
Expand Down Expand Up @@ -313,7 +313,7 @@
*
* @psalm-return list{FilterInterface[]|null,ValidationResult}
*/
protected function makeFilters(): array

Check warning on line 316 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 list{FilterInterface[]|null,ValidationResult} */ - protected function makeFilters() : array + private function makeFilters() : array { return [[], new ValidationResult()]; }
{
return [[], new ValidationResult()];
}
Expand Down Expand Up @@ -424,19 +424,22 @@
}
}

if (!empty($sort) && $dataReader->isSortable()) {

Check warning on line 427 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": --- Original +++ New @@ @@ $dataReader = $dataReader->withToken(PageToken::previous($previousPage)); } } - if (!empty($sort) && $dataReader->isSortable()) { + if (!empty($sort) && !$dataReader->isSortable()) { $sortObject = $dataReader->getSort(); if ($sortObject !== null) { $order = OrderHelper::stringToArray($sort);
$sortObject = $dataReader->getSort();
if ($sortObject !== null) {
$order = OrderHelper::stringToArray($sort);
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)
)
);
}
}

if (!empty($filters) && $dataReader->isFilterable()) {

Check warning on line 442 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ $dataReader = $dataReader->withSort($sortObject->withOrder($this->prepareOrder($order))); } } - if (!empty($filters) && $dataReader->isFilterable()) { + if (!empty($filters) || $dataReader->isFilterable()) { $dataReader = $dataReader->withFilter(new All(...$filters)); } return $dataReader;

Check warning on line 442 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndNegation": --- Original +++ New @@ @@ $dataReader = $dataReader->withSort($sortObject->withOrder($this->prepareOrder($order))); } } - if (!empty($filters) && $dataReader->isFilterable()) { + if (!(!empty($filters) && $dataReader->isFilterable())) { $dataReader = $dataReader->withFilter(new All(...$filters)); } return $dataReader;

Check warning on line 442 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": --- Original +++ New @@ @@ $dataReader = $dataReader->withSort($sortObject->withOrder($this->prepareOrder($order))); } } - if (!empty($filters) && $dataReader->isFilterable()) { + if (!empty($filters) && !$dataReader->isFilterable()) { $dataReader = $dataReader->withFilter(new All(...$filters)); } return $dataReader;
$dataReader = $dataReader->withFilter(new All(...$filters));
}

Expand All @@ -445,9 +448,11 @@

/**
* @psalm-param TOrder $order
* @psalm-return TOrder
*/
protected function prepareOrder(array &$order): void
protected function prepareOrder(array $order): array
{
return [];
}

/**
Expand Down Expand Up @@ -571,7 +576,7 @@
*/
public function offsetPaginationConfig(array $config): static
{
$new = clone $this;

Check warning on line 579 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ */ public function offsetPaginationConfig(array $config) : static { - $new = clone $this; + $new = $this; $new->offsetPaginationConfig = $config; return $new; }
$new->offsetPaginationConfig = $config;
return $new;
}
Expand All @@ -584,7 +589,7 @@
*/
public function keysetPaginationConfig(array $config): static
{
$new = clone $this;

Check warning on line 592 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "CloneRemoval": --- Original +++ New @@ @@ */ public function keysetPaginationConfig(array $config) : static { - $new = clone $this; + $new = $this; $new->keysetPaginationConfig = $config; return $new; }
$new->keysetPaginationConfig = $config;
return $new;
}
Expand Down Expand Up @@ -777,7 +782,7 @@
*
* @psalm-return array<string, string>
*/
protected function getOverrideOrderFields(): array
protected function getOrderProperties(): array
{
return [];
}
Expand Down Expand Up @@ -838,7 +843,6 @@
}

$context = new PaginationContext(
$this->getOverrideOrderFields(),
$nextUrlPattern,
$previousUrlPattern,
$defaultUrl,
Expand Down Expand Up @@ -931,7 +935,7 @@
$currentPage = $dataReader->getCurrentPage();

// The starting row number (1-based) currently being displayed
$begin = ($currentPage - 1) * $dataReader->getPageSize() + 1;

Check warning on line 938 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": --- Original +++ New @@ @@ // The page number (1-based) current being displayed $currentPage = $dataReader->getCurrentPage(); // The starting row number (1-based) currently being displayed - $begin = ($currentPage - 1) * $dataReader->getPageSize() + 1; + $begin = ($currentPage - 0) * $dataReader->getPageSize() + 1; // The number of rows currently being displayed $count = $dataReader->getCurrentPageSize(); // The ending row number (1-based) currently being displayed

// The number of rows currently being displayed
$count = $dataReader->getCurrentPageSize();
Expand Down Expand Up @@ -1046,7 +1050,7 @@
}

$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
Loading