Skip to content

Commit

Permalink
Replace queryProperty to field in DataColumn (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Apr 4, 2024
1 parent 70933b9 commit 1fb609b
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 23 deletions.
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 @@ -366,7 +369,9 @@ private function prepareDataReaderByParams(
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));
}
}

Expand All @@ -377,6 +382,13 @@ private function prepareDataReaderByParams(
return $dataReader;
}

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

/**
* Return new instance with the header for the grid.
*
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 @@ final class HeaderContext
/**
* @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 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)) {
return [$cell, null, '', ''];
}
Expand All @@ -85,7 +91,7 @@ public function prepareSortable(Cell $cell, string $property): array
UrlParametersFactory::create(
$this->pageToken,
$this->pageSize,
$this->getLinkSortValue($this->originalSort, $this->sort, $property),
$this->getLinkSortValue($this->originalSort, $this->sort, $property, $originalProperty),
$this->urlConfig,
)
);
Expand All @@ -98,8 +104,12 @@ public function prepareSortable(Cell $cell, string $property): array
];
}

private function getLinkSortValue(Sort $originalSort, Sort $sort, string $property): ?string
{
private function getLinkSortValue(
Sort $originalSort,
Sort $sort,
string $property,
string $originalProperty
): ?string {
$originalOrder = $originalSort->getOrder();
$order = $sort->getOrder();

Expand Down Expand Up @@ -141,12 +151,14 @@ private function getLinkSortValue(Sort $originalSort, Sort $sort, string $proper
return null;
}

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

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

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 @@ public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext
}
$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 @@ public function renderFilter(ColumnInterface $column, Cell $cell, FilterContext
{
$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 @@ public function renderFilter(ColumnInterface $column, Cell $cell, FilterContext
$content = [
$widget->withContext(
new Context(
$column->queryProperty,
$context->getQueryValue($column->queryProperty),
$column->property,
$context->getQueryValue($column->property),
$context->formId
)
),
];

$errors = $context->validationResult->getAttributeErrorMessages($column->queryProperty);
$errors = $context->validationResult->getAttributeErrorMessages($column->property);
if (!empty($errors)) {
$cell = $cell->addClass($context->cellInvalidClass);
$content[] = Html::div(attributes: $context->errorsContainerAttributes)
Expand All @@ -123,11 +123,11 @@ public function renderFilter(ColumnInterface $column, Cell $cell, FilterContext
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 @@ public function makeFilter(ColumnInterface $column, MakeFilterContext $context):
$context->validationResult->addError(
$error->getMessage(),
$error->getParameters(),
[$column->queryProperty]
[$column->property]
);
}
return null;
Expand All @@ -163,7 +163,7 @@ public function makeFilter(ColumnInterface $column, MakeFilterContext $context):
$factory = $column->filterFactory;
}

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

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

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];
}
}
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 @@ 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 = 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 @@ protected function makeFilters(): array
return [$filters, $validationResult];
}

protected function prepareOrder(array &$order): void
{
$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);
}
}
}
}

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

0 comments on commit 1fb609b

Please sign in to comment.