Skip to content

Commit cc7bc93

Browse files
authored
Fix #123: Separate criteria and filters (#126)
1 parent bc229f9 commit cc7bc93

4 files changed

Lines changed: 63 additions & 58 deletions

File tree

README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,18 @@ Filter could be composed with:
128128

129129
#### Filtering with arrays
130130

131-
The `All` and `Any` filters have a `withFiltersArray()` method, which allows you to define filters with arrays.
131+
The `All` and `Any` filters have a `withCriteriaArray()` method, which allows you to define filters with arrays.
132132

133133
```php
134-
$dataReader->withFilter((new All())->withFiltersArray([
135-
['=', 'id', 88],
136-
[
137-
'or',
134+
$dataReader->withFilter((new All())->withCriteriaArray([
135+
['=', 'id', 88],
138136
[
139-
['=', 'color', 'red'],
140-
['=', 'state', 1],
137+
'or',
138+
[
139+
['=', 'color', 'red'],
140+
['=', 'state', 1],
141+
]
141142
]
142-
]
143143
]));
144144
```
145145

src/Reader/Filter/Group.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ public function toCriteriaArray(): array
6565
* ));
6666
* ```
6767
*
68-
* @param array[]|FilterInterface[] $criteriaArray Criteria array to use.
68+
* @param array[] $criteriaArray Criteria array to use.
6969
* Instances of FilterInterface are ignored.
7070
*
71-
* @throws InvalidArgumentException If criteria arrays is not valid.
71+
* @throws InvalidArgumentException If criteria array is not valid.
7272
*
7373
* @return static New instance.
7474
*
@@ -77,10 +77,6 @@ public function toCriteriaArray(): array
7777
public function withCriteriaArray(array $criteriaArray): static
7878
{
7979
foreach ($criteriaArray as $key => $item) {
80-
if ($item instanceof FilterInterface) {
81-
continue;
82-
}
83-
8480
if (!is_array($item)) {
8581
throw new InvalidArgumentException(sprintf('Invalid filter on "%s" key.', $key));
8682
}

tests/Reader/Filter/AllTest.php

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66

77
use InvalidArgumentException;
88
use Yiisoft\Data\Reader\Filter\All;
9-
use Yiisoft\Data\Reader\Filter\Any;
10-
use Yiisoft\Data\Reader\Filter\Between;
11-
use Yiisoft\Data\Reader\Filter\Equals;
129
use Yiisoft\Data\Reader\Filter\GreaterThan;
13-
use Yiisoft\Data\Reader\Filter\In;
1410
use Yiisoft\Data\Reader\Filter\LessThan;
1511
use Yiisoft\Data\Tests\TestCase;
1612

@@ -32,53 +28,60 @@ public function testToArray(): void
3228
], $filter->toCriteriaArray());
3329
}
3430

35-
public function testToArrayAndWithFiltersArray(): void
31+
public function testWithCriteriaArrayIsImmutable(): void
3632
{
3733
$filter = new All(
3834
new LessThan('test', 4),
3935
new GreaterThan('test', 2),
4036
);
4137

4238
$newFilter = $filter->withCriteriaArray([
43-
new Any(
44-
new Between('test', 2, 4),
45-
new In('test', [1, 2, 3, 4]),
46-
),
47-
['=', 'test', 3],
39+
['>', 'test', 1],
40+
['<', 'test', 5],
4841
]);
4942

5043
$this->assertNotSame($filter, $newFilter);
44+
}
5145

52-
$this->assertSame([
53-
'and',
46+
public function testWithCriteriaArrayOverridesConstructor(): void
47+
{
48+
$filter = new All(
49+
new LessThan('test', 4),
50+
new GreaterThan('test', 2),
51+
);
52+
53+
$newFilter = $filter->withCriteriaArray([
54+
['>', 'test', 1],
55+
['<', 'test', 5],
56+
]);
57+
58+
$this->assertEquals(
5459
[
60+
'and',
5561
[
56-
'or',
57-
[
58-
['between', 'test', 2, 4],
59-
['in', 'test', [1, 2, 3, 4]],
60-
],
62+
['>', 'test', 1],
63+
['<', 'test', 5],
6164
],
62-
['=', 'test', 3],
6365
],
64-
], $newFilter->toCriteriaArray());
66+
$newFilter->toCriteriaArray()
67+
);
6568
}
6669

6770
/**
6871
* @dataProvider invalidFilterDataProvider
6972
*/
70-
public function testWithFiltersArrayFailForInvalidFilter($filter): void
73+
public function testWithCriteriaArrayFailForInvalidFilter($filter): void
7174
{
7275
$this->expectException(InvalidArgumentException::class);
7376
$this->expectExceptionMessage('Invalid filter on "1" key.');
7477

75-
(new All())->withCriteriaArray([new Equals('test', 1), $filter]);
78+
(new All())->withCriteriaArray([['=', 'test', 1], $filter]);
7679
}
7780

7881
/**
7982
* @dataProvider invalidFilterOperatorDataProvider
8083
*/
81-
public function testWithFiltersArrayFailForInvalidFilterOperator(array $filter): void
84+
public function testWithCriteriaArrayFailForInvalidFilterOperator(array $filter): void
8285
{
8386
$this->expectException(InvalidArgumentException::class);
8487
$this->expectExceptionMessage('Invalid filter operator on "0" key.');

tests/Reader/Filter/AnyTest.php

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
namespace Yiisoft\Data\Tests\Reader\Filter;
66

77
use InvalidArgumentException;
8-
use Yiisoft\Data\Reader\Filter\All;
98
use Yiisoft\Data\Reader\Filter\Any;
10-
use Yiisoft\Data\Reader\Filter\Between;
11-
use Yiisoft\Data\Reader\Filter\Equals;
129
use Yiisoft\Data\Reader\Filter\GreaterThan;
13-
use Yiisoft\Data\Reader\Filter\In;
1410
use Yiisoft\Data\Reader\Filter\LessThan;
1511
use Yiisoft\Data\Tests\TestCase;
1612

@@ -32,53 +28,63 @@ public function testToArray(): void
3228
], $filter->toCriteriaArray());
3329
}
3430

35-
public function testToArrayAndWithFiltersArray(): void
31+
public function testWithCriteriaArrayIsImmutable(): void
3632
{
3733
$filter = new Any(
3834
new LessThan('test', 4),
3935
new GreaterThan('test', 2),
4036
);
4137

4238
$newFilter = $filter->withCriteriaArray([
43-
new All(
44-
new Between('test', 2, 4),
45-
new In('test', [1, 2, 3, 4]),
46-
),
47-
['=', 'test', 3],
39+
['>', 'test', 1],
40+
['<', 'test', 5],
4841
]);
4942

5043
$this->assertNotSame($filter, $newFilter);
44+
}
5145

52-
$this->assertSame([
53-
'or',
46+
public function testWithCriteriaArrayOverridesConstructor(): void
47+
{
48+
$filter = new Any(
49+
new LessThan('test', 4),
50+
new GreaterThan('test', 2),
51+
);
52+
53+
$newFilter = $filter->withCriteriaArray([
54+
['>', 'test', 1],
55+
['<', 'test', 5],
56+
]);
57+
58+
$this->assertEquals(
5459
[
60+
'or',
5561
[
56-
'and',
57-
[
58-
['between', 'test', 2, 4],
59-
['in', 'test', [1, 2, 3, 4]],
60-
],
62+
['>', 'test', 1],
63+
['<', 'test', 5],
6164
],
62-
['=', 'test', 3],
6365
],
64-
], $newFilter->toCriteriaArray());
66+
$newFilter->toCriteriaArray()
67+
);
6568
}
6669

6770
/**
6871
* @dataProvider invalidFilterDataProvider
6972
*/
70-
public function testWithFiltersArrayFailForInvalidFilter($filter): void
73+
public function testWithCriteriaArrayFailForInvalidFilter(mixed $filter): void
7174
{
7275
$this->expectException(InvalidArgumentException::class);
7376
$this->expectExceptionMessage('Invalid filter on "1" key.');
7477

75-
(new Any())->withCriteriaArray([new Equals('test', 1), $filter]);
78+
(new Any())->withCriteriaArray([
79+
['=', 'test', 1],
80+
$filter,
81+
]);
7682
}
7783

7884
/**
7985
* @dataProvider invalidFilterOperatorDataProvider
8086
*/
81-
public function testWithFiltersArrayFailForInvalidFilterOperator(array $filter): void
87+
public function testWithCriteriaArrayFailForInvalidFilterOperator(array $filter): void
8288
{
8389
$this->expectException(InvalidArgumentException::class);
8490
$this->expectExceptionMessage('Invalid filter operator on "0" key.');

0 commit comments

Comments
 (0)