diff options
-rw-r--r-- | CHANGELOG.md | 6 | ||||
-rw-r--r-- | src/Modifier/QueryFilter.php | 102 | ||||
-rw-r--r-- | test/ReaderTest.php | 30 |
3 files changed, 20 insertions, 118 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index ce1c5d8..b2942a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,12 @@ All Notable changes to `League\Csv` will be documented in this file - `Controls::detectDelimiterList` - `Reader::query` - The `$newline` argument from `AbstractCsv::createFromString` +- `QueryFilter::removeFilter` +- `QueryFilter::removeSortBy` +- `QueryFilter::hasFilter` +- `QueryFilter::hasSortBy` +- `QueryFilter::clearFilter` +- `QueryFilter::clearSortBy` ## 7.2.0 - 2015-11-02 diff --git a/src/Modifier/QueryFilter.php b/src/Modifier/QueryFilter.php index 9435418..42d3d69 100644 --- a/src/Modifier/QueryFilter.php +++ b/src/Modifier/QueryFilter.php @@ -169,45 +169,6 @@ trait QueryFilter } /** - * Remove a callable from the collection - * - * @param callable $callable - * - * @return $this - */ - public function removeSortBy(callable $callable) - { - $res = array_search($callable, $this->iterator_sort_by, true); - unset($this->iterator_sort_by[$res]); - - return $this; - } - - /** - * Detect if the callable is already registered - * - * @param callable $callable - * - * @return bool - */ - public function hasSortBy(callable $callable) - { - return false !== array_search($callable, $this->iterator_sort_by, true); - } - - /** - * Remove all registered callable - * - * @return $this - */ - public function clearSortBy() - { - $this->iterator_sort_by = []; - - return $this; - } - - /** * Set the Iterator filter method * * @param callable $callable @@ -222,45 +183,6 @@ trait QueryFilter } /** - * Remove a filter from the callable collection - * - * @param callable $callable - * - * @return $this - */ - public function removeFilter(callable $callable) - { - $res = array_search($callable, $this->iterator_filters, true); - unset($this->iterator_filters[$res]); - - return $this; - } - - /** - * Detect if the callable filter is already registered - * - * @param callable $callable - * - * @return bool - */ - public function hasFilter(callable $callable) - { - return false !== array_search($callable, $this->iterator_filters, true); - } - - /** - * Remove all registered callable filter - * - * @return $this - */ - public function clearFilter() - { - $this->iterator_filters = []; - - return $this; - } - - /** * Return the Iterator without the BOM sequence * * @param Iterator $iterator @@ -271,7 +193,7 @@ trait QueryFilter { $bom = $this->getInputBom(); - return new MapIterator($iterator, function ($row, $index) use ($bom) { + $stripBom = function ($row, $index) use ($bom) { if (0 == $index) { $row[0] = mb_substr($row[0], mb_strlen($bom)); $enclosure = $this->getEnclosure(); @@ -282,7 +204,9 @@ trait QueryFilter } return $row; - }); + }; + + return new MapIterator($iterator, $stripBom); } /** @@ -297,9 +221,10 @@ trait QueryFilter */ protected function getQueryIterator() { - array_unshift($this->iterator_filters, function ($row) { + $normalizedCsv = function ($row) { return is_array($row) && $row != [null]; - }); + }; + array_unshift($this->iterator_filters, $normalizedCsv); $iterator = $this->getIterator(); $iterator = $this->applyBomStripping($iterator); $iterator = $this->applyIteratorFilter($iterator); @@ -352,7 +277,7 @@ trait QueryFilter foreach ($this->iterator_filters as $callable) { $iterator = new CallbackFilterIterator($iterator, $callable); } - $this->clearFilter(); + $this->iterator_filters = []; return $iterator; } @@ -369,7 +294,9 @@ trait QueryFilter if (!$this->iterator_sort_by) { return $iterator; } - $sortFunc = function ($rowA, $rowB) { + + $obj = new ArrayObject(iterator_to_array($iterator)); + $obj->uasort(function ($rowA, $rowB) { $sortRes = 0; foreach ($this->iterator_sort_by as $callable) { if (0 !== ($sortRes = call_user_func($callable, $rowA, $rowB))) { @@ -378,11 +305,8 @@ trait QueryFilter } return $sortRes; - }; - - $obj = new ArrayObject(iterator_to_array($iterator)); - $obj->uasort($sortFunc); - $this->clearSortBy(); + }); + $this->iterator_sort_by = []; return $obj->getIterator(); } diff --git a/test/ReaderTest.php b/test/ReaderTest.php index c9c3c91..1b4ebf9 100644 --- a/test/ReaderTest.php +++ b/test/ReaderTest.php @@ -95,28 +95,10 @@ class ReaderTest extends AbstractTestCase public function testFilter() { $func = function ($row) { - return ! in_array('jane', $row); + return !in_array('jane', $row); }; $this->csv->addFilter($func); $this->assertNotContains(['jane', 'doe', 'jane.doe@example.com'], $this->csv->fetchAll()); - - $func2 = function ($row) { - return ! in_array('john', $row); - }; - $this->csv->addFilter($func2); - $this->csv->addFilter($func); - $res = $this->csv->fetchAll(); - - $this->assertNotContains(['john', 'doe', 'john.doe@example.com'], $res); - $this->assertNotContains(['jane', 'doe', 'jane.doe@example.com'], $res); - - $this->csv->addFilter($func2); - $this->csv->addFilter($func); - $this->assertTrue($this->csv->hasFilter($func2)); - $this->csv->removeFilter($func2); - $this->assertFalse($this->csv->hasFilter($func2)); - - $this->assertContains(['john', 'doe', 'john.doe@example.com'], $this->csv->fetchAll()); } public function testSortBy() @@ -130,16 +112,6 @@ class ReaderTest extends AbstractTestCase }); $this->assertSame(array_reverse($this->expected), $this->csv->fetchAll()); - - $this->csv->addSortBy($func); - $this->csv->addSortBy($func); - $this->csv->removeSortBy($func); - $this->assertTrue($this->csv->hasSortBy($func)); - $this->csv->addFilter(function ($row) { - return $row != [null]; - - }); - $this->assertSame(array_reverse($this->expected), $this->csv->fetchAll()); } public function testFetchAll() |