Skip to content

Commit

Permalink
Replace checkColumn() methods in column renderers to generic (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik authored Jan 22, 2025
1 parent b305e70 commit 738e585
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 118 deletions.
28 changes: 5 additions & 23 deletions src/Column/ActionColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
namespace Yiisoft\Yii\DataView\Column;

use Closure;
use InvalidArgumentException;
use Yiisoft\Html\Html;
use Yiisoft\Yii\DataView\Column\Base\Cell;
use Yiisoft\Yii\DataView\Column\Base\GlobalContext;
use Yiisoft\Yii\DataView\Column\Base\DataContext;
use Yiisoft\Yii\DataView\Column\Base\HeaderContext;

use function is_bool;
use function is_callable;

/**
* @implements ColumnRendererInterface<ActionColumn>
*
* @psalm-import-type UrlCreator from ActionColumn
* @psalm-import-type ButtonRenderer from ActionColumn
*/
Expand Down Expand Up @@ -53,22 +57,18 @@ public function __construct(

public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell
{
$this->checkColumn($column);
return $cell->addAttributes($column->columnAttributes);
}

public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext $context): Cell
{
$this->checkColumn($column);
return $cell
->content($column->header ?? $context->translate('Actions'))
->addAttributes($column->headerAttributes);
}

public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell
{
$this->checkColumn($column);

$contentSource = $column->content;

if ($contentSource !== null) {
Expand Down Expand Up @@ -114,8 +114,6 @@ function (array $matches) use ($column, $buttons, $context): string {

public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell
{
$this->checkColumn($column);

if ($column->footer !== null) {
$cell = $cell->content($column->footer);
}
Expand Down Expand Up @@ -238,20 +236,4 @@ private function getTemplate(ActionColumn $column, array $buttons): string

return implode("\n", $tokens);
}

/**
* @psalm-assert ActionColumn $column
*/
private function checkColumn(ColumnInterface $column): void
{
if (!$column instanceof ActionColumn) {
throw new InvalidArgumentException(
sprintf(
'Expected "%s", but "%s" given.',
self::class,
$column::class
)
);
}
}
}
27 changes: 3 additions & 24 deletions src/Column/CheckboxColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,25 @@

namespace Yiisoft\Yii\DataView\Column;

use InvalidArgumentException;
use Stringable;
use Yiisoft\Html\Html;
use Yiisoft\Yii\DataView\Column\Base\Cell;
use Yiisoft\Yii\DataView\Column\Base\DataContext;
use Yiisoft\Yii\DataView\Column\Base\GlobalContext;
use Yiisoft\Yii\DataView\Column\Base\HeaderContext;

/**
* @implements ColumnRendererInterface<CheckboxColumn>
*/
final class CheckboxColumnRenderer implements ColumnRendererInterface
{
public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell
{
$this->checkColumn($column);
return $cell->addAttributes($column->columnAttributes);
}

public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext $context): ?Cell
{
$this->checkColumn($column);

$header = $column->header;
if ($header === null) {
if (!$column->multiple) {
Expand All @@ -39,8 +38,6 @@ public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext

public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell
{
$this->checkColumn($column);

$inputAttributes = $column->inputAttributes;
$name = null;
$value = null;
Expand All @@ -67,28 +64,10 @@ public function renderBody(ColumnInterface $column, Cell $cell, DataContext $con

public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell
{
$this->checkColumn($column);

if ($column->footer !== null) {
$cell = $cell->content($column->footer);
}

return $cell;
}

/**
* @psalm-assert CheckboxColumn $column
*/
private function checkColumn(ColumnInterface $column): void
{
if (!$column instanceof CheckboxColumn) {
throw new InvalidArgumentException(
sprintf(
'Expected "%s", but "%s" given.',
CheckboxColumn::class,
$column::class
)
);
}
}
}
14 changes: 14 additions & 0 deletions src/Column/ColumnRendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,28 @@

/**
* An interface for column renderers that implement {@see ColumnInterface}.
*
* @template TColumn as ColumnInterface
*/
interface ColumnRendererInterface
{
/**
* @psalm-param TColumn $column
*/
public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell;

/**
* @psalm-param TColumn $column
*/
public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext $context): ?Cell;

/**
* @psalm-param TColumn $column
*/
public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell;

/**
* @psalm-param TColumn $column
*/
public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell;
}
28 changes: 4 additions & 24 deletions src/Column/DataColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Yiisoft\Yii\DataView\Column;

use DateTimeInterface;
use InvalidArgumentException;
use Psr\Container\ContainerInterface;
use Yiisoft\Arrays\ArrayHelper;
use Yiisoft\Data\Reader\FilterInterface;
Expand All @@ -31,6 +30,10 @@
use function is_string;

/**
* @template TColumn as DataColumn
* @implements FilterableColumnRendererInterface<TColumn>
* @implements SortableColumnInterface<TColumn>
*
* @psalm-import-type FilterEmptyCallable from DataColumn
*/
final class DataColumnRenderer implements FilterableColumnRendererInterface, SortableColumnInterface
Expand All @@ -57,7 +60,6 @@ public function __construct(

public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell
{
$this->checkColumn($column);
/** @var DataColumn $column This annotation is for IDE only */

return $cell
Expand All @@ -67,7 +69,6 @@ public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext

public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext $context): Cell
{
$this->checkColumn($column);
/** @var DataColumn $column This annotation is for IDE only */

$cell = $cell
Expand Down Expand Up @@ -96,7 +97,6 @@ public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext

public function renderFilter(ColumnInterface $column, Cell $cell, FilterContext $context): ?Cell
{
$this->checkColumn($column);
/** @var DataColumn $column This annotation is for IDE only */

if ($column->property === null || $column->filter === false) {
Expand Down Expand Up @@ -133,7 +133,6 @@ public function renderFilter(ColumnInterface $column, Cell $cell, FilterContext

public function makeFilter(ColumnInterface $column, MakeFilterContext $context): ?FilterInterface
{
$this->checkColumn($column);
/** @var DataColumn $column This annotation is for IDE only */

if ($column->property === null) {
Expand Down Expand Up @@ -181,7 +180,6 @@ public function makeFilter(ColumnInterface $column, MakeFilterContext $context):

public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell
{
$this->checkColumn($column);
/** @var DataColumn $column This annotation is for IDE only */

$contentSource = $column->content;
Expand Down Expand Up @@ -215,7 +213,6 @@ public function renderBody(ColumnInterface $column, Cell $cell, DataContext $con

public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell
{
$this->checkColumn($column);
/** @var DataColumn $column This annotation is for IDE only */

if ($column->footer !== null) {
Expand Down Expand Up @@ -255,25 +252,8 @@ private function castToString(mixed $value, DataColumn $column): string
return (string) $value;
}

/**
* @psalm-assert DataColumn $column
*/
private function checkColumn(ColumnInterface $column): void
{
if (!$column instanceof DataColumn) {
throw new InvalidArgumentException(
sprintf(
'Expected "%s", but "%s" given.',
DataColumn::class,
$column::class
)
);
}
}

public function getOrderProperties(ColumnInterface $column): array
{
$this->checkColumn($column);
/** @var DataColumn $column This annotation is for IDE only */

if ($column->property === null) {
Expand Down
9 changes: 9 additions & 0 deletions src/Column/FilterableColumnRendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@
use Yiisoft\Yii\DataView\Column\Base\MakeFilterContext;
use Yiisoft\Yii\DataView\Filter\Factory\IncorrectValueException;

/**
* @template TColumn as ColumnInterface
* @extends ColumnRendererInterface<TColumn>
*/
interface FilterableColumnRendererInterface extends ColumnRendererInterface
{
/**
* @psalm-param TColumn $column
*/
public function renderFilter(ColumnInterface $column, Cell $cell, FilterContext $context): ?Cell;

/**
* @throws IncorrectValueException
*
* @psalm-param TColumn $column
*/
public function makeFilter(ColumnInterface $column, MakeFilterContext $context): ?FilterInterface;
}
29 changes: 5 additions & 24 deletions src/Column/RadioColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@

namespace Yiisoft\Yii\DataView\Column;

use InvalidArgumentException;
use Stringable;
use Yiisoft\Html\Html;
use Yiisoft\Yii\DataView\Column\Base\Cell;
use Yiisoft\Yii\DataView\Column\Base\DataContext;
use Yiisoft\Yii\DataView\Column\Base\GlobalContext;
use Yiisoft\Yii\DataView\Column\Base\HeaderContext;

use function array_key_exists;

/**
* @implements ColumnRendererInterface<RadioColumn>
*/
final class RadioColumnRenderer implements ColumnRendererInterface
{
public function renderColumn(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell
{
$this->checkColumn($column);
return $cell->addAttributes($column->columnAttributes);
}

public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext $context): ?Cell
{
$this->checkColumn($column);

$header = $column->header;
if ($header === null) {
return null;
Expand All @@ -36,8 +37,6 @@ public function renderHeader(ColumnInterface $column, Cell $cell, HeaderContext

public function renderBody(ColumnInterface $column, Cell $cell, DataContext $context): Cell
{
$this->checkColumn($column);

$inputAttributes = $column->inputAttributes;
$name = null;
$value = null;
Expand All @@ -64,28 +63,10 @@ public function renderBody(ColumnInterface $column, Cell $cell, DataContext $con

public function renderFooter(ColumnInterface $column, Cell $cell, GlobalContext $context): Cell
{
$this->checkColumn($column);

if ($column->footer !== null) {
$cell = $cell->content($column->footer);
}

return $cell;
}

/**
* @psalm-assert RadioColumn $column
*/
private function checkColumn(ColumnInterface $column): void
{
if (!$column instanceof RadioColumn) {
throw new InvalidArgumentException(
sprintf(
'Expected "%s", but "%s" given.',
RadioColumn::class,
$column::class
)
);
}
}
}
Loading

0 comments on commit 738e585

Please sign in to comment.