Skip to content

Commit 0ca75df

Browse files
authored
Add All and None filters (#232)
1 parent 1113566 commit 0ca75df

File tree

12 files changed

+220
-0
lines changed

12 files changed

+220
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
- Chg #226: Refactor filter classes to use readonly properties instead of getters (@vjik)
6060
- New #213: Add `nextPage()` and `previousPage()` methods to `PaginatorInterface` (@samdark)
6161
- New #200: Add matching mode parameter to `Like` filter (@samdark, @vjik)
62+
- New #232: Add `All` and `None` filters (@vjik)
6263

6364
## 1.0.1 January 25, 2023
6465

src/Reader/Filter/All.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Data\Reader\Filter;
6+
7+
use Yiisoft\Data\Reader\FilterInterface;
8+
9+
/**
10+
* Represents a filter that matches all items.
11+
* Used to indicate that no filtering should be applied.
12+
*/
13+
final class All implements FilterInterface
14+
{
15+
}

src/Reader/Filter/None.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Data\Reader\Filter;
6+
7+
use Yiisoft\Data\Reader\FilterInterface;
8+
9+
/**
10+
* Represents a filter that matches no items.
11+
* Used to indicate that all items should be excluded.
12+
*/
13+
final class None implements FilterInterface
14+
{
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Data\Reader\Iterable\FilterHandler;
6+
7+
use Yiisoft\Data\Reader\Filter\All;
8+
use Yiisoft\Data\Reader\FilterInterface;
9+
use Yiisoft\Data\Reader\Iterable\Context;
10+
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;
11+
12+
/**
13+
* Handles the {@see All} filter for iterable data readers.
14+
* Always matches all items, effectively disabling filtering.
15+
*/
16+
final class AllHandler implements IterableFilterHandlerInterface
17+
{
18+
public function getFilterClass(): string
19+
{
20+
return All::class;
21+
}
22+
23+
public function match(object|array $item, FilterInterface $filter, Context $context): bool
24+
{
25+
return true;
26+
}
27+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Data\Reader\Iterable\FilterHandler;
6+
7+
use Yiisoft\Data\Reader\Filter\None;
8+
use Yiisoft\Data\Reader\FilterInterface;
9+
use Yiisoft\Data\Reader\Iterable\Context;
10+
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;
11+
12+
/**
13+
* Handles the {@see None} filter for iterable data readers.
14+
* Never matches any items, effectively excluding all data.
15+
*/
16+
final class NoneHandler implements IterableFilterHandlerInterface
17+
{
18+
public function getFilterClass(): string
19+
{
20+
return None::class;
21+
}
22+
23+
public function match(object|array $item, FilterInterface $filter, Context $context): bool
24+
{
25+
return false;
26+
}
27+
}

src/Reader/Iterable/IterableDataReader.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
use Yiisoft\Data\Reader\DataReaderInterface;
1313
use Yiisoft\Data\Reader\FilterHandlerInterface;
1414
use Yiisoft\Data\Reader\FilterInterface;
15+
use Yiisoft\Data\Reader\Iterable\FilterHandler\AllHandler;
1516
use Yiisoft\Data\Reader\Iterable\FilterHandler\AndXHandler;
17+
use Yiisoft\Data\Reader\Iterable\FilterHandler\NoneHandler;
1618
use Yiisoft\Data\Reader\Iterable\FilterHandler\OrXHandler;
1719
use Yiisoft\Data\Reader\Iterable\FilterHandler\BetweenHandler;
1820
use Yiisoft\Data\Reader\Iterable\FilterHandler\EqualsHandler;
@@ -75,6 +77,8 @@ public function __construct(
7577
private readonly ValueReaderInterface $valueReader = new FlatValueReader(),
7678
) {
7779
$this->coreFilterHandlers = $this->prepareFilterHandlers([
80+
new AllHandler(),
81+
new NoneHandler(),
7882
new AndXHandler(),
7983
new OrXHandler(),
8084
new BetweenHandler(),
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter;
6+
7+
use Yiisoft\Data\Reader\Filter\All;
8+
use Yiisoft\Data\Tests\Common\Reader\BaseReaderTestCase;
9+
10+
abstract class BaseReaderWithAllTestCase extends BaseReaderTestCase
11+
{
12+
public function testWithReader(): void
13+
{
14+
$reader = $this->getReader()->withFilter(new All());
15+
16+
$result = $reader->read();
17+
18+
$this->assertFixtures([0, 1, 2, 3, 4], $result);
19+
}
20+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter;
6+
7+
use Yiisoft\Data\Reader\Filter\None;
8+
use Yiisoft\Data\Tests\Common\Reader\BaseReaderTestCase;
9+
10+
use function PHPUnit\Framework\assertSame;
11+
12+
abstract class BaseReaderWithNoneTestCase extends BaseReaderTestCase
13+
{
14+
public function testWithReader(): void
15+
{
16+
$reader = $this->getReader()->withFilter(new None());
17+
18+
$result = $reader->read();
19+
20+
assertSame([], $result);
21+
}
22+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Data\Tests\Reader\Iterable\FilterHandler;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Yiisoft\Data\Reader\Filter\All;
9+
use Yiisoft\Data\Reader\Iterable\Context;
10+
use Yiisoft\Data\Reader\Iterable\FilterHandler\AllHandler;
11+
use Yiisoft\Data\Reader\Iterable\ValueReader\FlatValueReader;
12+
13+
use function PHPUnit\Framework\assertSame;
14+
use function PHPUnit\Framework\assertTrue;
15+
16+
final class AllHandlerTest extends TestCase
17+
{
18+
public function testMatchAlwaysTrue(): void
19+
{
20+
$handler = new AllHandler();
21+
$context = new Context([], new FlatValueReader());
22+
23+
$result = $handler->match(['any' => 'value'], new All(), $context);
24+
25+
assertTrue($result);
26+
}
27+
28+
public function testGetFilterClass(): void
29+
{
30+
$handler = new AllHandler();
31+
assertSame(All::class, $handler->getFilterClass());
32+
}
33+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yiisoft\Data\Reader\Iterable\FilterHandler\Tests;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Yiisoft\Data\Reader\Filter\None;
9+
use Yiisoft\Data\Reader\Iterable\Context;
10+
use Yiisoft\Data\Reader\Iterable\FilterHandler\NoneHandler;
11+
use Yiisoft\Data\Reader\Iterable\ValueReader\FlatValueReader;
12+
13+
use function PHPUnit\Framework\assertFalse;
14+
15+
final class NoneHandlerTest extends TestCase
16+
{
17+
public function testMatchAlwaysFalse(): void
18+
{
19+
$handler = new NoneHandler();
20+
$context = new Context([], new FlatValueReader());
21+
22+
$result = $handler->match(['any' => 'value'], new None(), $context);
23+
24+
assertFalse($result);
25+
}
26+
27+
public function testGetFilterClass(): void
28+
{
29+
$handler = new NoneHandler();
30+
$this->assertSame(None::class, $handler->getFilterClass());
31+
}
32+
}

0 commit comments

Comments
 (0)