summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgnace Nyamagana Butera <nyamsprod@gmail.com>2014-03-05 14:11:53 +0100
committerIgnace Nyamagana Butera <nyamsprod@gmail.com>2014-03-10 12:30:16 +0100
commit5a5200ff9d13b6a1e82c279f0a83df095aec94a4 (patch)
treeb7ab840638748aa732c4ef4260454b7fd563e035
parentb311ed3d328f538038bb363c920d59831c461634 (diff)
downloadcsv-5a5200ff9d13b6a1e82c279f0a83df095aec94a4.zip
csv-5a5200ff9d13b6a1e82c279f0a83df095aec94a4.tar.gz
csv-5a5200ff9d13b6a1e82c279f0a83df095aec94a4.tar.bz2
you can add/remove multiple filter
Improve IteratorFilter::applyFilter method #11
-rw-r--r--src/Iterator/IteratorFilter.php60
-rw-r--r--test/Iterator/IteratorQueryTest.php23
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()