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 @@ -55,6 +55,7 @@
- Enh #223: `KeysetPaginator` now uses default order from `Sort` when no sort is set (@vjik)
- Chg #224: Change `$iterableFilterHandlers` to context object in `IterableFilterHandlerInterface::match()` (@vjik)
- New #224: Add filtering by nested values support in `IterableDataReader` (@vjik)
- Chg #225: Rename classes: `All` to `AndX`, `Any` to `OrX`. Remove `Group` class (@vjik)

## 1.0.1 January 25, 2023

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ To filter data in a data reader implementing `FilterableDataInterface` you need
`withFilter()` method:

```php
$filter = new All(
$filter = new AndX(
new GreaterThan('id', 3),
new Like('name', 'agent')
);
Expand All @@ -114,8 +114,7 @@ $data = $reader->read();

Filter could be composed with:

- `All`
- `Any`
- `AndX`
- `Between`
- `Equals`
- `EqualsNull`
Expand All @@ -127,13 +126,14 @@ Filter could be composed with:
- `LessThanOrEqual`
- `Like`
- `Not`
- `OrX`

#### Filtering with arrays

The `All` and `Any` filters have a `withCriteriaArray()` method, which allows you to define filters with arrays.
The `AndX` and `OrX` filters have a `withCriteriaArray()` method, which allows you to define filters with arrays.

```php
$dataReader->withFilter((new All())->withCriteriaArray([
$dataReader->withFilter((new AndX())->withCriteriaArray([
['=', 'id', 88],
[
'or',
Expand Down Expand Up @@ -197,7 +197,7 @@ class OwnIterableNotTwoFilterHandler implements IterableFilterHandlerInterface
}

// and using it on a data reader
$filter = new All(
$filter = new AndX(
new LessThan('id', 8),
new OwnNotTwoFilter('id'),
);
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
},
"require-dev": {
"maglnet/composer-require-checker": "^4.7.1",
"phpunit/phpunit": "^10.5.46",
"rector/rector": "^2.0.18",
"phpunit/phpunit": "^10.5.48",
"rector/rector": "^2.1.2",
"roave/infection-static-analysis-plugin": "^1.35",
"spatie/phpunit-watcher": "^1.24",
"vimeo/psalm": "^5.26.1 || ^6.10.3"
Expand Down
12 changes: 6 additions & 6 deletions docs/guide/pt-BR/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Para filtrar dados em um leitor de dados implementando `FilterableDataInterface`
método `withFilter()`:

```php
$filter = new All(
$filter = new AndX(
new GreaterThan('id', 3),
new Like('name', 'agent')
);
Expand All @@ -78,8 +78,7 @@ $data = $reader->read();

O filtro pode ser composto por:

- `All`
- `Any`
- `AndX`
- `Between`
- `Equals`
- `EqualsNull`
Expand All @@ -91,13 +90,14 @@ O filtro pode ser composto por:
- `LessThanOrEqual`
- `Like`
- `Not`
- `OrX`

#### Filtrando com matrizes

Os filtros `All` e `Any` possuem um método `withCriteriaArray()`, que permite definir filtros com arrays.
Os filtros `AndX` e `OrX` possuem um método `withCriteriaArray()`, que permite definir filtros com arrays.

```php
$dataReader->withFilter((new All())->withCriteriaArray([
$dataReader->withFilter((new AndX())->withCriteriaArray([
['=', 'id', 88],
[
'or',
Expand Down Expand Up @@ -161,7 +161,7 @@ class OwnIterableNotTwoFilterHandler implements IterableFilterHandlerInterface
}

// and using it on a data reader
$filter = new All(
$filter = new AndX(
new LessThan('id', 8),
new OwnNotTwoFilter('id'),
);
Expand Down
22 changes: 0 additions & 22 deletions src/Reader/Filter/All.php

This file was deleted.

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

declare(strict_types=1);

namespace Yiisoft\Data\Reader\Filter;

use Yiisoft\Data\Reader\FilterInterface;

/**
* `AndX` filter allows combining multiple sub-filters using "AND" operator.
*
* ```php
* $dataReader->withFilter(
* new AndX(
* new GreaterThan('id', 88),
* new Equals('state', 2),
* new Like('name', 'eva'),
* )
* );
* ```
*/
final class AndX implements FilterInterface
{
/**
* @var FilterInterface[] Sub-filters to use.
*/
public readonly array $filters;

/**
* @param FilterInterface ...$filters Sub-filters to use.
*/
public function __construct(FilterInterface ...$filters)
{
$this->filters = $filters;
}
}
21 changes: 0 additions & 21 deletions src/Reader/Filter/Any.php

This file was deleted.

23 changes: 12 additions & 11 deletions src/Reader/Filter/Group.php → src/Reader/Filter/OrX.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@
use Yiisoft\Data\Reader\FilterInterface;

/**
* `Group` filter is an abstract class that allows combining multiple sub-filters.
* `OrX` filter allows combining multiple sub-filters using "OR" operator.
*
* ```php
* $dataReader->withFilter(
* new OrX(
* new GreaterThan('id', 88),
* new Equals('state', 2),
* )
* );
* ```
*/
abstract class Group implements FilterInterface
final class OrX implements FilterInterface
{
/**
* @var FilterInterface[] Sub-filters to use.
*/
private array $filters;
public readonly array $filters;

/**
* @param FilterInterface ...$filters Sub-filters to use.
Expand All @@ -23,12 +32,4 @@ public function __construct(FilterInterface ...$filters)
{
$this->filters = $filters;
}

/**
* @return FilterInterface[]
*/
public function getFilters(): array
{
return $this->filters;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@

namespace Yiisoft\Data\Reader\Iterable\FilterHandler;

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

/**
* `All` iterable filter handler allows combining multiple sub-filters.
* `AndX` iterable filter handler allows combining multiple sub-filters.
* The filter matches only if all the sub-filters match.
*/
final class AllHandler implements IterableFilterHandlerInterface
final class AndXHandler implements IterableFilterHandlerInterface
{
public function getFilterClass(): string
{
return All::class;
return AndX::class;
}

public function match(object|array $item, FilterInterface $filter, Context $context): bool
{
/** @var All $filter */
/** @var AndX $filter */

foreach ($filter->getFilters() as $subFilter) {
foreach ($filter->filters as $subFilter) {
$filterHandler = $context->getFilterHandler($subFilter::class);
if (!$filterHandler->match($item, $subFilter, $context)) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@

namespace Yiisoft\Data\Reader\Iterable\FilterHandler;

use Yiisoft\Data\Reader\Filter\Any;
use Yiisoft\Data\Reader\Filter\OrX;
use Yiisoft\Data\Reader\FilterInterface;
use Yiisoft\Data\Reader\Iterable\Context;
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;

/**
* `Any` iterable filter handler allows combining multiple sub-filters.
* `OrX` iterable filter handler allows combining multiple sub-filters.
* The filter matches if any of the sub-filters match.
*/
final class AnyHandler implements IterableFilterHandlerInterface
final class OrXHandler implements IterableFilterHandlerInterface
{
public function getFilterClass(): string
{
return Any::class;
return OrX::class;
}

public function match(object|array $item, FilterInterface $filter, Context $context): bool
{
/** @var Any $filter */
/** @var OrX $filter */

foreach ($filter->getFilters() as $subFilter) {
foreach ($filter->filters as $subFilter) {
$filterHandler = $context->getFilterHandler($subFilter::class);
if ($filterHandler->match($item, $subFilter, $context)) {
return true;
Expand Down
8 changes: 4 additions & 4 deletions src/Reader/Iterable/IterableDataReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
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\AnyHandler;
use Yiisoft\Data\Reader\Iterable\FilterHandler\AndXHandler;
use Yiisoft\Data\Reader\Iterable\FilterHandler\OrXHandler;
use Yiisoft\Data\Reader\Iterable\FilterHandler\BetweenHandler;
use Yiisoft\Data\Reader\Iterable\FilterHandler\EqualsHandler;
use Yiisoft\Data\Reader\Iterable\FilterHandler\EqualsNullHandler;
Expand Down Expand Up @@ -75,8 +75,8 @@ public function __construct(
private readonly ValueReaderInterface $valueReader = new FlatValueReader(),
) {
$this->coreFilterHandlers = $this->prepareFilterHandlers([
new AllHandler(),
new AnyHandler(),
new AndXHandler(),
new OrXHandler(),
new BetweenHandler(),
new EqualsHandler(),
new EqualsNullHandler(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

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

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

abstract class BaseReaderWithAllTestCase extends BaseReaderTestCase
abstract class BaseReaderWithAndXTestCase extends BaseReaderTestCase
{
public function testWithReader(): void
{
$reader = $this
->getReader()
->withFilter(new All(new Equals('balance', 100), new Equals('email', 'seed@beat')));
->withFilter(new AndX(new Equals('balance', 100), new Equals('email', 'seed@beat')));
$this->assertFixtures([2], $reader->read());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Yiisoft\Data\Tests\Common\Reader\ReaderWithFilter;

use PHPUnit\Framework\Attributes\DataProvider;
use Yiisoft\Data\Reader\Filter\All;
use Yiisoft\Data\Reader\Filter\Any;
use Yiisoft\Data\Reader\Filter\AndX;
use Yiisoft\Data\Reader\Filter\OrX;
use Yiisoft\Data\Reader\Filter\Between;
use Yiisoft\Data\Reader\Filter\Equals;
use Yiisoft\Data\Reader\Filter\EqualsNull;
Expand All @@ -24,8 +24,8 @@ abstract class BaseReaderWithNotTestCase extends BaseReaderTestCase
public static function dataWithReader(): array
{
return [
'all' => [new Not(new All(new Equals('number', 1), new Equals('number', 2))), range(1, 5)],
'any' => [new Not(new Any(new Equals('number', 1), new Equals('number', 2))), range(3, 5)],
'all' => [new Not(new AndX(new Equals('number', 1), new Equals('number', 2))), range(1, 5)],
'any' => [new Not(new OrX(new Equals('number', 1), new Equals('number', 2))), range(3, 5)],
'between' => [new Not(new Between('balance', 10.25, 100.0)), [2, 4]],
'equals' => [new Not(new Equals('number', 1)), range(2, 5)],
'equals null' => [new Not(new EqualsNull('born_at')), [5]],
Expand Down
Loading
Loading