Skip to content

Fix #270: Add BaseListView::containerClass() and BaseListView::addContainerClass() #271

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

Merged
merged 2 commits into from
Mar 31, 2025
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
34 changes: 34 additions & 0 deletions src/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Yiisoft\Yii\DataView;

use BackedEnum;
use InvalidArgumentException;
use Stringable;
use Yiisoft\Data\Paginator\InvalidPageException;
Expand Down Expand Up @@ -217,7 +218,7 @@
return $new;
}

final public function multiSort(bool $enable = true): static

Check warning on line 221 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "TrueValue": @@ @@ $new->urlParameterProvider = $provider; return $new; } - final public function multiSort(bool $enable = true): static + final public function multiSort(bool $enable = false): static { $new = clone $this; $new->multiSort = $enable;
{
$new = clone $this;
$new->multiSort = $enable;
Expand Down Expand Up @@ -592,7 +593,7 @@
*
* @psalm-return list{FilterInterface[]|null,ValidationResult}
*/
protected function makeFilters(): array

Check warning on line 596 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "ProtectedVisibility": @@ @@ * * @psalm-return list{FilterInterface[]|null,ValidationResult} */ - protected function makeFilters(): array + private function makeFilters(): array { return [[], new ValidationResult()]; }
{
return [[], new ValidationResult()];
}
Expand Down Expand Up @@ -761,7 +762,7 @@
}
}

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

Check warning on line 765 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": @@ @@ $dataReader = $dataReader->withToken(PageToken::previous($previousPage)); } } - if (!empty($sort) && $dataReader->isSortable()) { + if (!empty($sort) && !$dataReader->isSortable()) { $sortObject = $dataReader->getSort(); if ($sortObject !== null) { $order = OrderHelper::stringToArray($sort);
$sortObject = $dataReader->getSort();
if ($sortObject !== null) {
$order = OrderHelper::stringToArray($sort);
Expand All @@ -776,7 +777,7 @@
}
}

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

Check warning on line 780 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndSingleSubExprNegation": @@ @@ $dataReader = $dataReader->withSort($sortObject->withOrder($this->prepareOrder($order))); } } - if (!empty($filters) && $dataReader->isFilterable()) { + if (!empty($filters) && !$dataReader->isFilterable()) { $dataReader = $dataReader->withFilter(new All(...$filters)); } return $dataReader;

Check warning on line 780 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "LogicalAndNegation": @@ @@ $dataReader = $dataReader->withSort($sortObject->withOrder($this->prepareOrder($order))); } } - if (!empty($filters) && $dataReader->isFilterable()) { + if (!(!empty($filters) && $dataReader->isFilterable())) { $dataReader = $dataReader->withFilter(new All(...$filters)); } return $dataReader;

Check warning on line 780 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "LogicalAnd": @@ @@ $dataReader = $dataReader->withSort($sortObject->withOrder($this->prepareOrder($order))); } } - if (!empty($filters) && $dataReader->isFilterable()) { + if (!empty($filters) || $dataReader->isFilterable()) { $dataReader = $dataReader->withFilter(new All(...$filters)); } return $dataReader;
$dataReader = $dataReader->withFilter(new All(...$filters));
}

Expand Down Expand Up @@ -920,7 +921,7 @@
$currentPage = $dataReader->getCurrentPage();

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

Check warning on line 924 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "Multiplication": @@ @@ // 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

Check warning on line 924 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "Minus": @@ @@ // 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

Check warning on line 924 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "IncrementInteger": @@ @@ // 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 924 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.4-ubuntu-latest

Escaped Mutant for Mutator "DecrementInteger": @@ @@ // 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

// The number of rows currently being displayed
$count = $dataReader->getCurrentPageSize();
Expand Down Expand Up @@ -1144,4 +1145,37 @@
$new->urlConfig = $this->urlConfig->withSortParameterType($type);
return $new;
}

/**
* Set new container classes.
*
* Multiple classes can be set by passing them as separate arguments. `null` values are filtered out
* automatically.
*
* @param BackedEnum|string|null ...$class One or more CSS class names to use. Pass `null` to skip a class.
* @return self
*/
public function containerClass(BackedEnum|string|null ...$class): self
{
$new = clone $this;
$new->containerAttributes['class'] = [];
Html::addCssClass($new->containerAttributes, $class);
return $new;
}

/**
* Adds one or more CSS classes to the existing container classes.
*
* Multiple classes can be added by passing them as separate arguments. `null` values are filtered out
* automatically.
*
* @param BackedEnum|string|null ...$class One or more CSS class names to add. Pass `null` to skip adding a class.
* @return self A new instance with the specified CSS classes added to existing ones.
*/
public function addContainerClass(BackedEnum|string|null ...$class): self
{
$new = clone $this;
Html::addCssClass($new->containerAttributes, $class);
return $new;
}
}
52 changes: 52 additions & 0 deletions tests/ListView/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -557,4 +557,56 @@ public function testIgnoreMissingPageFalse(): void

$listView->render();
}

public function testContainerClass(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div class="my container">
<ul>
<li>
<div>Id: 1</div><div>Name: John</div><div>Age: 20</div>
</li>
<li>
<div>Id: 2</div><div>Name: Mary</div><div>Age: 21</div>
</li>
</ul>
<div>Page <b>1</b> of <b>1</b></div>
</div>
HTML,
ListView::widget()
->itemView(dirname(__DIR__) . '/Support/view/_listview.php')
->containerAttributes(['class' => 'existing'])
->containerClass('my', 'container')
->dataReader($this->createOffsetPaginator($this->data, 10))
->separator(PHP_EOL)
->render(),
);
}

public function testAddContainerClass(): void
{
Assert::equalsWithoutLE(
<<<HTML
<div class="existing my container">
<ul>
<li>
<div>Id: 1</div><div>Name: John</div><div>Age: 20</div>
</li>
<li>
<div>Id: 2</div><div>Name: Mary</div><div>Age: 21</div>
</li>
</ul>
<div>Page <b>1</b> of <b>1</b></div>
</div>
HTML,
ListView::widget()
->itemView(dirname(__DIR__) . '/Support/view/_listview.php')
->containerAttributes(['class' => 'existing'])
->addContainerClass('my', 'container')
->dataReader($this->createOffsetPaginator($this->data, 10))
->separator(PHP_EOL)
->render(),
);
}
}