Skip to content

Commit 3d35e2f

Browse files
committed
remove useless QueryFilter methods
1 parent ca4c7f6 commit 3d35e2f

File tree

3 files changed

+20
-118
lines changed

3 files changed

+20
-118
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ All Notable changes to `League\Csv` will be documented in this file
2424
- `Controls::detectDelimiterList`
2525
- `Reader::query`
2626
- The `$newline` argument from `AbstractCsv::createFromString`
27+
- `QueryFilter::removeFilter`
28+
- `QueryFilter::removeSortBy`
29+
- `QueryFilter::hasFilter`
30+
- `QueryFilter::hasSortBy`
31+
- `QueryFilter::clearFilter`
32+
- `QueryFilter::clearSortBy`
2733

2834
## 7.2.0 - 2015-11-02
2935

src/Modifier/QueryFilter.php

Lines changed: 13 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -168,45 +168,6 @@ public function addSortBy(callable $callable)
168168
return $this;
169169
}
170170

171-
/**
172-
* Remove a callable from the collection
173-
*
174-
* @param callable $callable
175-
*
176-
* @return $this
177-
*/
178-
public function removeSortBy(callable $callable)
179-
{
180-
$res = array_search($callable, $this->iterator_sort_by, true);
181-
unset($this->iterator_sort_by[$res]);
182-
183-
return $this;
184-
}
185-
186-
/**
187-
* Detect if the callable is already registered
188-
*
189-
* @param callable $callable
190-
*
191-
* @return bool
192-
*/
193-
public function hasSortBy(callable $callable)
194-
{
195-
return false !== array_search($callable, $this->iterator_sort_by, true);
196-
}
197-
198-
/**
199-
* Remove all registered callable
200-
*
201-
* @return $this
202-
*/
203-
public function clearSortBy()
204-
{
205-
$this->iterator_sort_by = [];
206-
207-
return $this;
208-
}
209-
210171
/**
211172
* Set the Iterator filter method
212173
*
@@ -221,45 +182,6 @@ public function addFilter(callable $callable)
221182
return $this;
222183
}
223184

224-
/**
225-
* Remove a filter from the callable collection
226-
*
227-
* @param callable $callable
228-
*
229-
* @return $this
230-
*/
231-
public function removeFilter(callable $callable)
232-
{
233-
$res = array_search($callable, $this->iterator_filters, true);
234-
unset($this->iterator_filters[$res]);
235-
236-
return $this;
237-
}
238-
239-
/**
240-
* Detect if the callable filter is already registered
241-
*
242-
* @param callable $callable
243-
*
244-
* @return bool
245-
*/
246-
public function hasFilter(callable $callable)
247-
{
248-
return false !== array_search($callable, $this->iterator_filters, true);
249-
}
250-
251-
/**
252-
* Remove all registered callable filter
253-
*
254-
* @return $this
255-
*/
256-
public function clearFilter()
257-
{
258-
$this->iterator_filters = [];
259-
260-
return $this;
261-
}
262-
263185
/**
264186
* Return the Iterator without the BOM sequence
265187
*
@@ -271,7 +193,7 @@ protected function getStripBomIterator(Iterator $iterator)
271193
{
272194
$bom = $this->getInputBom();
273195

274-
return new MapIterator($iterator, function ($row, $index) use ($bom) {
196+
$stripBom = function ($row, $index) use ($bom) {
275197
if (0 == $index) {
276198
$row[0] = mb_substr($row[0], mb_strlen($bom));
277199
$enclosure = $this->getEnclosure();
@@ -282,7 +204,9 @@ protected function getStripBomIterator(Iterator $iterator)
282204
}
283205

284206
return $row;
285-
});
207+
};
208+
209+
return new MapIterator($iterator, $stripBom);
286210
}
287211

288212
/**
@@ -297,9 +221,10 @@ abstract public function getEnclosure();
297221
*/
298222
protected function getQueryIterator()
299223
{
300-
array_unshift($this->iterator_filters, function ($row) {
224+
$normalizedCsv = function ($row) {
301225
return is_array($row) && $row != [null];
302-
});
226+
};
227+
array_unshift($this->iterator_filters, $normalizedCsv);
303228
$iterator = $this->getIterator();
304229
$iterator = $this->applyBomStripping($iterator);
305230
$iterator = $this->applyIteratorFilter($iterator);
@@ -352,7 +277,7 @@ protected function applyIteratorFilter(Iterator $iterator)
352277
foreach ($this->iterator_filters as $callable) {
353278
$iterator = new CallbackFilterIterator($iterator, $callable);
354279
}
355-
$this->clearFilter();
280+
$this->iterator_filters = [];
356281

357282
return $iterator;
358283
}
@@ -369,7 +294,9 @@ protected function applyIteratorSortBy(Iterator $iterator)
369294
if (!$this->iterator_sort_by) {
370295
return $iterator;
371296
}
372-
$sortFunc = function ($rowA, $rowB) {
297+
298+
$obj = new ArrayObject(iterator_to_array($iterator));
299+
$obj->uasort(function ($rowA, $rowB) {
373300
$sortRes = 0;
374301
foreach ($this->iterator_sort_by as $callable) {
375302
if (0 !== ($sortRes = call_user_func($callable, $rowA, $rowB))) {
@@ -378,11 +305,8 @@ protected function applyIteratorSortBy(Iterator $iterator)
378305
}
379306

380307
return $sortRes;
381-
};
382-
383-
$obj = new ArrayObject(iterator_to_array($iterator));
384-
$obj->uasort($sortFunc);
385-
$this->clearSortBy();
308+
});
309+
$this->iterator_sort_by = [];
386310

387311
return $obj->getIterator();
388312
}

test/ReaderTest.php

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -95,28 +95,10 @@ public function testIntervalThrowException()
9595
public function testFilter()
9696
{
9797
$func = function ($row) {
98-
return ! in_array('jane', $row);
98+
return !in_array('jane', $row);
9999
};
100100
$this->csv->addFilter($func);
101101
$this->assertNotContains(['jane', 'doe', '[email protected]'], $this->csv->fetchAll());
102-
103-
$func2 = function ($row) {
104-
return ! in_array('john', $row);
105-
};
106-
$this->csv->addFilter($func2);
107-
$this->csv->addFilter($func);
108-
$res = $this->csv->fetchAll();
109-
110-
$this->assertNotContains(['john', 'doe', '[email protected]'], $res);
111-
$this->assertNotContains(['jane', 'doe', '[email protected]'], $res);
112-
113-
$this->csv->addFilter($func2);
114-
$this->csv->addFilter($func);
115-
$this->assertTrue($this->csv->hasFilter($func2));
116-
$this->csv->removeFilter($func2);
117-
$this->assertFalse($this->csv->hasFilter($func2));
118-
119-
$this->assertContains(['john', 'doe', '[email protected]'], $this->csv->fetchAll());
120102
}
121103

122104
public function testSortBy()
@@ -130,16 +112,6 @@ public function testSortBy()
130112

131113
});
132114
$this->assertSame(array_reverse($this->expected), $this->csv->fetchAll());
133-
134-
$this->csv->addSortBy($func);
135-
$this->csv->addSortBy($func);
136-
$this->csv->removeSortBy($func);
137-
$this->assertTrue($this->csv->hasSortBy($func));
138-
$this->csv->addFilter(function ($row) {
139-
return $row != [null];
140-
141-
});
142-
$this->assertSame(array_reverse($this->expected), $this->csv->fetchAll());
143115
}
144116

145117
public function testFetchAll()

0 commit comments

Comments
 (0)