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

Fix error when used objects with PK in ArrayColumn #141

Merged
merged 3 commits into from
Nov 27, 2023
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
28 changes: 15 additions & 13 deletions src/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@
*
* @param bool $value Whether container is enabled or not.
*/
public function withContainer(bool $value = true): static

Check warning on line 335 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ * * @param bool $value Whether container is enabled or not. */ - public function withContainer(bool $value = true) : static + public function withContainer(bool $value = false) : static { $new = clone $this; $new->withContainer = $value;

Check warning on line 335 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": --- Original +++ New @@ @@ * * @param bool $value Whether container is enabled or not. */ - public function withContainer(bool $value = true) : static + public function withContainer(bool $value = false) : static { $new = clone $this; $new->withContainer = $value;
{
$new = clone $this;
$new->withContainer = $value;
Expand Down Expand Up @@ -371,9 +371,13 @@
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();

Check warning on line 377 in src/BaseListView.php

View check run for this annotation

Codecov / codecov/patch

src/BaseListView.php#L374-L377

Added lines #L374 - L377 were not covered by tests
} else {
return '';

Check warning on line 379 in src/BaseListView.php

View check run for this annotation

Codecov / codecov/patch

src/BaseListView.php#L379

Added line #L379 was not covered by tests
}

return $linkSorter
->attribute($attribute)
Expand All @@ -392,7 +396,7 @@
public function render(): string
{
if ($this->dataReader === null) {
throw new DataReaderNotSetException();

Check warning on line 399 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ public function render() : string { if ($this->dataReader === null) { - throw new DataReaderNotSetException(); + new DataReaderNotSetException(); } return $this->renderGrid(); }

Check warning on line 399 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Throw_": --- Original +++ New @@ @@ public function render() : string { if ($this->dataReader === null) { - throw new DataReaderNotSetException(); + new DataReaderNotSetException(); } return $this->renderGrid(); }
}

return $this->renderGrid();
Expand All @@ -419,14 +423,12 @@

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) {
Expand All @@ -436,8 +438,8 @@
$summary = $this->translator->translate(
$this->summary ?? 'Page <b>{currentPage}</b> of <b>{totalPages}</b>',
[
'currentPage' => $paginator->getCurrentPage(),
'totalPages' => $paginator->getTotalPages(),
'currentPage' => $dataReader->getCurrentPage(),
'totalPages' => $dataReader->getTotalPages(),
],
$this->translationCategory,
);
Expand All @@ -456,15 +458,15 @@
);
}

return match ($this->withContainer) {

Check warning on line 461 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MatchArmRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . "\n" . Div::tag()->attributes($attributes)->content("\n" . $this->renderGridTable() . "\n")->encode(false)->render()), - false => trim($contentGrid . "\n" . $this->renderGridTable()), }; } private function renderGridTable() : string

Check warning on line 461 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "MatchArmRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . "\n" . Div::tag()->attributes($attributes)->content("\n" . $this->renderGridTable() . "\n")->encode(false)->render()), - false => trim($contentGrid . "\n" . $this->renderGridTable()), }; } private function renderGridTable() : string
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()),

Check warning on line 469 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . "\n" . Div::tag()->attributes($attributes)->content("\n" . $this->renderGridTable() . "\n")->encode(false)->render()), - false => trim($contentGrid . "\n" . $this->renderGridTable()), + false => trim("\n" . $contentGrid . $this->renderGridTable()), }; } private function renderGridTable() : string

Check warning on line 469 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . "\n" . Div::tag()->attributes($attributes)->content("\n" . $this->renderGridTable() . "\n")->encode(false)->render()), - false => trim($contentGrid . "\n" . $this->renderGridTable()), + false => trim("\n" . $this->renderGridTable()), }; } private function renderGridTable() : string

Check warning on line 469 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . "\n" . Div::tag()->attributes($attributes)->content("\n" . $this->renderGridTable() . "\n")->encode(false)->render()), - false => trim($contentGrid . "\n" . $this->renderGridTable()), + false => trim($contentGrid . $this->renderGridTable()), }; } private function renderGridTable() : string

Check warning on line 469 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . "\n" . Div::tag()->attributes($attributes)->content("\n" . $this->renderGridTable() . "\n")->encode(false)->render()), - false => trim($contentGrid . "\n" . $this->renderGridTable()), + false => trim($contentGrid . $this->renderGridTable() . "\n"), }; } private function renderGridTable() : string

Check warning on line 469 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . "\n" . Div::tag()->attributes($attributes)->content("\n" . $this->renderGridTable() . "\n")->encode(false)->render()), - false => trim($contentGrid . "\n" . $this->renderGridTable()), + false => trim($contentGrid . "\n"), }; } private function renderGridTable() : string

Check warning on line 469 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapTrim": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . "\n" . Div::tag()->attributes($attributes)->content("\n" . $this->renderGridTable() . "\n")->encode(false)->render()), - false => trim($contentGrid . "\n" . $this->renderGridTable()), + false => $contentGrid . "\n" . $this->renderGridTable(), }; } private function renderGridTable() : string

Check warning on line 469 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . "\n" . Div::tag()->attributes($attributes)->content("\n" . $this->renderGridTable() . "\n")->encode(false)->render()), - false => trim($contentGrid . "\n" . $this->renderGridTable()), + false => trim("\n" . $contentGrid . $this->renderGridTable()), }; } private function renderGridTable() : string

Check warning on line 469 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . "\n" . Div::tag()->attributes($attributes)->content("\n" . $this->renderGridTable() . "\n")->encode(false)->render()), - false => trim($contentGrid . "\n" . $this->renderGridTable()), + false => trim("\n" . $this->renderGridTable()), }; } private function renderGridTable() : string

Check warning on line 469 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . "\n" . Div::tag()->attributes($attributes)->content("\n" . $this->renderGridTable() . "\n")->encode(false)->render()), - false => trim($contentGrid . "\n" . $this->renderGridTable()), + false => trim($contentGrid . $this->renderGridTable()), }; } private function renderGridTable() : string

Check warning on line 469 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "Concat": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . "\n" . Div::tag()->attributes($attributes)->content("\n" . $this->renderGridTable() . "\n")->encode(false)->render()), - false => trim($contentGrid . "\n" . $this->renderGridTable()), + false => trim($contentGrid . $this->renderGridTable() . "\n"), }; } private function renderGridTable() : string

Check warning on line 469 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ConcatOperandRemoval": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . "\n" . Div::tag()->attributes($attributes)->content("\n" . $this->renderGridTable() . "\n")->encode(false)->render()), - false => trim($contentGrid . "\n" . $this->renderGridTable()), + false => trim($contentGrid . "\n"), }; } private function renderGridTable() : string

Check warning on line 469 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "UnwrapTrim": --- Original +++ New @@ @@ } return match ($this->withContainer) { true => trim($contentGrid . "\n" . Div::tag()->attributes($attributes)->content("\n" . $this->renderGridTable() . "\n")->encode(false)->render()), - false => trim($contentGrid . "\n" . $this->renderGridTable()), + false => $contentGrid . "\n" . $this->renderGridTable(), }; } private function renderGridTable() : string
};
}

Expand All @@ -475,7 +477,7 @@
$this->layoutGridTable,
[
'{header}' => $this->renderHeader(),
'{toolbar}' => $this->toolbar,

Check warning on line 480 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ArrayItem": --- Original +++ New @@ @@ } private function renderGridTable() : string { - return trim(strtr($this->layoutGridTable, ['{header}' => $this->renderHeader(), '{toolbar}' => $this->toolbar, '{items}' => $this->renderItems(), '{summary}' => $this->renderSummary(), '{pager}' => $this->renderPagination()])); + return trim(strtr($this->layoutGridTable, ['{header}' => $this->renderHeader(), '{toolbar}' > $this->toolbar, '{items}' => $this->renderItems(), '{summary}' => $this->renderSummary(), '{pager}' => $this->renderPagination()])); } private function renderHeader() : string {

Check warning on line 480 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.1-ubuntu-latest

Escaped Mutant for Mutator "ArrayItem": --- Original +++ New @@ @@ } private function renderGridTable() : string { - return trim(strtr($this->layoutGridTable, ['{header}' => $this->renderHeader(), '{toolbar}' => $this->toolbar, '{items}' => $this->renderItems(), '{summary}' => $this->renderSummary(), '{pager}' => $this->renderPagination()])); + return trim(strtr($this->layoutGridTable, ['{header}' => $this->renderHeader(), '{toolbar}' > $this->toolbar, '{items}' => $this->renderItems(), '{summary}' => $this->renderSummary(), '{pager}' => $this->renderPagination()])); } private function renderHeader() : string {
'{items}' => $this->renderItems(),
'{summary}' => $this->renderSummary(),
'{pager}' => $this->renderPagination(),
Expand Down
7 changes: 4 additions & 3 deletions src/Column/ActionColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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);
}

Expand All @@ -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() ?? '';
Expand All @@ -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]
Expand Down
11 changes: 8 additions & 3 deletions src/Column/DataColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -127,9 +128,13 @@
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 '';

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

View check run for this annotation

Codecov / codecov/patch

src/Column/DataColumnRenderer.php#L136

Added line #L136 was not covered by tests
}

return $linkSorter
->attribute($property)
Expand Down
4 changes: 2 additions & 2 deletions src/DetailView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
6 changes: 3 additions & 3 deletions src/GridView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"
. '</table>';
}

Expand Down
4 changes: 2 additions & 2 deletions src/KeysetPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/ListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/OffsetPagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
49 changes: 49 additions & 0 deletions tests/Column/ActionColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
<<<HTML
<div>
<table>
<thead>
<tr>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<a name="view" href="/admin/manage/view?id=23" title="View" role="button" style="text-decoration: none!important;"><span>🔎</span></a>
<a name="update" href="/admin/manage/update?id=23" title="Update" role="button" style="text-decoration: none!important;"><span>✎</span></a>
<a name="delete" href="/admin/manage/delete?id=23" title="Delete" role="button" style="text-decoration: none!important;"><span>❌</span></a>
</td>
</tr>
<tr>
<td>
<a name="view" href="/admin/manage/view?id=78" title="View" role="button" style="text-decoration: none!important;"><span>🔎</span></a>
<a name="update" href="/admin/manage/update?id=78" title="Update" role="button" style="text-decoration: none!important;"><span>✎</span></a>
<a name="delete" href="/admin/manage/delete?id=78" title="Delete" role="button" style="text-decoration: none!important;"><span>❌</span></a>
</td>
</tr>
</tbody>
</table>
</div>
HTML,
$html
);
}
}