diff --git a/src/BaseListView.php b/src/BaseListView.php index dffaafb18..3c93484a1 100644 --- a/src/BaseListView.php +++ b/src/BaseListView.php @@ -371,9 +371,13 @@ protected function renderLinkSorter(string $attribute, string $label): string return ''; } - $linkSorter = $dataReader instanceof OffsetPaginator - ? LinkSorter::widget()->currentPage($dataReader->getCurrentPage()) - : LinkSorter::widget(); + if ($dataReader instanceof OffsetPaginator) { + $linkSorter = LinkSorter::widget()->currentPage($dataReader->getCurrentPage()); + } elseif ($dataReader instanceof KeysetPaginator) { + $linkSorter = LinkSorter::widget(); + } else { + return ''; + } return $linkSorter ->attribute($attribute) @@ -419,14 +423,12 @@ private function renderPagination(): string private function renderSummary(): string { - if ($this->getDataReader() instanceof KeysetPaginator) { + $dataReader = $this->getDataReader(); + if (!$dataReader instanceof OffsetPaginator) { return ''; } - /** @var OffsetPaginator $paginator */ - $paginator = $this->getDataReader(); - - $data = iterator_to_array($paginator->read()); + $data = iterator_to_array($dataReader->read()); $pageCount = count($data); if ($pageCount <= 0) { @@ -436,8 +438,8 @@ private function renderSummary(): string $summary = $this->translator->translate( $this->summary ?? 'Page {currentPage} of {totalPages}', [ - 'currentPage' => $paginator->getCurrentPage(), - 'totalPages' => $paginator->getTotalPages(), + 'currentPage' => $dataReader->getCurrentPage(), + 'totalPages' => $dataReader->getTotalPages(), ], $this->translationCategory, ); @@ -458,13 +460,13 @@ private function renderGrid(): string return match ($this->withContainer) { true => trim( - $contentGrid . PHP_EOL . Div::tag() + $contentGrid . "\n" . Div::tag() ->attributes($attributes) - ->content(PHP_EOL . $this->renderGridTable() . PHP_EOL) + ->content("\n" . $this->renderGridTable() . "\n") ->encode(false) ->render() ), - false => trim($contentGrid . PHP_EOL . $this->renderGridTable()), + false => trim($contentGrid . "\n" . $this->renderGridTable()), }; } diff --git a/src/Column/ActionColumnRenderer.php b/src/Column/ActionColumnRenderer.php index 40d1fac31..46b4b85c8 100644 --- a/src/Column/ActionColumnRenderer.php +++ b/src/Column/ActionColumnRenderer.php @@ -13,6 +13,8 @@ use Yiisoft\Yii\DataView\Column\Base\GlobalContext; use Yiisoft\Yii\DataView\Column\Base\DataContext; +use function is_object; + final class ActionColumnRenderer implements ColumnRendererInterface { public function __construct( @@ -78,7 +80,7 @@ function (array $matches) use ($column, $buttons, $context): string { return $cell ->addAttributes($column->bodyAttributes) - ->content(PHP_EOL . $content . PHP_EOL) + ->content("\n" . $content . "\n") ->encode(false); } @@ -103,7 +105,7 @@ private function createUrl(ActionColumn $column, string $action, array|object $d $routeName = $column->routeName; if ($primaryKey !== '') { - $key = $data[$primaryKey] ?? $key; + $key = (is_object($data) ? $data->$primaryKey : $data[$primaryKey]) ?? $key; } $currentRouteName = $this->currentRoute->getName() ?? ''; @@ -112,7 +114,6 @@ private function createUrl(ActionColumn $column, string $action, array|object $d ? $currentRouteName . '/' . $action : $routeName . '/' . $action; - $urlParamsConfig = array_merge( $column->urlParamsConfig, is_array($key) ? $key : [$primaryKey => $key] diff --git a/src/Column/DataColumnRenderer.php b/src/Column/DataColumnRenderer.php index ed3533398..2a20efa38 100644 --- a/src/Column/DataColumnRenderer.php +++ b/src/Column/DataColumnRenderer.php @@ -6,6 +6,7 @@ use InvalidArgumentException; use Yiisoft\Arrays\ArrayHelper; +use Yiisoft\Data\Paginator\KeysetPaginator; use Yiisoft\Data\Paginator\OffsetPaginator; use Yiisoft\Data\Paginator\PaginatorInterface; use Yiisoft\Html\Html; @@ -127,9 +128,13 @@ private function renderLinkSorter(GlobalContext $context, string $property, stri return ''; } - $linkSorter = $dataReader instanceof OffsetPaginator - ? LinkSorter::widget()->currentPage($dataReader->getCurrentPage()) - : LinkSorter::widget(); + if ($dataReader instanceof OffsetPaginator) { + $linkSorter = LinkSorter::widget()->currentPage($dataReader->getCurrentPage()); + } elseif ($dataReader instanceof KeysetPaginator) { + $linkSorter = LinkSorter::widget(); + } else { + return ''; + } return $linkSorter ->attribute($property) diff --git a/src/DetailView.php b/src/DetailView.php index de515571f..099337f63 100644 --- a/src/DetailView.php +++ b/src/DetailView.php @@ -384,7 +384,7 @@ private function renderItems(): string ]); } - return implode(PHP_EOL, $rows); + return implode("\n", $rows); } private function renderValue(string $attribute, mixed $value): mixed @@ -421,6 +421,6 @@ private function renderValue(string $attribute, mixed $value): mixed */ private function removeDoubleLinesBreaks(string $string): string { - return preg_replace("/([\r\n]{4,}|[\n]{2,}|[\r]{2,})/", PHP_EOL, $string); + return preg_replace("/([\r\n]{4,}|[\n]{2,}|[\r]{2,})/", "\n", $string); } } diff --git a/src/GridView.php b/src/GridView.php index 35de36777..4b52fb0d5 100644 --- a/src/GridView.php +++ b/src/GridView.php @@ -528,9 +528,9 @@ protected function renderItems(): string : Html::tbody($this->tbodyAttributes)->rows(...$rows)->render(); return Html::tag('table', attributes: $this->tableAttributes)->open() - . PHP_EOL - . implode(PHP_EOL, $blocks) - . PHP_EOL + . "\n" + . implode("\n", $blocks) + . "\n" . ''; } diff --git a/src/KeysetPagination.php b/src/KeysetPagination.php index 0289d5f6b..b35a2c4cd 100644 --- a/src/KeysetPagination.php +++ b/src/KeysetPagination.php @@ -38,13 +38,13 @@ public function render(): string Nav::tag() ->attributes($attributes) ->content( - PHP_EOL . + "\n" . Menu::widget() ->class($this->getMenuClass()) ->items(array_filter($items)) ->itemsContainerClass($this->getMenuItemContainerClass()) ->linkClass($this->getMenuItemLinkClass()) . - PHP_EOL + "\n" ) ->encode(false) ->render(); diff --git a/src/ListView.php b/src/ListView.php index a13975f01..02b18a7da 100644 --- a/src/ListView.php +++ b/src/ListView.php @@ -206,7 +206,7 @@ protected function renderItem(array|object $data, mixed $key, int $index): strin return Div::tag() ->attributes($this->itemViewAttributes) - ->content(PHP_EOL . $content) + ->content("\n" . $content) ->encode(false) ->render(); } diff --git a/src/OffsetPagination.php b/src/OffsetPagination.php index 630ede894..bb49d4973 100644 --- a/src/OffsetPagination.php +++ b/src/OffsetPagination.php @@ -217,13 +217,13 @@ public function render(): string Nav::tag() ->attributes($attributes) ->content( - PHP_EOL . + "\n" . Menu::widget() ->class($this->getMenuClass()) ->items(array_filter($items)) ->itemsContainerClass($this->getMenuItemContainerClass()) ->linkClass($this->getMenuItemLinkClass()) . - PHP_EOL + "\n" ) ->encode(false) ->render(); diff --git a/tests/Column/ActionColumnTest.php b/tests/Column/ActionColumnTest.php index db97dac94..aa4d796b6 100644 --- a/tests/Column/ActionColumnTest.php +++ b/tests/Column/ActionColumnTest.php @@ -5,6 +5,7 @@ namespace Yiisoft\Yii\DataView\Tests\Column; use PHPUnit\Framework\TestCase; +use Yiisoft\Data\Reader\Iterable\IterableDataReader; use Yiisoft\Definitions\Exception\CircularReferenceException; use Yiisoft\Definitions\Exception\InvalidConfigException; use Yiisoft\Definitions\Exception\NotInstantiableException; @@ -713,4 +714,52 @@ public function testVisibleButtonsClosure(): void ->render() ); } + + public function testObjectsWithPrimaryKey(): void + { + $dataReader = new IterableDataReader([ + new class () { + public int $id = 23; + }, + new class () { + public int $id = 78; + }, + ]); + + $html = GridView::widget() + ->columns(new ActionColumn(primaryKey: 'id')) + ->dataReader($dataReader) + ->render(); + + $this->assertSame( + << +
Actions | +
---|
+ 🔎 + ✎ + ❌ + | +
+ 🔎 + ✎ + ❌ + | +