Skip to content

Commit 3217318

Browse files
authored
Remove FilterHandlerInterface and FilterableDataInterface::withAddedFilterHandlers() (#241)
1 parent 43e46bf commit 3217318

File tree

8 files changed

+16
-94
lines changed

8 files changed

+16
-94
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
`OffsetableDataInterface::getOffset()` (@samdark)
3535
- Chg #187: `LimitableDataInterface::withLimit()` now accepts `null` to indicate "no limit". `0` is now a valid limit
3636
value meaning `return nothing` (@samdark)
37-
- Chg #163: Rename `FilterableDataInterface::withFilterHandlers()` to `FilterableDataInterface::withAddedFilterHandlers()` (@samdark)
37+
- Chg #163, #241: Remove `FilterableDataInterface::withFilterHandlers()` (@samdark, @vjik)
3838
- Enh #190: Use `str_contains` for case-sensitive match in `LikeHandler` (@samdark)
3939
- Enh #194: Improve psalm annotations in `LimitableDataInterface` (@vjik)
4040
- Bug #195: Fix invalid count in `IterableDataReader` when limit or/and offset used (@vjik)
@@ -64,6 +64,7 @@
6464
- Chg #233: Remove nullable types from `withFilter()` and `getFilter()` methods of `FilterableDataInterface` (@vjik)
6565
- Bug #234: Fix handling of `null` values in `IterableDataReader` (@vjik)
6666
- New #236: Add `PaginatorInterface::getFilter()` method (@vjik)
67+
- Chg #241: Remove `FilterHandlerInterface` interface (@vjik)
6768

6869
## 1.0.1 January 25, 2023
6970

src/Reader/FilterHandlerInterface.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

src/Reader/FilterableDataInterface.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,4 @@ public function withFilter(FilterInterface $filter): static;
3737
* @return FilterInterface Data reading criteria.
3838
*/
3939
public function getFilter(): FilterInterface;
40-
41-
/**
42-
* Returns new instance with additional handlers set.
43-
*
44-
* @param FilterHandlerInterface ...$filterHandlers Additional filter handlers.
45-
*
46-
* @return static New instance.
47-
*/
48-
public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandlers): static;
4940
}

src/Reader/Iterable/IterableDataReader.php

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
use InvalidArgumentException;
99
use Traversable;
1010
use Yiisoft\Arrays\ArrayHelper;
11-
use Yiisoft\Data\Reader\DataReaderException;
1211
use Yiisoft\Data\Reader\DataReaderInterface;
1312
use Yiisoft\Data\Reader\Filter\All;
14-
use Yiisoft\Data\Reader\FilterHandlerInterface;
1513
use Yiisoft\Data\Reader\FilterInterface;
1614
use Yiisoft\Data\Reader\Iterable\FilterHandler\AllHandler;
1715
use Yiisoft\Data\Reader\Iterable\FilterHandler\AndXHandler;
@@ -34,7 +32,6 @@
3432
use function array_merge;
3533
use function count;
3634
use function iterator_to_array;
37-
use function sprintf;
3835
use function uasort;
3936

4037
/**
@@ -97,10 +94,7 @@ public function __construct(
9794
$this->filter = new All();
9895
}
9996

100-
/**
101-
* @psalm-return $this
102-
*/
103-
public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandlers): static
97+
public function withAddedFilterHandlers(IterableFilterHandlerInterface ...$filterHandlers): self
10498
{
10599
$new = clone $this;
106100
$new->context = new Context(
@@ -294,28 +288,17 @@ static function (array|object $itemA, array|object $itemB) use ($criteria) {
294288
}
295289

296290
/**
297-
* @param FilterHandlerInterface[] $filterHandlers
291+
* @param IterableFilterHandlerInterface[] $filterHandlers
298292
*
299293
* @return IterableFilterHandlerInterface[]
300294
* @psalm-return array<string, IterableFilterHandlerInterface>
301295
*/
302296
private function prepareFilterHandlers(array $filterHandlers): array
303297
{
304298
$result = [];
305-
306299
foreach ($filterHandlers as $filterHandler) {
307-
if (!$filterHandler instanceof IterableFilterHandlerInterface) {
308-
throw new DataReaderException(
309-
sprintf(
310-
'%s::withFilterHandlers() accepts instances of %s only.',
311-
self::class,
312-
IterableFilterHandlerInterface::class,
313-
),
314-
);
315-
}
316300
$result[$filterHandler->getFilterClass()] = $filterHandler;
317301
}
318-
319302
return $result;
320303
}
321304

src/Reader/Iterable/IterableFilterHandlerInterface.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,13 @@
44

55
namespace Yiisoft\Data\Reader\Iterable;
66

7-
use Yiisoft\Data\Reader\FilterHandlerInterface;
87
use Yiisoft\Data\Reader\FilterInterface;
98

109
/**
1110
* Iterable filter handler checks whether an item matches criteria defined
1211
* in the filter with the same operator.
1312
*/
14-
interface IterableFilterHandlerInterface extends FilterHandlerInterface
13+
interface IterableFilterHandlerInterface
1514
{
1615
/**
1716
* Check whether an item matches iterable filter handlers
@@ -23,4 +22,15 @@ interface IterableFilterHandlerInterface extends FilterHandlerInterface
2322
* @return bool Whether item matches the filter.
2423
*/
2524
public function match(array|object $item, FilterInterface $filter, Context $context): bool;
25+
26+
/**
27+
* Get matching filter class name.
28+
*
29+
* If the filter is active, a corresponding handler will be used during matching.
30+
*
31+
* @return string The filter class name.
32+
*
33+
* @psalm-return class-string<FilterInterface>
34+
*/
35+
public function getFilterClass(): string;
2636
}

tests/Paginator/KeysetPaginatorTest.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Yiisoft\Data\Reader\Filter\LessThan;
2121
use Yiisoft\Data\Reader\Filter\LessThanOrEqual;
2222
use Yiisoft\Data\Reader\FilterableDataInterface;
23-
use Yiisoft\Data\Reader\FilterHandlerInterface;
2423
use Yiisoft\Data\Reader\FilterInterface;
2524
use Yiisoft\Data\Reader\Iterable\IterableDataReader;
2625
use Yiisoft\Data\Reader\LimitableDataInterface;
@@ -126,11 +125,6 @@ public function withFilter(?FilterInterface $filter): static
126125
return clone $this;
127126
}
128127

129-
public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandlers): static
130-
{
131-
return clone $this;
132-
}
133-
134128
public function getFilter(): FilterInterface
135129
{
136130
return new All();
@@ -1175,11 +1169,6 @@ public function withFilter(?FilterInterface $filter): static
11751169
return clone $this;
11761170
}
11771171

1178-
public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandlers): static
1179-
{
1180-
return clone $this;
1181-
}
1182-
11831172
public function getFilter(): FilterInterface
11841173
{
11851174
return new All();

tests/Reader/Iterable/IterableDataReaderTest.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Generator;
99
use InvalidArgumentException;
1010
use LogicException;
11-
use Yiisoft\Data\Reader\DataReaderException;
1211
use Yiisoft\Data\Reader\Filter\All;
1312
use Yiisoft\Data\Reader\Filter\AndX;
1413
use Yiisoft\Data\Reader\Filter\OrX;
@@ -20,7 +19,6 @@
2019
use Yiisoft\Data\Reader\Filter\LessThanOrEqual;
2120
use Yiisoft\Data\Reader\Filter\Like;
2221
use Yiisoft\Data\Reader\Filter\Not;
23-
use Yiisoft\Data\Reader\FilterHandlerInterface;
2422
use Yiisoft\Data\Reader\FilterInterface;
2523
use Yiisoft\Data\Reader\Iterable\Context;
2624
use Yiisoft\Data\Reader\Iterable\IterableDataReader;
@@ -76,26 +74,6 @@ public function testImmutability(): void
7674
$this->assertNotSame($reader, $reader->withLimit(1));
7775
}
7876

79-
public function testExceptionOnPassingNonIterableFilters(): void
80-
{
81-
$nonIterableFilterHandler = new class implements FilterHandlerInterface {
82-
public function getFilterClass(): string
83-
{
84-
return '?';
85-
}
86-
};
87-
88-
$this->expectException(DataReaderException::class);
89-
$message = sprintf(
90-
'%s::withFilterHandlers() accepts instances of %s only.',
91-
IterableDataReader::class,
92-
IterableFilterHandlerInterface::class,
93-
);
94-
$this->expectExceptionMessage($message);
95-
96-
(new IterableDataReader([]))->withAddedFilterHandlers($nonIterableFilterHandler);
97-
}
98-
9977
public function testWithLimitFailForNegativeValues(): void
10078
{
10179
$this->expectException(InvalidArgumentException::class);

tests/Support/MutationDataReader.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use Closure;
88
use Yiisoft\Data\Reader\FilterableDataInterface;
9-
use Yiisoft\Data\Reader\FilterHandlerInterface;
109
use Yiisoft\Data\Reader\FilterInterface;
1110
use Yiisoft\Data\Reader\Iterable\IterableDataReader;
1211
use Yiisoft\Data\Reader\LimitableDataInterface;
@@ -32,13 +31,6 @@ public function withFilter(FilterInterface $filter): static
3231
return $new;
3332
}
3433

35-
public function withAddedFilterHandlers(FilterHandlerInterface ...$filterHandlers): static
36-
{
37-
$new = clone $this;
38-
$new->decorated = $this->decorated->withAddedFilterHandlers(...$filterHandlers);
39-
return $new;
40-
}
41-
4234
public function withLimit(?int $limit): static
4335
{
4436
$new = clone $this;

0 commit comments

Comments
 (0)