Skip to content

Commit

Permalink
Add "case-sensitive" parameter to LikeFilterFactory + Minor improve…
Browse files Browse the repository at this point in the history
…ments (#204)
  • Loading branch information
vjik authored Sep 3, 2024
1 parent fb1100a commit e4a80c4
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 3 deletions.
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
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());
}
}

0 comments on commit e4a80c4

Please sign in to comment.