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 queryProperty to field in DataColumn #182

Merged
merged 4 commits into from
Apr 4, 2024
Merged
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"ext-json": "*",
"ext-mbstring": "*",
"psr/container": "^1.0|^2.0",
"yiisoft/arrays": "^2.0|^3.0",
"yiisoft/arrays": "^3.1",
"yiisoft/data": "dev-master",
"yiisoft/factory": "^1.0",
"yiisoft/friendly-exception": "^1.0",
Expand Down
14 changes: 13 additions & 1 deletion src/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Yiisoft\Data\Reader\LimitableDataInterface;
use Yiisoft\Data\Reader\OffsetableDataInterface;
use Yiisoft\Data\Reader\ReadableDataInterface;
use Yiisoft\Data\Reader\Sort;
use Yiisoft\Data\Reader\SortableDataInterface;
use Yiisoft\Html\Html;
use Yiisoft\Html\Tag\Div;
Expand All @@ -30,12 +31,14 @@
use Yiisoft\Translator\TranslatorInterface;
use Yiisoft\Validator\Result as ValidationResult;
use Yiisoft\Widget\Widget;
use Yiisoft\Data\Reader\OrderHelper;
use Yiisoft\Yii\DataView\Exception\DataReaderNotSetException;

/**
* @psalm-type UrlArguments = array<string,scalar|Stringable|null>
* @psalm-type UrlCreator = callable(UrlArguments,array):string
* @psalm-type PageNotFoundExceptionCallback = callable(PageNotFoundException):void
* @psalm-import-type TOrder from Sort
*/
abstract class BaseListView extends Widget
{
Expand Down Expand Up @@ -253,7 +256,7 @@
*
* @psalm-return list{FilterInterface[]|null,ValidationResult}
*/
protected function makeFilters(): array

Check warning on line 259 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 @@ -331,7 +334,7 @@

if (!$dataReader instanceof PaginatorInterface) {
if (
$dataReader instanceof OffsetableDataInterface

Check warning on line 337 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 = $this->getDataReader(); if (!$dataReader instanceof PaginatorInterface) { - if ($dataReader instanceof OffsetableDataInterface && $dataReader instanceof CountableDataInterface && $dataReader instanceof LimitableDataInterface) { + if (($dataReader instanceof OffsetableDataInterface || $dataReader instanceof CountableDataInterface) && $dataReader instanceof LimitableDataInterface) { $dataReader = new OffsetPaginator($dataReader); } elseif ($dataReader instanceof FilterableDataInterface && $dataReader instanceof SortableDataInterface && $dataReader instanceof LimitableDataInterface) { if ($dataReader->getSort() !== null) {
&& $dataReader instanceof CountableDataInterface
&& $dataReader instanceof LimitableDataInterface
) {
Expand All @@ -351,7 +354,7 @@
}
}

if ($dataReader->isPaginationRequired()) {

Check warning on line 357 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "IfNegation": --- Original +++ New @@ @@ return $dataReader; } } - if ($dataReader->isPaginationRequired()) { + if (!$dataReader->isPaginationRequired()) { if ($pageSize !== null) { $dataReader = $dataReader->withPageSize((int) $pageSize); }
if ($pageSize !== null) {
$dataReader = $dataReader->withPageSize((int) $pageSize);
}
Expand All @@ -363,20 +366,29 @@
}
}

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

Check warning on line 369 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 ($dataReader->isSortable() && !empty($sort)) { + if (!$dataReader->isSortable() && !empty($sort)) { $sortObject = $dataReader->getSort(); if ($sortObject !== null) { $order = OrderHelper::stringToArray($sort);
$sortObject = $dataReader->getSort();
if ($sortObject !== null) {
$dataReader = $dataReader->withSort($sortObject->withOrderString($sort));
$order = OrderHelper::stringToArray($sort);
$this->prepareOrder($order);
$dataReader = $dataReader->withSort($sortObject->withOrder($order));

Check warning on line 374 in src/BaseListView.php

View check run for this annotation

Codecov / codecov/patch

src/BaseListView.php#L372-L374

Added lines #L372 - L374 were not covered by tests
}
}

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

Check warning on line 378 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($order)); } } - if ($dataReader->isFilterable() && !empty($filters)) { + if ($dataReader->isFilterable() || !empty($filters)) { $dataReader = $dataReader->withFilter(new All(...$filters)); } return $dataReader;

Check warning on line 378 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($order)); } } - if ($dataReader->isFilterable() && !empty($filters)) { + if (!($dataReader->isFilterable() && !empty($filters))) { $dataReader = $dataReader->withFilter(new All(...$filters)); } return $dataReader;

Check warning on line 378 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($order)); } } - if ($dataReader->isFilterable() && !empty($filters)) { + if (!$dataReader->isFilterable() && !empty($filters)) { $dataReader = $dataReader->withFilter(new All(...$filters)); } return $dataReader;
$dataReader = $dataReader->withFilter(new All(...$filters));
}

return $dataReader;
}

/**
* @psalm-param TOrder $order
*/
protected function prepareOrder(array &$order): void

Check warning on line 388 in src/BaseListView.php

View check run for this annotation

Codecov / codecov/patch

src/BaseListView.php#L388

Added line #L388 was not covered by tests
{
}

Check warning on line 390 in src/BaseListView.php

View check run for this annotation

Codecov / codecov/patch

src/BaseListView.php#L390

Added line #L390 was not covered by tests

/**
* Return new instance with the header for the grid.
*
Expand Down Expand Up @@ -652,7 +664,7 @@
$currentPage = $dataReader->getCurrentPage();

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

Check warning on line 667 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

Check warning on line 667 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": --- 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 - 2) * $dataReader->getPageSize() + 1; // The number of rows currently being displayed $count = $dataReader->getCurrentPageSize(); // The ending row number (1-based) currently being displayed

Check warning on line 667 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "Minus": --- 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 + 1) * $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
24 changes: 18 additions & 6 deletions src/Column/Base/HeaderContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
namespace Yiisoft\Yii\DataView\Column\Base;

use Stringable;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Data\Paginator\PageToken;
use Yiisoft\Data\Reader\Sort;
use Yiisoft\Html\Html;
use Yiisoft\Html\Tag\A;
use Yiisoft\Translator\TranslatorInterface;
use Yiisoft\Yii\DataView\BaseListView;
use Yiisoft\Data\Reader\OrderHelper;
use Yiisoft\Yii\DataView\UrlConfig;
use Yiisoft\Yii\DataView\UrlParametersFactory;

Expand All @@ -22,11 +24,13 @@
/**
* @internal
*
* @psalm-param array<string,string> $overrideOrderFields
* @psalm-param UrlCreator|null $urlCreator
*/
public function __construct(
private readonly ?Sort $originalSort,
private readonly ?Sort $sort,
private readonly array $overrideOrderFields,
private readonly ?string $sortableHeaderClass,
private string|Stringable $sortableHeaderPrepend,
private string|Stringable $sortableHeaderAppend,
Expand Down Expand Up @@ -59,6 +63,8 @@
*/
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)) {
return [$cell, null, '', ''];
}
Expand All @@ -85,7 +91,7 @@
UrlParametersFactory::create(
$this->pageToken,
$this->pageSize,
$this->getLinkSortValue($this->originalSort, $this->sort, $property),
$this->getLinkSortValue($this->originalSort, $this->sort, $property, $originalProperty),

Check warning on line 94 in src/Column/Base/HeaderContext.php

View check run for this annotation

Codecov / codecov/patch

src/Column/Base/HeaderContext.php#L94

Added line #L94 was not covered by tests
$this->urlConfig,
)
);
Expand All @@ -98,8 +104,12 @@
];
}

private function getLinkSortValue(Sort $originalSort, Sort $sort, string $property): ?string
{
private function getLinkSortValue(

Check warning on line 107 in src/Column/Base/HeaderContext.php

View check run for this annotation

Codecov / codecov/patch

src/Column/Base/HeaderContext.php#L107

Added line #L107 was not covered by tests
Sort $originalSort,
Sort $sort,
string $property,
string $originalProperty
): ?string {
$originalOrder = $originalSort->getOrder();
$order = $sort->getOrder();

Expand Down Expand Up @@ -141,12 +151,14 @@
return null;
}

$result = $sort->withOrder($order)->getOrderAsString();
if (empty($result)) {
$resultOrder = $sort->withOrder($order)->getOrder();
if (empty($resultOrder)) {

Check warning on line 155 in src/Column/Base/HeaderContext.php

View check run for this annotation

Codecov / codecov/patch

src/Column/Base/HeaderContext.php#L154-L155

Added lines #L154 - L155 were not covered by tests
return null;
}

return $result;
return OrderHelper::arrayToString(
ArrayHelper::renameKey($resultOrder, $property, $originalProperty)
);

Check warning on line 161 in src/Column/Base/HeaderContext.php

View check run for this annotation

Codecov / codecov/patch

src/Column/Base/HeaderContext.php#L159-L161

Added lines #L159 - L161 were not covered by tests
}

private function isEqualOrders(array $a, array $b): bool
Expand Down
5 changes: 1 addition & 4 deletions src/Column/DataColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
*/
final class DataColumn implements ColumnInterface
{
public readonly ?string $queryProperty;

/**
* @var bool|callable|null
* @psalm-var bool|FilterEmptyCallable|null
Expand All @@ -39,7 +37,7 @@ final class DataColumn implements ColumnInterface
*/
public function __construct(
public readonly ?string $property = null,
?string $queryProperty = null,
public readonly ?string $field = null,
public readonly ?string $header = null,
public readonly bool $encodeHeader = true,
public readonly ?string $footer = null,
Expand All @@ -55,7 +53,6 @@ public function __construct(
bool|callable|null $filterEmpty = null,
private readonly bool $visible = true,
) {
$this->queryProperty = $queryProperty ?? $this->property;
$this->filterEmpty = $filterEmpty;
}

Expand Down
36 changes: 25 additions & 11 deletions src/Column/DataColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/**
* @psalm-import-type FilterEmptyCallable from DataColumn
*/
final class DataColumnRenderer implements FilterableColumnRendererInterface
final class DataColumnRenderer implements FilterableColumnRendererInterface, OverrideOrderFieldsColumnInterface
{
/**
* @var bool|callable
Expand Down Expand Up @@ -72,11 +72,11 @@
}
$cell = $cell->content($label);

if (!$column->withSorting || $column->queryProperty === null) {
if (!$column->withSorting || $column->property === null) {
return $cell;
}

[$cell, $link, $prepend, $append] = $context->prepareSortable($cell, $column->queryProperty);
[$cell, $link, $prepend, $append] = $context->prepareSortable($cell, $column->property);
if ($link !== null) {
$link = $link->content($label)->encode(false);
}
Expand All @@ -88,7 +88,7 @@
{
$this->checkColumn($column);

if ($column->queryProperty === null || $column->filter === false) {
if ($column->property === null || $column->filter === false) {
return null;
}

Expand All @@ -103,14 +103,14 @@
$content = [
$widget->withContext(
new Context(
$column->queryProperty,
$context->getQueryValue($column->queryProperty),
$column->property,
$context->getQueryValue($column->property),

Check warning on line 107 in src/Column/DataColumnRenderer.php

View check run for this annotation

Codecov / codecov/patch

src/Column/DataColumnRenderer.php#L106-L107

Added lines #L106 - L107 were not covered by tests
$context->formId
)
),
];

$errors = $context->validationResult->getAttributeErrorMessages($column->queryProperty);
$errors = $context->validationResult->getAttributeErrorMessages($column->property);

Check warning on line 113 in src/Column/DataColumnRenderer.php

View check run for this annotation

Codecov / codecov/patch

src/Column/DataColumnRenderer.php#L113

Added line #L113 was not covered by tests
if (!empty($errors)) {
$cell = $cell->addClass($context->cellInvalidClass);
$content[] = Html::div(attributes: $context->errorsContainerAttributes)
Expand All @@ -123,11 +123,11 @@
public function makeFilter(ColumnInterface $column, MakeFilterContext $context): ?FilterInterface
{
$this->checkColumn($column);
if ($column->queryProperty === null) {
if ($column->property === null) {
return null;
}

$value = $context->getQueryValue($column->queryProperty);
$value = $context->getQueryValue($column->property);
if ($value === null) {
return null;
}
Expand All @@ -144,7 +144,7 @@
$context->validationResult->addError(
$error->getMessage(),
$error->getParameters(),
[$column->queryProperty]
[$column->property]

Check warning on line 147 in src/Column/DataColumnRenderer.php

View check run for this annotation

Codecov / codecov/patch

src/Column/DataColumnRenderer.php#L147

Added line #L147 was not covered by tests
);
}
return null;
Expand All @@ -163,7 +163,7 @@
$factory = $column->filterFactory;
}

return $factory->create($column->queryProperty, $value);
return $factory->create($column->field ?? $column->property, $value);

Check warning on line 166 in src/Column/DataColumnRenderer.php

View check run for this annotation

Codecov / codecov/patch

src/Column/DataColumnRenderer.php#L166

Added line #L166 was not covered by tests
}

public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell
Expand Down Expand Up @@ -244,4 +244,18 @@
);
}
}

public function getOverrideOrderFields(ColumnInterface $column): array
{
$this->checkColumn($column);

if ($column->property === null
|| $column->field === null
|| $column->property === $column->field
) {
return [];
}

return [$column->property => $column->field];

Check warning on line 259 in src/Column/DataColumnRenderer.php

View check run for this annotation

Codecov / codecov/patch

src/Column/DataColumnRenderer.php#L259

Added line #L259 was not covered by tests
}
}
13 changes: 13 additions & 0 deletions src/Column/OverrideOrderFieldsColumnInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\DataView\Column;

interface OverrideOrderFieldsColumnInterface
{
/**
* @return array<string,string>
*/
public function getOverrideOrderFields(ColumnInterface $column): array;
}
23 changes: 23 additions & 0 deletions src/GridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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 @@ -25,6 +26,7 @@
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\Filter\Factory\IncorrectValueException;

/**
Expand Down Expand Up @@ -531,10 +533,18 @@
$blocks[] = Html::colgroup()->columns(...$tags)->render();
}

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

if ($this->headerTableEnabled) {
$headerContext = new HeaderContext(
$this->getSort($dataReader),
$this->getSort($this->preparedDataReader),
$overrideOrderFields,
$this->sortableHeaderClass,
$this->sortableHeaderPrepend,
$this->sortableHeaderAppend,
Expand Down Expand Up @@ -665,6 +675,19 @@
return [$filters, $validationResult];
}

protected function prepareOrder(array &$order): void

Check warning on line 678 in src/GridView.php

View check run for this annotation

Codecov / codecov/patch

src/GridView.php#L678

Added line #L678 was not covered by tests
{
$columns = $this->getColumns();
$renderers = $this->getColumnRenderers();
foreach ($columns as $i => $column) {
if ($renderers[$i] instanceof OverrideOrderFieldsColumnInterface) {
foreach ($renderers[$i]->getOverrideOrderFields($column) as $from => $to) {
$order = ArrayHelper::renameKey($order, $from, $to);

Check warning on line 685 in src/GridView.php

View check run for this annotation

Codecov / codecov/patch

src/GridView.php#L680-L685

Added lines #L680 - L685 were not covered by tests
}
}
}
}

private function prepareBodyAttributes(array $attributes, DataContext $context): array
{
foreach ($attributes as $i => $attribute) {
Expand Down
Loading