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

Add "case-sensitive" parameter to LikeFilterFactory + Minor improvements #204

Merged
merged 1 commit into from
Sep 3, 2024
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
3 changes: 3 additions & 0 deletions src/BaseListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
use Yiisoft\Data\Reader\OrderHelper;
use Yiisoft\Yii\DataView\Exception\DataReaderNotSetException;

use function is_array;
use function is_string;

/**
* @psalm-type UrlArguments = array<string,scalar|Stringable|null>
* @psalm-type UrlCreator = callable(UrlArguments,array):string
Expand Down Expand Up @@ -265,7 +268,7 @@
*
* @psalm-return list{FilterInterface[]|null,ValidationResult}
*/
protected function makeFilters(): array

Check warning on line 271 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

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

if (!$dataReader instanceof PaginatorInterface) {
if (
$dataReader instanceof OffsetableDataInterface

Check warning on line 349 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

Escaped Mutant for Mutator "LogicalAnd": --- Original +++ New @@ @@ { $dataReader = $this->getDataReader(); if (!$dataReader instanceof PaginatorInterface) { - if ($dataReader instanceof OffsetableDataInterface && $dataReader instanceof CountableDataInterface && $dataReader instanceof LimitableDataInterface) { + if (($dataReader instanceof OffsetableDataInterface || $dataReader instanceof CountableDataInterface) && $dataReader instanceof LimitableDataInterface) { $dataReader = new OffsetPaginator($dataReader); } elseif ($dataReader instanceof FilterableDataInterface && $dataReader instanceof SortableDataInterface && $dataReader instanceof LimitableDataInterface) { if ($dataReader->getSort() !== null) {
&& $dataReader instanceof CountableDataInterface
&& $dataReader instanceof LimitableDataInterface
) {
Expand All @@ -363,7 +366,7 @@
}
}

if ($dataReader->isPaginationRequired()) {

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 "IfNegation": --- Original +++ New @@ @@ return $dataReader; } } - if ($dataReader->isPaginationRequired()) { + if (!$dataReader->isPaginationRequired()) { if ($pageSize !== null) { $dataReader = $dataReader->withPageSize((int) $pageSize); }
if ($pageSize !== null) {
$dataReader = $dataReader->withPageSize((int) $pageSize);
}
Expand All @@ -375,7 +378,7 @@
}
}

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

Check warning on line 381 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 (!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 @@ -387,7 +390,7 @@
}
}

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

Check warning on line 393 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

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

Check warning on line 393 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

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

Check warning on line 393 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->withSort($sortObject->withOrder($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 @@ -676,7 +679,7 @@
$currentPage = $dataReader->getCurrentPage();

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

Check warning on line 682 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

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

Check warning on line 682 in src/BaseListView.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

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

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.2-ubuntu-latest

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

// The number of rows currently being displayed
$count = $dataReader->getCurrentPageSize();
Expand Down
2 changes: 2 additions & 0 deletions src/BasePagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Yiisoft\Html\Html;
use Yiisoft\Widget\Widget;

use function call_user_func_array;

/**
* @psalm-import-type UrlCreator from BaseListView
*/
Expand Down
1 change: 1 addition & 0 deletions src/Column/DataColumnRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

use function is_array;
use function is_callable;
use function is_string;

/**
* @psalm-import-type FilterEmptyCallable from DataColumn
Expand Down
10 changes: 7 additions & 3 deletions src/Filter/Factory/LikeFilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
namespace Yiisoft\Yii\DataView\Filter\Factory;

use Yiisoft\Data\Reader\Filter\Like;
use Yiisoft\Data\Reader\FilterInterface;

final class LikeFilterFactory implements FilterFactoryInterface
{
public function create(string $property, string $value): ?FilterInterface
public function __construct(
private readonly ?bool $caseSensitive = null,
) {
}

public function create(string $property, string $value): ?Like
{
if (empty($value)) {
return null;
}

return new Like($property, $value);
return new Like($property, $value, $this->caseSensitive);
}
}
2 changes: 2 additions & 0 deletions src/ListView.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Yiisoft\View\Exception\ViewNotFoundException;
use Yiisoft\View\View;

use function is_string;

/**
* The ListView widget displays data from data provider. Each data model is rendered using the view specified.
*/
Expand Down
40 changes: 40 additions & 0 deletions tests/Filter/Factory/LikeFilterFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Yii\DataView\Tests\Filter\Factory;

use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Yiisoft\Yii\DataView\Filter\Factory\LikeFilterFactory;

final class LikeFilterFactoryTest extends TestCase
{
public function testBase(): void
{
$factory = new LikeFilterFactory();

$filter = $factory->create('name', 'John');

$this->assertSame('name', $filter->getField());
$this->assertSame('John', $filter->getValue());
$this->assertNull($filter->isCaseSensitive());
}

public static function dataCaseSensitive(): iterable
{
yield [null];
yield [true];
yield [false];
}

#[DataProvider('dataCaseSensitive')]
public function testCaseSensitive(?bool $value): void
{
$factory = new LikeFilterFactory($value);

$filter = $factory->create('name', 'John');

$this->assertSame($value, $filter->isCaseSensitive());
}
}
Loading