From 3d35e2f5aede98fc8d19d0472ea8c9673d0a7f99 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Tue, 1 Dec 2015 09:48:55 +0100 Subject: [PATCH] remove useless QueryFilter methods --- CHANGELOG.md | 6 +++ src/Modifier/QueryFilter.php | 102 +++++------------------------------ test/ReaderTest.php | 30 +---------- 3 files changed, 20 insertions(+), 118 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ce1c5d8d..b2942a60 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 94354182..42d3d698 100644 --- a/src/Modifier/QueryFilter.php +++ b/src/Modifier/QueryFilter.php @@ -168,45 +168,6 @@ public function addSortBy(callable $callable) return $this; } - /** - * 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 * @@ -221,45 +182,6 @@ public function addFilter(callable $callable) return $this; } - /** - * 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 * @@ -271,7 +193,7 @@ protected function getStripBomIterator(Iterator $iterator) { $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 @@ protected function getStripBomIterator(Iterator $iterator) } return $row; - }); + }; + + return new MapIterator($iterator, $stripBom); } /** @@ -297,9 +221,10 @@ abstract public function getEnclosure(); */ 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 @@ protected function applyIteratorFilter(Iterator $iterator) foreach ($this->iterator_filters as $callable) { $iterator = new CallbackFilterIterator($iterator, $callable); } - $this->clearFilter(); + $this->iterator_filters = []; return $iterator; } @@ -369,7 +294,9 @@ protected function applyIteratorSortBy(Iterator $iterator) 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 @@ protected function applyIteratorSortBy(Iterator $iterator) } 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 c9c3c919..1b4ebf9f 100644 --- a/test/ReaderTest.php +++ b/test/ReaderTest.php @@ -95,28 +95,10 @@ public function testIntervalThrowException() 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 @@ public function testSortBy() }); $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()