summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgnace Nyamagana Butera <nyamsprod@gmail.com>2014-04-25 10:23:22 +0200
committerIgnace Nyamagana Butera <nyamsprod@gmail.com>2014-04-25 10:23:22 +0200
commit96f7e6bb3dee085f0496bbfcf5ebb565bd9f6220 (patch)
tree77f4db70d1c2fbf2a22fe59b423908c9a1ca320c
parent38ae3e0023671af23f54b1e62078a955579e57ed (diff)
downloadcsv-96f7e6bb3dee085f0496bbfcf5ebb565bd9f6220.zip
csv-96f7e6bb3dee085f0496bbfcf5ebb565bd9f6220.tar.gz
csv-96f7e6bb3dee085f0496bbfcf5ebb565bd9f6220.tar.bz2
remove IteratorQuery
-rw-r--r--src/Iterator/IteratorFilter.php14
-rw-r--r--src/Iterator/IteratorInterval.php18
-rw-r--r--src/Iterator/IteratorQuery.php80
-rw-r--r--src/Iterator/IteratorSortBy.php18
-rw-r--r--src/Reader.php42
-rw-r--r--test/Iterator/IteratorQueryTest.php136
-rw-r--r--test/ReaderTest.php78
7 files changed, 141 insertions, 245 deletions
diff --git a/src/Iterator/IteratorFilter.php b/src/Iterator/IteratorFilter.php
index bd44d10..17a8230 100644
--- a/src/Iterator/IteratorFilter.php
+++ b/src/Iterator/IteratorFilter.php
@@ -49,7 +49,7 @@ trait IteratorFilter
*
* @var array
*/
- private $filter = [];
+ protected $iterator_filters = [];
/**
* Set the Iterator filter method
@@ -76,7 +76,7 @@ trait IteratorFilter
*/
public function addFilter(callable $callable)
{
- $this->filter[] = $callable;
+ $this->iterator_filters[] = $callable;
return $this;
}
@@ -90,9 +90,9 @@ trait IteratorFilter
*/
public function removeFilter(callable $callable)
{
- $res = array_search($callable, $this->filter, true);
+ $res = array_search($callable, $this->iterator_filters, true);
if (false !== $res) {
- unset($this->filter[$res]);
+ unset($this->iterator_filters[$res]);
}
return $this;
@@ -107,7 +107,7 @@ trait IteratorFilter
*/
public function hasFilter(callable $callable)
{
- return false !== array_search($callable, $this->filter, true);
+ return false !== array_search($callable, $this->iterator_filters, true);
}
/**
@@ -117,7 +117,7 @@ trait IteratorFilter
*/
public function clearFilter()
{
- $this->filter = [];
+ $this->iterator_filters = [];
return $this;
}
@@ -131,7 +131,7 @@ trait IteratorFilter
*/
protected function applyFilter(Iterator $iterator)
{
- foreach ($this->filter as $callable) {
+ foreach ($this->iterator_filters as $callable) {
$iterator = new CallbackFilterIterator($iterator, $callable);
}
$this->clearFilter();
diff --git a/src/Iterator/IteratorInterval.php b/src/Iterator/IteratorInterval.php
index a77b90c..bdd339e 100644
--- a/src/Iterator/IteratorInterval.php
+++ b/src/Iterator/IteratorInterval.php
@@ -50,14 +50,14 @@ trait IteratorInterval
*
* @var integer
*/
- private $offset = 0;
+ protected $iterator_offset = 0;
/**
* iterator maximum length
*
* @var integer
*/
- private $limit = -1;
+ protected $iterator_limit = -1;
/**
* Set LimitIterator Offset
@@ -71,7 +71,7 @@ trait IteratorInterval
if (false === filter_var($offset, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
throw new InvalidArgumentException('the offset must be a positive integer or 0');
}
- $this->offset = $offset;
+ $this->iterator_offset = $offset;
return $this;
}
@@ -88,7 +88,7 @@ trait IteratorInterval
if (false === filter_var($limit, FILTER_VALIDATE_INT, ['options' => ['min_range' => -1]])) {
throw new InvalidArgumentException('the limit must an integer greater or equals to -1');
}
- $this->limit = $limit;
+ $this->iterator_limit = $limit;
return $this;
}
@@ -102,14 +102,14 @@ trait IteratorInterval
*/
protected function applyInterval(Iterator $iterator)
{
- if (0 == $this->offset && -1 == $this->limit) {
+ if (0 == $this->iterator_offset && -1 == $this->iterator_limit) {
return $iterator;
}
- $offset = $this->offset;
- $limit = $this->limit;
+ $offset = $this->iterator_offset;
+ $limit = $this->iterator_limit;
- $this->limit = -1;
- $this->offset = 0;
+ $this->iterator_limit = -1;
+ $this->iterator_offset = 0;
return new LimitIterator($iterator, $offset, $limit);
}
diff --git a/src/Iterator/IteratorQuery.php b/src/Iterator/IteratorQuery.php
deleted file mode 100644
index 5ec12e2..0000000
--- a/src/Iterator/IteratorQuery.php
+++ /dev/null
@@ -1,80 +0,0 @@
-<?php
-/**
-* League.csv - A CSV data manipulation library
-*
-* @author Ignace Nyamagana Butera <nyamsprod@gmail.com>
-* @copyright 2014 Ignace Nyamagana Butera
-* @link https://github.com/thephpleague/csv/
-* @license http://opensource.org/licenses/MIT
-* @version 5.4.0
-* @package League.csv
-*
-* MIT LICENSE
-*
-* Permission is hereby granted, free of charge, to any person obtaining
-* a copy of this software and associated documentation files (the
-* "Software"), to deal in the Software without restriction, including
-* without limitation the rights to use, copy, modify, merge, publish,
-* distribute, sublicense, and/or sell copies of the Software, and to
-* permit persons to whom the Software is furnished to do so, subject to
-* the following conditions:
-*
-* The above copyright notice and this permission notice shall be
-* included in all copies or substantial portions of the Software.
-*
-* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-*/
-namespace League\Csv\Iterator;
-
-use Iterator;
-
-/**
- * A Trait to Query in a SQL-like manner Iterators
- *
- * @package League.csv
- * @since 4.0.0
- *
- */
-trait IteratorQuery
-{
- /**
- * Iterator Filtering Trait
- */
- use IteratorFilter;
-
- /**
- * Iterator Sorting Trait
- */
- use IteratorSortBy;
-
- /**
- * Iterator Set Interval Trait
- */
- use IteratorInterval;
-
- /**
- * Return a filtered Iterator based on the filtering settings
- *
- * @param Iterator $iterator The iterator to be filtered
- * @param callable $callable a callable function to be applied to each Iterator item
- *
- * @return Iterator
- */
- protected function execute(Iterator $iterator, callable $callable = null)
- {
- $iterator = $this->applyFilter($iterator);
- $iterator = $this->applySortBy($iterator);
- $iterator = $this->applyInterval($iterator);
- if (! is_null($callable)) {
- $iterator = new MapIterator($iterator, $callable);
- }
-
- return $iterator;
- }
-}
diff --git a/src/Iterator/IteratorSortBy.php b/src/Iterator/IteratorSortBy.php
index 9a64d92..3965f31 100644
--- a/src/Iterator/IteratorSortBy.php
+++ b/src/Iterator/IteratorSortBy.php
@@ -49,7 +49,7 @@ trait IteratorSortBy
*
* @var callable
*/
- private $sortBy = [];
+ protected $iterator_sort_by = [];
/**
* Set the Iterator SortBy method
@@ -68,7 +68,7 @@ trait IteratorSortBy
}
/**
- * Set an Iterator sortBy method
+ * Set an Iterator $this->iterator_sort_by method
*
* @param callable $filter
*
@@ -76,7 +76,7 @@ trait IteratorSortBy
*/
public function addSortBy(callable $callable)
{
- $this->sortBy[] = $callable;
+ $this->iterator_sort_by[] = $callable;
return $this;
}
@@ -90,9 +90,9 @@ trait IteratorSortBy
*/
public function removeSortBy(callable $callable)
{
- $res = array_search($callable, $this->sortBy, true);
+ $res = array_search($callable, $this->iterator_sort_by, true);
if (false !== $res) {
- unset($this->sortBy[$res]);
+ unset($this->iterator_sort_by[$res]);
}
return $this;
@@ -107,7 +107,7 @@ trait IteratorSortBy
*/
public function hasSortBy(callable $callable)
{
- return false !== array_search($callable, $this->sortBy, true);
+ return false !== array_search($callable, $this->iterator_sort_by, true);
}
/**
@@ -117,7 +117,7 @@ trait IteratorSortBy
*/
public function clearSortBy()
{
- $this->sortBy = [];
+ $this->iterator_sort_by = [];
return $this;
}
@@ -131,13 +131,13 @@ trait IteratorSortBy
*/
protected function applySortBy(Iterator $iterator)
{
- if (! $this->sortBy) {
+ if (! $this->iterator_sort_by) {
return $iterator;
}
$res = iterator_to_array($iterator, false);
uasort($res, function ($rowA, $rowB) {
- foreach ($this->sortBy as $callable) {
+ foreach ($this->iterator_sort_by as $callable) {
$res = $callable($rowA, $rowB);
if (0 !== $res) {
break;
diff --git a/src/Reader.php b/src/Reader.php
index 0ee9038..e714f43 100644
--- a/src/Reader.php
+++ b/src/Reader.php
@@ -34,9 +34,12 @@ namespace League\Csv;
use InvalidArgumentException;
+use Iterator;
use CallbackFilterIterator;
use League\Csv\Iterator\MapIterator;
-use League\Csv\Iterator\IteratorQuery;
+use League\Csv\Iterator\IteratorFilter;
+use League\Csv\Iterator\IteratorSortBy;
+use League\Csv\Iterator\IteratorInterval;
/**
* A class to manage extracting and filtering a CSV
@@ -48,10 +51,23 @@ use League\Csv\Iterator\IteratorQuery;
class Reader extends AbstractCsv
{
/**
- * Iterator Query Trait
+ * Iterator Filtering Trait
*/
- use IteratorQuery;
+ use IteratorFilter;
+ /**
+ * Iterator Sorting Trait
+ */
+ use IteratorSortBy;
+
+ /**
+ * Iterator Set Interval Trait
+ */
+ use IteratorInterval;
+
+ /**
+ * {@ihneritdoc}
+ */
protected $stream_filter_mode = STREAM_FILTER_READ;
/**
@@ -76,6 +92,26 @@ class Reader extends AbstractCsv
}
/**
+ * Return a filtered Iterator based on the filtering settings
+ *
+ * @param Iterator $iterator The iterator to be filtered
+ * @param callable $callable a callable function to be applied to each Iterator item
+ *
+ * @return Iterator
+ */
+ protected function execute(Iterator $iterator, callable $callable = null)
+ {
+ $iterator = $this->applyFilter($iterator);
+ $iterator = $this->applySortBy($iterator);
+ $iterator = $this->applyInterval($iterator);
+ if (! is_null($callable)) {
+ $iterator = new MapIterator($iterator, $callable);
+ }
+
+ return $iterator;
+ }
+
+ /**
* Return a Filtered Iterator
*
* @param callable $callable a callable function to be applied to each Iterator item
diff --git a/test/Iterator/IteratorQueryTest.php b/test/Iterator/IteratorQueryTest.php
deleted file mode 100644
index d47ae1d..0000000
--- a/test/Iterator/IteratorQueryTest.php
+++ /dev/null
@@ -1,136 +0,0 @@
-<?php
-
-namespace League\Csv\test\Iterator;
-
-use ArrayIterator;
-use ReflectionClass;
-use PHPUnit_Framework_TestCase;
-use League\Csv\Iterator\IteratorQuery;
-
-/**
- * @group iterator
- */
-class IteratorQueryTest extends PHPUnit_Framework_TestCase
-{
- private $traitQuery;
- private $iterator;
- private $data = ['john', 'jane', 'foo', 'bar'];
-
- public function invokeMethod(&$object, $methodName, array $parameters = [])
- {
- $reflection = new ReflectionClass(get_class($object));
- $method = $reflection->getMethod($methodName);
- $method->setAccessible(true);
-
- return $method->invokeArgs($object, $parameters);
- }
-
- private function createTraitObject()
- {
- return $this->getObjectForTrait('\League\Csv\Iterator\IteratorQuery');
- }
-
- public function setUp()
- {
- $this->traitQuery = $this->createTraitObject();
- $this->iterator = new ArrayIterator($this->data);
- }
-
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testSetLimit()
- {
- $this->traitQuery->setLimit(1);
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $res = iterator_to_array($iterator);
- $this->assertCount(1, $res);
-
- $this->traitQuery->setLimit(-4);
- }
-
- /**
- * @expectedException \InvalidArgumentException
- */
- public function testSetOffset()
- {
- $this->traitQuery->setOffset(1);
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $res = iterator_to_array($iterator);
- $this->assertCount(3, $res);
-
- $this->traitQuery->setOffset('toto');
- }
-
- public function testIntervalLimitTooLong()
- {
- $this->traitQuery->setOffset(3);
- $this->traitQuery->setLimit(10);
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $res = iterator_to_array($iterator);
- $this->assertSame([3 => 'bar'], $res);
- $this->assertCount(1, $res);
- }
-
- public function testInterval()
- {
- $this->traitQuery->setOffset(1);
- $this->traitQuery->setLimit(1);
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $res = iterator_to_array($iterator);
- $this->assertCount(1, $res);
- }
-
- public function testFilter()
- {
- $func = function ($row) {
- return false !== strpos($row, 'o');
- };
- $this->traitQuery->setFilter($func);
-
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $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()
- {
- $this->traitQuery->setSortBy('strcmp');
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $res = iterator_to_array($iterator, false);
- $this->assertSame(['bar', 'foo', 'jane', 'john'], $res);
-
- $this->traitQuery->addSortBy('strcmp');
- $this->traitQuery->addSortBy('strcmp');
- $this->traitQuery->removeSortBy('strcmp');
- $this->assertTrue($this->traitQuery->hasSortBy('strcmp'));
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator]);
- $res = iterator_to_array($iterator, false);
- $this->assertSame(['bar', 'foo', 'jane', 'john'], $res);
- }
-
- public function testExecuteWithCallback()
- {
- $iterator = $this->invokeMethod($this->traitQuery, 'execute', [$this->iterator, function ($value) {
- return strtoupper($value);
- }]);
- $this->assertSame(array_map('strtoupper', $this->data), iterator_to_array($iterator));
- }
-}
diff --git a/test/ReaderTest.php b/test/ReaderTest.php
index 36f8f0b..f9f0d48 100644
--- a/test/ReaderTest.php
+++ b/test/ReaderTest.php
@@ -15,7 +15,7 @@ class ReaderTest extends PHPUnit_Framework_TestCase
private $expected = [
['john', 'doe', 'john.doe@example.com'],
- ['jane','doe','jane.doe@example.com'],
+ ['jane', 'doe', 'jane.doe@example.com'],
];
public function setUp()
@@ -28,6 +28,82 @@ class ReaderTest extends PHPUnit_Framework_TestCase
$this->csv = new Reader($csv);
}
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testSetLimit()
+ {
+ $this->csv->setLimit(1);
+ $this->assertCount(1, $this->csv->fetchAll());
+ $this->csv->setLimit(-4);
+ }
+
+ /**
+ * @expectedException \InvalidArgumentException
+ */
+ public function testSetOffset()
+ {
+ $this->csv->setOffset(1);
+ $this->assertCount(1, $this->csv->fetchAll());
+
+ $this->csv->setOffset('toto');
+ }
+
+ public function testIntervalLimitTooLong()
+ {
+ $this->csv->setOffset(1);
+ $this->csv->setLimit(10);
+ $this->assertSame([['jane', 'doe', 'jane.doe@example.com']], $this->csv->fetchAll());
+ }
+
+ public function testInterval()
+ {
+ $this->csv->setOffset(1);
+ $this->csv->setLimit(1);
+ $this->assertCount(1, $this->csv->fetchAll());
+ }
+
+ public function testFilter()
+ {
+ $func = function ($row) {
+ return ! in_array('jane', $row);
+ };
+ $this->csv->setFilter($func);
+
+ $this->assertCount(1, $this->csv->fetchAll());
+
+ $func2 = function ($row) {
+ return ! in_array('john', $row);
+ };
+ $this->csv->addFilter($func2);
+ $this->csv->addFilter($func);
+
+ $this->assertCount(0, $this->csv->fetchAll());
+
+ $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->assertCount(1, $this->csv->fetchAll());
+ }
+
+ public function testSortBy()
+ {
+ $func = function ($rowA, $rowB) {
+ return strcmp($rowA[0], $rowB[0]);
+ };
+ $this->csv->setSortBy($func);
+ $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->assertSame(array_reverse($this->expected), $this->csv->fetchAll());
+ }
+
public function testFetchAll()
{
$func = function ($value) {