Skip to content
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
- Chg #226: Refactor filter classes to use readonly properties instead of getters (@vjik)
- New #213: Add `nextPage()` and `previousPage()` methods to `PaginatorInterface` (@samdark)
- New #200: Add matching mode parameter to `Like` filter (@samdark, @vjik)
- New #232: Add `All` and `None` filters (@vjik)

## 1.0.1 January 25, 2023

Expand Down
15 changes: 15 additions & 0 deletions src/Reader/Filter/All.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Reader\Filter;

use Yiisoft\Data\Reader\FilterInterface;

/**
* Represents a filter that matches all items.
* Used to indicate that no filtering should be applied.
*/
final class All implements FilterInterface
{
}
15 changes: 15 additions & 0 deletions src/Reader/Filter/None.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Reader\Filter;

use Yiisoft\Data\Reader\FilterInterface;

/**
* Represents a filter that matches no items.
* Used to indicate that all items should be excluded.
*/
final class None implements FilterInterface
{
}
27 changes: 27 additions & 0 deletions src/Reader/Iterable/FilterHandler/AllHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Reader\Iterable\FilterHandler;

use Yiisoft\Data\Reader\Filter\All;
use Yiisoft\Data\Reader\FilterInterface;
use Yiisoft\Data\Reader\Iterable\Context;
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;

/**
* Handles the {@see All} filter for iterable data readers.
* Always matches all items, effectively disabling filtering.
*/
final class AllHandler implements IterableFilterHandlerInterface
{
public function getFilterClass(): string
{
return All::class;
}

public function match(object|array $item, FilterInterface $filter, Context $context): bool
{
return true;
}
}
27 changes: 27 additions & 0 deletions src/Reader/Iterable/FilterHandler/NoneHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Reader\Iterable\FilterHandler;

use Yiisoft\Data\Reader\Filter\None;
use Yiisoft\Data\Reader\FilterInterface;
use Yiisoft\Data\Reader\Iterable\Context;
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;

/**
* Handles the {@see None} filter for iterable data readers.
* Never matches any items, effectively excluding all data.
*/
final class NoneHandler implements IterableFilterHandlerInterface
{
public function getFilterClass(): string
{
return None::class;
}

public function match(object|array $item, FilterInterface $filter, Context $context): bool
{
return false;
}
}
4 changes: 4 additions & 0 deletions src/Reader/Iterable/IterableDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use Yiisoft\Data\Reader\DataReaderInterface;
use Yiisoft\Data\Reader\FilterHandlerInterface;
use Yiisoft\Data\Reader\FilterInterface;
use Yiisoft\Data\Reader\Iterable\FilterHandler\AllHandler;
use Yiisoft\Data\Reader\Iterable\FilterHandler\AndXHandler;
use Yiisoft\Data\Reader\Iterable\FilterHandler\NoneHandler;
use Yiisoft\Data\Reader\Iterable\FilterHandler\OrXHandler;
use Yiisoft\Data\Reader\Iterable\FilterHandler\BetweenHandler;
use Yiisoft\Data\Reader\Iterable\FilterHandler\EqualsHandler;
Expand Down Expand Up @@ -75,6 +77,8 @@ public function __construct(
private readonly ValueReaderInterface $valueReader = new FlatValueReader(),
) {
$this->coreFilterHandlers = $this->prepareFilterHandlers([
new AllHandler(),
new NoneHandler(),
new AndXHandler(),
new OrXHandler(),
new BetweenHandler(),
Expand Down
20 changes: 20 additions & 0 deletions tests/Common/Reader/ReaderWithFilter/BaseReaderWithAllTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter;

use Yiisoft\Data\Reader\Filter\All;
use Yiisoft\Data\Tests\Common\Reader\BaseReaderTestCase;

abstract class BaseReaderWithAllTestCase extends BaseReaderTestCase
{
public function testWithReader(): void
{
$reader = $this->getReader()->withFilter(new All());

$result = $reader->read();

$this->assertFixtures([0, 1, 2, 3, 4], $result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter;

use Yiisoft\Data\Reader\Filter\None;
use Yiisoft\Data\Tests\Common\Reader\BaseReaderTestCase;

use function PHPUnit\Framework\assertSame;

abstract class BaseReaderWithNoneTestCase extends BaseReaderTestCase
{
public function testWithReader(): void
{
$reader = $this->getReader()->withFilter(new None());

$result = $reader->read();

assertSame([], $result);
}
}
33 changes: 33 additions & 0 deletions tests/Reader/Iterable/FilterHandler/AllHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Tests\Reader\Iterable\FilterHandler;

use PHPUnit\Framework\TestCase;
use Yiisoft\Data\Reader\Filter\All;
use Yiisoft\Data\Reader\Iterable\Context;
use Yiisoft\Data\Reader\Iterable\FilterHandler\AllHandler;
use Yiisoft\Data\Reader\Iterable\ValueReader\FlatValueReader;

use function PHPUnit\Framework\assertSame;
use function PHPUnit\Framework\assertTrue;

final class AllHandlerTest extends TestCase
{
public function testMatchAlwaysTrue(): void
{
$handler = new AllHandler();
$context = new Context([], new FlatValueReader());

$result = $handler->match(['any' => 'value'], new All(), $context);

assertTrue($result);
}

public function testGetFilterClass(): void
{
$handler = new AllHandler();
assertSame(All::class, $handler->getFilterClass());
}
}
32 changes: 32 additions & 0 deletions tests/Reader/Iterable/FilterHandler/NoneHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Reader\Iterable\FilterHandler\Tests;

use PHPUnit\Framework\TestCase;
use Yiisoft\Data\Reader\Filter\None;
use Yiisoft\Data\Reader\Iterable\Context;
use Yiisoft\Data\Reader\Iterable\FilterHandler\NoneHandler;
use Yiisoft\Data\Reader\Iterable\ValueReader\FlatValueReader;

use function PHPUnit\Framework\assertFalse;

final class NoneHandlerTest extends TestCase
{
public function testMatchAlwaysFalse(): void
{
$handler = new NoneHandler();
$context = new Context([], new FlatValueReader());

$result = $handler->match(['any' => 'value'], new None(), $context);

assertFalse($result);
}

public function testGetFilterClass(): void
{
$handler = new NoneHandler();
$this->assertSame(None::class, $handler->getFilterClass());
}
}
12 changes: 12 additions & 0 deletions tests/Reader/Iterable/ReaderWithFilter/ReaderWithAllTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Tests\Reader\Iterable\ReaderWithFilter;

use Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter\BaseReaderWithAllTestCase;

final class ReaderWithAllTest extends BaseReaderWithAllTestCase
{
use ReaderTrait;
}
12 changes: 12 additions & 0 deletions tests/Reader/Iterable/ReaderWithFilter/ReaderWithNoneTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Data\Tests\Reader\Iterable\ReaderWithFilter;

use Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter\BaseReaderWithNoneTestCase;

final class ReaderWithNoneTest extends BaseReaderWithNoneTestCase
{
use ReaderTrait;
}
Loading