1313 */
1414class ChainableFilter extends AbstractFilter implements FilterableInterface
1515{
16- /**
17- * @var FilterInterface[] $filters Array of filter instances composing the chain.
18- */
19- protected iterable $ filters ;
20-
16+ use Filterable;
17+
2118 /**
2219 * Constructor.
2320 *
@@ -31,41 +28,7 @@ public function __construct(
3128 foreach ($ filters as $ filter ) $ this ->addFilter ($ filter );
3229 parent ::__construct ($ iterable === [] ? $ filters : $ iterable );
3330 }
34-
35- /**
36- * Returns a new ChainableFilter instance with an additional filter added.
37- *
38- * The $prepend parameter determines whether the new filter is added at the beginning (applied first)
39- * or appended to the end of the chain (applied last).
40- *
41- * @param FilterInterface $filter The new filter to add.
42- * @param bool $prepend If true, the new filter is added at the beginning of the chain.
43- * @return ChainableFilter Returns a new FilterChain instance with the updated filter list.
44- */
45- public function withFilter (FilterInterface $ filter , bool $ prepend = false ): ChainableFilter
46- {
47- $ filters = $ this ->filters ;
48-
49- if ($ prepend ) array_unshift ($ filters , $ filter );
50- else $ filters [] = $ filter ;
51-
52- $ copy = clone $ this ;
53- $ copy ->filters = $ filters ;
54-
55- return $ copy ;
56- }
57-
58- /**
59- * Checks whether the given filter exists in the chain.
60- *
61- * @param FilterInterface $filter The filter to search for.
62- * @return bool Returns true if the filter is found in the chain; otherwise, false.
63- */
64- public function hasFilter (FilterInterface $ filter ): bool
65- {
66- return in_array ($ filter , $ this ->filters , true );
67- }
68-
31+
6932 /**
7033 * Adds a filter to the chain.
7134 *
@@ -77,37 +40,4 @@ private function addFilter(FilterInterface $filter): void
7740 {
7841 $ this ->filters [] = $ filter ;
7942 }
80-
81- /**
82- * Determines whether a given element should be accepted by the filter chain.
83- *
84- * The element is accepted only if every filter in the chain returns true for its key and value.
85- *
86- * @param mixed $value The element to be evaluated.
87- * @param string|int|null $key The key associated with the value.
88- * @return bool Returns true if all filters accept the element; false if any one rejects it.
89- */
90- public function accept (mixed $ value , string |int |null $ key = null ): bool
91- {
92- // Note: array_all() is assumed to be a helper function that returns true only if the provided callback
93- // returns true for every element in the iterable.
94- return array_all ($ this ->filters , static fn ($ filter ) => $ filter ->accept ($ value , $ key ));
95- }
96-
97- /**
98- * Returns a new ChainableFilter instance without the specified filter.
99- *
100- * This method removes the given filter (using strict inequality) from the current chain
101- * and returns a new instance with the updated filter list.
102- *
103- * @param FilterInterface $filter The filter to be removed.
104- * @return ChainableFilter Returns a new ChainableFilter instance without the specified filter.
105- */
106- public function withoutFilter (FilterInterface $ filter ): ChainableFilter
107- {
108- $ copy = clone $ this ;
109- $ copy ->filters = array_filter ($ this ->filters , static fn ($ f ) => $ f !== $ filter );
110-
111- return $ copy ;
112- }
113- }
43+ }
0 commit comments