Skip to content

Commit

Permalink
remove useless QueryFilter methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 1, 2015
1 parent ca4c7f6 commit 3d35e2f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 118 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -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

Expand Down
102 changes: 13 additions & 89 deletions src/Modifier/QueryFilter.php
Expand Up @@ -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
*
Expand All @@ -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
*
Expand All @@ -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();
Expand All @@ -282,7 +204,9 @@ protected function getStripBomIterator(Iterator $iterator)
}

return $row;
});
};

return new MapIterator($iterator, $stripBom);
}

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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))) {
Expand All @@ -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();
}
Expand Down
30 changes: 1 addition & 29 deletions test/ReaderTest.php
Expand Up @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 3d35e2f

Please sign in to comment.