1414use Yiisoft \Data \Cycle \Reader \FilterHandler \LikeHandler \LikeHandlerFactory ;
1515use Yiisoft \Data \Reader \DataReaderInterface ;
1616use Yiisoft \Data \Reader \Filter \All ;
17- use Yiisoft \Data \Reader \FilterHandlerInterface ;
1817use Yiisoft \Data \Reader \FilterInterface ;
1918use Yiisoft \Data \Reader \Sort ;
2019use Yiisoft \Data \Cycle \Reader \Cache \CachedCollection ;
@@ -40,11 +39,14 @@ final class EntityReader implements DataReaderInterface
4039 private CachedCollection $ itemsCache ;
4140 private CachedCollection $ oneItemCache ;
4241 /**
43- * @psalm-var array<class-string, FilterHandlerInterface & QueryBuilderFilterHandler> $handlers
42+ * @psalm-var array<class-string, QueryBuilderFilterHandler> $handlers
4443 */
4544 private array $ filterHandlers = [];
4645
47- public function __construct (Select |SelectQuery $ query )
46+ /**
47+ * @param QueryBuilderFilterHandler[] $extraFilterHandlers
48+ **/
49+ public function __construct (Select |SelectQuery $ query , array $ extraFilterHandlers = [])
4850 {
4951 $ this ->query = clone $ query ;
5052 $ this ->countCache = new CachedCount ($ this ->query );
@@ -70,6 +72,7 @@ public function __construct(Select|SelectQuery $query)
7072 new FilterHandler \LessThanOrEqualHandler (),
7173 $ likeHandler ,
7274 new FilterHandler \NotHandler (),
75+ ...$ extraFilterHandlers ,
7376 );
7477
7578 $ this ->filter = new All ();
@@ -145,22 +148,6 @@ public function withFilter(FilterInterface $filter): static
145148 return $ new ;
146149 }
147150
148- /**
149- * @psalm-mutation-free
150- */
151- #[\Override]
152- public function withAddedFilterHandlers (FilterHandlerInterface ...$ filterHandlers ): static
153- {
154- $ new = clone $ this ;
155- /** @psalm-suppress ImpureMethodCall */
156- $ new ->setFilterHandlers (...$ filterHandlers );
157- /** @psalm-suppress ImpureMethodCall */
158- $ new ->resetCountCache ();
159- $ new ->itemsCache = new CachedCollection ();
160- $ new ->oneItemCache = new CachedCollection ();
161- return $ new ;
162- }
163-
164151 #[\Override]
165152 public function count (): int
166153 {
@@ -207,13 +194,11 @@ public function getSql(): string
207194 return (string )($ query instanceof Select ? $ query ->buildQuery () : $ query );
208195 }
209196
210- private function setFilterHandlers (FilterHandlerInterface ...$ filterHandlers ): void
197+ private function setFilterHandlers (QueryBuilderFilterHandler ...$ filterHandlers ): void
211198 {
212199 $ handlers = [];
213200 foreach ($ filterHandlers as $ filterHandler ) {
214- if ($ filterHandler instanceof QueryBuilderFilterHandler) {
215- $ handlers [$ filterHandler ->getFilterClass ()] = $ filterHandler ;
216- }
201+ $ handlers [$ filterHandler ->getFilterClass ()] = $ filterHandler ;
217202 }
218203 $ this ->filterHandlers = array_merge ($ this ->filterHandlers , $ handlers );
219204 }
@@ -238,7 +223,7 @@ private function buildSelectQuery(): SelectQuery|Select
238223
239224 private function makeFilterClosure (FilterInterface $ filter ): Closure
240225 {
241- return function (QueryBuilder $ select ) use ($ filter ) {
226+ return function (QueryBuilder | SelectQuery $ select ) use ($ filter ) {
242227 if (!array_key_exists ($ filter ::class, $ this ->filterHandlers )) {
243228 throw new NotSupportedFilterException ($ filter ::class);
244229 }
0 commit comments