diff options
author | ignace nyamagana butera <nyamsprod@gmail.com> | 2015-11-27 09:33:46 +0100 |
---|---|---|
committer | ignace nyamagana butera <nyamsprod@gmail.com> | 2015-11-27 10:26:09 +0100 |
commit | a94b152004b070cccf3a6cb669f247a6bd764bd6 (patch) | |
tree | 6388fbb85454fb1542666c94a5c45fcf37a03204 | |
parent | 0274c0fd79e0ba4ed98de3b0c4fc383aae014e63 (diff) | |
download | csv-a94b152004b070cccf3a6cb669f247a6bd764bd6.zip csv-a94b152004b070cccf3a6cb669f247a6bd764bd6.tar.gz csv-a94b152004b070cccf3a6cb669f247a6bd764bd6.tar.bz2 |
Improve returnType reset
-rw-r--r-- | src/AbstractCsv.php | 1 | ||||
-rw-r--r-- | src/Reader.php | 7 | ||||
-rw-r--r-- | test/ReaderTest.php | 10 |
3 files changed, 12 insertions, 6 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php index c13f52b..1e93c31 100644 --- a/src/AbstractCsv.php +++ b/src/AbstractCsv.php @@ -133,6 +133,7 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate */ public function getIterator() { + $this->returnType = self::TYPE_ARRAY; $iterator = $this->path; if (!$iterator instanceof SplFileObject) { $iterator = new SplFileObject($this->getStreamFilterPath(), $this->open_mode); diff --git a/src/Reader.php b/src/Reader.php index 140d959..f45b1bf 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -50,7 +50,6 @@ class Reader extends AbstractCsv $iterator = $this->applyIteratorFilter($iterator); $iterator = $this->applyIteratorSortBy($iterator); $iterator = $this->applyIteratorInterval($iterator); - $this->returnType = self::TYPE_ARRAY; return $this->applyCallable($iterator, $callable); } @@ -147,6 +146,7 @@ class Reader extends AbstractCsv */ public function fetchColumn($columnIndex = 0, callable $callable = null) { + $type = $this->returnType; $columnIndex = $this->filterInteger($columnIndex, 0, 'the column index must be a positive integer or 0'); $filterColumn = function ($row) use ($columnIndex) { @@ -158,7 +158,6 @@ class Reader extends AbstractCsv }; $this->addFilter($filterColumn); - $type = $this->returnType; $iterator = $this->fetch($selectColumn); $iterator = $this->applyCallable($iterator, $callable); @@ -183,6 +182,7 @@ class Reader extends AbstractCsv */ public function fetchPairs($offsetColumnIndex = 0, $valueColumnIndex = 1, callable $callable = null) { + $type = $this->returnType; $offsetColumnIndex = $this->filterInteger($offsetColumnIndex, 0, 'the offset column index must be a positive integer or 0'); $valueColumnIndex = $this->filterInteger($valueColumnIndex, 0, 'the value column index must be a positive integer or 0'); $filterPairs = function ($row) use ($offsetColumnIndex, $valueColumnIndex) { @@ -192,7 +192,6 @@ class Reader extends AbstractCsv return [$row[$offsetColumnIndex], $row[$valueColumnIndex]]; }; $this->addFilter($filterPairs); - $type = $this->returnType; $iterator = $this->fetch($selectPairs); $iterator = $this->applyCallable($iterator, $callable); @@ -230,6 +229,7 @@ class Reader extends AbstractCsv */ public function fetchAssoc($offset_or_keys = 0, callable $callable = null) { + $type = $this->returnType; $keys = $this->getAssocKeys($offset_or_keys); $keys_count = count($keys); $combineArray = function (array $row) use ($keys, $keys_count) { @@ -239,7 +239,6 @@ class Reader extends AbstractCsv return array_combine($keys, $row); }; - $type = $this->returnType; $iterator = $this->fetch($combineArray); $iterator = $this->applyCallable($iterator, $callable); diff --git a/test/ReaderTest.php b/test/ReaderTest.php index b2ecb6a..893df93 100644 --- a/test/ReaderTest.php +++ b/test/ReaderTest.php @@ -606,12 +606,18 @@ class ReaderTest extends AbstractTestCase $this->csv->setReturnType(Reader::TYPE_ITERATOR); $this->assertInstanceof('\Iterator', $this->csv->fetchAssoc()); $this->assertInternalType('array', $this->csv->fetchAssoc()); + $this->csv->setReturnType(Reader::TYPE_ITERATOR); + $this->csv->toXML(); + $this->assertInternalType('array', $this->csv->fetchAssoc()); } public function testReturnTypeResetBetweenCallToArrayWithFetchPairs() { $this->csv->setReturnType(Reader::TYPE_ITERATOR); - $this->assertInstanceof('\Iterator', $this->csv->fetchAssoc()); - $this->assertInternalType('array', $this->csv->fetchAssoc()); + $this->assertInstanceof('\Iterator', $this->csv->fetchPairs()); + $this->assertInternalType('array', $this->csv->fetchPairs()); + $this->csv->setReturnType(Reader::TYPE_ITERATOR); + $this->csv->toXML(); + $this->assertInternalType('array', $this->csv->fetchPairs()); } } |