diff options
-rw-r--r-- | src/Iterator/IteratorFilter.php | 60 | ||||
-rw-r--r-- | test/Iterator/IteratorQueryTest.php | 23 |
2 files changed, 72 insertions, 11 deletions
diff --git a/src/Iterator/IteratorFilter.php b/src/Iterator/IteratorFilter.php index b313f39..e6e254e 100644 --- a/src/Iterator/IteratorFilter.php +++ b/src/Iterator/IteratorFilter.php @@ -47,38 +47,82 @@ trait IteratorFilter /** * Callable function to filter the iterator * - * @var callable + * @var array */ - private $filter; + private $filter = []; /** * Set the Iterator filter method * + * DEPRECATION WARNING! This method will be removed in the next major point release + * + * @deprecated deprecated since version 5.1 + * * @param callable $filter * * @return self */ public function setFilter(callable $filter) { - $this->filter = $filter; + return $this->addFilter($filter); + } + + /** + * Set the Iterator filter method + * + * @param callable $filter + * + * @return self + */ + public function addFilter(callable $filter) + { + $this->filter[] = $filter; + + return $this; + } + + /** + * Remove a filter from the callable collection + * + * @param callable $filter + * + * @return self + */ + public function removeFilter(callable $filter) + { + $res = array_search($filter, $this->filter, true); + if (false !== $res) { + unset($this->filter[$res]); + } return $this; } /** + * Detect if the callable filter is already registered + * + * @param callable $filter + * + * @return boolean + */ + public function hasFilter(callable $filter) + { + return false !== array_search($filter, $this->filter, true); + } + + /** * Filter the Iterator * * @param \Iterator $iterator * - * @return \CallbackFilterIterator + * @return \Iterator */ protected function applyFilter(Iterator $iterator) { - if (! $this->filter) { - return $iterator; + foreach ($this->filter as $callable) { + $iterator = new CallbackFilterIterator($iterator, $callable); } - $iterator = new CallbackFilterIterator($iterator, $this->filter); - $this->filter = null; + $this->filter = []; return $iterator; } diff --git a/test/Iterator/IteratorQueryTest.php b/test/Iterator/IteratorQueryTest.php index 1f1953e..d44847b 100644 --- a/test/Iterator/IteratorQueryTest.php +++ b/test/Iterator/IteratorQueryTest.php @@ -83,13 +83,30 @@ class IteratorQueryTest extends PHPUnit_Framework_TestCase public function testFilter() { $func = function ($row) { - return $row == 'john'; + return false !== strpos($row, 'o'); }; $this->traitQuery->setFilter($func); $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]); - $res = iterator_to_array($iterator); - $this->assertCount(1, $res); + $this->assertCount(2, iterator_to_array($iterator, false)); + + $func2 = function ($row) { + return false !== strpos($row, 'j'); + }; + $this->traitQuery->addFilter($func2); + $this->traitQuery->addFilter($func); + + $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]); + $this->assertCount(1, iterator_to_array($iterator, false)); + + $this->traitQuery->addFilter($func2); + $this->traitQuery->addFilter($func); + $this->assertTrue($this->traitQuery->hasFilter($func2)); + $this->traitQuery->removeFilter($func2); + $this->assertFalse($this->traitQuery->hasFilter($func2)); + + $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]); + $this->assertCount(2, iterator_to_array($iterator, false)); } public function testSortBy() |