summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorignace nyamagana butera <nyamsprod@gmail.com>2015-12-09 17:43:14 +0100
committerignace nyamagana butera <nyamsprod@gmail.com>2015-12-09 17:43:14 +0100
commit8096c4df88a4ad3a652dbed1ce8ca70bc6bea9f5 (patch)
treebda70d22b88c45cd8fc2d4b6afb9da8f86eef01a
parent5bc6dec95b61123dda7ffae9758325a55ec6988d (diff)
downloadcsv-8096c4df88a4ad3a652dbed1ce8ca70bc6bea9f5.zip
csv-8096c4df88a4ad3a652dbed1ce8ca70bc6bea9f5.tar.gz
csv-8096c4df88a4ad3a652dbed1ce8ca70bc6bea9f5.tar.bz2
adding fetchPairsWithoutDuplicates method
-rw-r--r--src/AbstractCsv.php11
-rw-r--r--src/Modifier/QueryFilter.php49
-rw-r--r--src/Reader.php36
-rw-r--r--test/ReaderTest.php72
4 files changed, 38 insertions, 130 deletions
diff --git a/src/AbstractCsv.php b/src/AbstractCsv.php
index 799666b..51b0df1 100644
--- a/src/AbstractCsv.php
+++ b/src/AbstractCsv.php
@@ -13,7 +13,6 @@
namespace League\Csv;
use InvalidArgumentException;
-use Iterator;
use IteratorAggregate;
use JsonSerializable;
use League\Csv\Config\Controls;
@@ -67,16 +66,6 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
const BOM_UTF32_LE = "\x00\x00\xFF\xFE";
/**
- * return an array
- */
- const TYPE_ARRAY = 1;
-
- /**
- * return an iterator
- */
- const TYPE_ITERATOR = 2;
-
- /**
* The constructor path
*
* can be a SplFileInfo object or the string path to a file
diff --git a/src/Modifier/QueryFilter.php b/src/Modifier/QueryFilter.php
index aaa1bfc..daaaa89 100644
--- a/src/Modifier/QueryFilter.php
+++ b/src/Modifier/QueryFilter.php
@@ -15,9 +15,7 @@ namespace League\Csv\Modifier;
use ArrayObject;
use CallbackFilterIterator;
use Iterator;
-use League\Csv\AbstractCsv;
use LimitIterator;
-use UnexpectedValueException;
/**
* A Trait to Query rows against a SplFileObject
@@ -64,33 +62,6 @@ trait QueryFilter
protected $strip_bom = false;
/**
- * Reader return type
- *
- * @var int
- */
- protected $returnType = AbstractCsv::TYPE_ARRAY;
-
- /**
- * Set the return type for the next fetch call
- *
- * @param int $type
- *
- * @throws UnexpectedValueException If the value is not one of the defined constant
- *
- * @return static
- */
- public function setReturnType($type)
- {
- $returnTypeList = [AbstractCsv::TYPE_ARRAY => 1, AbstractCsv::TYPE_ITERATOR => 1];
- if (!isset($returnTypeList[$type])) {
- throw new UnexpectedValueException('Unknown return type');
- }
- $this->returnType = $type;
-
- return $this;
- }
-
- /**
* Stripping BOM setter
*
* @param bool $status
@@ -192,8 +163,6 @@ trait QueryFilter
$iterator = $this->applyIteratorSortBy($iterator);
$iterator = $this->applyIteratorInterval($iterator);
- $this->returnType = AbstractCsv::TYPE_ARRAY;
-
return $iterator;
}
@@ -326,22 +295,4 @@ trait QueryFilter
return new LimitIterator($iterator, $offset, $limit);
}
-
- /**
- * Convert the Iterator into an array depending on the selected return type
- *
- * @param int $type
- * @param Iterator $iterator
- * @param bool $use_keys Whether to use the iterator element keys as index
- *
- * @return Iterator|array
- */
- protected function applyReturnType($type, Iterator $iterator, $use_keys = true)
- {
- if (AbstractCsv::TYPE_ARRAY == $type) {
- return iterator_to_array($iterator, $use_keys);
- }
-
- return $iterator;
- }
}
diff --git a/src/Reader.php b/src/Reader.php
index 3f6d84d..a77e3ae 100644
--- a/src/Reader.php
+++ b/src/Reader.php
@@ -73,7 +73,7 @@ class Reader extends AbstractCsv
*/
public function fetchAll(callable $callable = null)
{
- return $this->applyReturnType(AbstractCsv::TYPE_ARRAY, $this->fetch($callable), false);
+ return iterator_to_array($this->fetch($callable), false);
}
/**
@@ -133,7 +133,7 @@ class Reader extends AbstractCsv
* @param int $columnIndex CSV column index
* @param callable|null $callable A callable to be applied to each of the value to be returned.
*
- * @return Iterator|array
+ * @return Iterator
*/
public function fetchColumn($columnIndex = 0, callable $callable = null)
{
@@ -148,11 +148,10 @@ class Reader extends AbstractCsv
};
$this->addFilter($filterColumn);
- $returnType = $this->returnType;
$iterator = $this->fetch($selectColumn);
$iterator = $this->applyCallable($iterator, $callable);
- return $this->applyReturnType($returnType, $iterator, false);
+ return $iterator;
}
/**
@@ -169,7 +168,7 @@ class Reader extends AbstractCsv
* @param int $valueIndex The column index to serve as value
* @param callable|null $callable A callable to be applied to each of the rows to be returned.
*
- * @return Generator|array
+ * @return Generator
*/
public function fetchPairs($offsetIndex = 0, $valueIndex = 1, callable $callable = null)
{
@@ -186,11 +185,10 @@ class Reader extends AbstractCsv
};
$this->addFilter($filterPairs);
- $returnType = $this->returnType;
$iterator = $this->fetch($selectPairs);
$iterator = $this->applyCallable($iterator, $callable);
- return $this->applyReturnType($returnType, $this->generatePairs($iterator));
+ return $this->generatePairs($iterator);
}
/**
@@ -208,6 +206,27 @@ class Reader extends AbstractCsv
}
/**
+ * Retrive CSV data as pairs
+ *
+ * Fetches an associative array of all rows as key-value pairs (first
+ * column is the key, second column is the value).
+ *
+ * By default if no column index is provided:
+ * - the first CSV column is used to provide the keys
+ * - the second CSV column is used to provide the value
+ *
+ * @param int $offsetIndex The column index to serve as offset
+ * @param int $valueIndex The column index to serve as value
+ * @param callable|null $callable A callable to be applied to each of the rows to be returned.
+ *
+ * @return array
+ */
+ public function fetchPairsWithoutDuplicates($offsetIndex = 0, $valueIndex = 1, callable $callable = null)
+ {
+ return iterator_to_array($this->fetchPairs($offsetIndex, $valueIndex, $callable), true);
+ }
+
+ /**
* Returns a sequential array of all CSV lines;
*
* The rows are presented as associated arrays
@@ -234,11 +253,10 @@ class Reader extends AbstractCsv
return array_combine($keys, $row);
};
- $returnType = $this->returnType;
$iterator = $this->fetch($combineArray);
$iterator = $this->applyCallable($iterator, $callable);
- return $this->applyReturnType($returnType, $iterator, false);
+ return $iterator;
}
/**
diff --git a/test/ReaderTest.php b/test/ReaderTest.php
index 1b4ebf9..9e606e0 100644
--- a/test/ReaderTest.php
+++ b/test/ReaderTest.php
@@ -128,7 +128,6 @@ class ReaderTest extends AbstractTestCase
{
$keys = ['firstname', 'lastname', 'email'];
$res = $this->csv->fetchAssoc($keys);
- $this->assertInternalType('array', $res);
foreach ($res as $offset => $row) {
$this->assertSame($keys, array_keys($row));
}
@@ -137,8 +136,7 @@ class ReaderTest extends AbstractTestCase
public function testFetchAssocReturnsIterator()
{
$keys = ['firstname', 'lastname', 'email'];
- $res = $this->csv->setReturnType(Reader::TYPE_ITERATOR)->fetchAssoc($keys);
- $this->assertInstanceof('\Iterator', $res);
+ $res = $this->csv->fetchAssoc($keys);
foreach ($res as $offset => $row) {
$this->assertSame($keys, array_keys($row));
}
@@ -246,7 +244,7 @@ class ReaderTest extends AbstractTestCase
}
$csv = Reader::createFromFileObject($tmp);
$csv->stripBom(true);
- $res = array_keys($csv->fetchAssoc()[0]);
+ $res = array_keys(iterator_to_array($csv->fetchAssoc(), false)[0]);
$this->assertSame('john', $res[0]);
}
@@ -261,7 +259,7 @@ class ReaderTest extends AbstractTestCase
$expected = [
['parent name' => 'parentA', 'child name' => 'childA', 'title' => 'titleA'],
];
- $this->assertSame($expected, $csv->fetchAssoc());
+ $this->assertSame($expected, iterator_to_array($csv->fetchAssoc(), false));
}
public function testStripBOMWithEnclosureFetchColumn()
@@ -345,12 +343,6 @@ class ReaderTest extends AbstractTestCase
$this->assertContains('jane', $this->csv->fetchColumn());
}
- public function testFetchColumnReturnsIterator()
- {
- $this->assertContains('john', $this->csv->setReturnType(Reader::TYPE_ITERATOR)->fetchColumn(0));
- $this->assertContains('jane', $this->csv->setReturnType(Reader::TYPE_ITERATOR)->fetchColumn());
- }
-
public function testFetchColumnInconsistentColumnCSV()
{
$raw = [
@@ -390,7 +382,7 @@ class ReaderTest extends AbstractTestCase
return strtoupper($value);
};
$iterator = $this->csv->fetchColumn(0, $func);
- $this->assertSame(['JOHN', 'JANE'], $iterator);
+ $this->assertSame(['JOHN', 'JANE'], iterator_to_array($iterator, false));
}
/**
@@ -450,7 +442,7 @@ class ReaderTest extends AbstractTestCase
*/
public function testFetchPairsIteratorMode($key, $value, $callable, $expected)
{
- $iterator = $this->csv->setReturnType(Reader::TYPE_ITERATOR)->fetchPairs($key, $value, $callable);
+ $iterator = $this->csv->fetchPairs($key, $value, $callable);
foreach ($iterator as $key => $value) {
$res = current($expected);
$this->assertSame($value, $res[$key]);
@@ -499,9 +491,9 @@ class ReaderTest extends AbstractTestCase
/**
* @dataProvider fetchPairsArrayDataProvider
*/
- public function testFetchPairsArrayMode($key, $value, $callable, $expected)
+ public function testFetchPairsAsArray($key, $value, $callable, $expected)
{
- $array = $this->csv->fetchPairs($key, $value, $callable);
+ $array = $this->csv->fetchPairsWithoutDuplicates($key, $value, $callable);
$this->assertSame($expected, $array);
}
@@ -536,45 +528,25 @@ class ReaderTest extends AbstractTestCase
public function testFetchPairsWithInvalidOffset()
{
- $this->assertCount(0, $this->csv->fetchPairs(10, 1));
+ $this->assertCount(0, iterator_to_array($this->csv->fetchPairs(10, 1), true));
}
public function testFetchPairsWithInvalidValue()
{
$res = $this->csv->fetchPairs(0, 15);
- $this->assertCount(2, $res);
foreach ($res as $value) {
$this->assertNull($value);
}
}
- /**
- * @expectedException \UnexpectedValueException
- */
- public function testReturnTypeThrowsException()
- {
- $this->csv->setReturnType('toto');
- }
-
public function testReturnTypeResetBetweenCallToArrayWithFetch()
{
- $this->csv->setReturnType(Reader::TYPE_ITERATOR);
- $this->assertInstanceof('\Iterator', $this->csv->fetch());
$this->assertInstanceof('\Iterator', $this->csv->fetch());
- }
-
- public function testReturnTypeResetBetweenCallToArrayWithFetchAll()
- {
- $this->csv->setReturnType(Reader::TYPE_ITERATOR);
- $this->assertInternalType('array', $this->csv->fetchAll());
$this->assertInternalType('array', $this->csv->fetchAll());
- }
-
- public function testReturnTypeResetBetweenCallToArrayWithFetchOne()
- {
- $this->csv->setReturnType(Reader::TYPE_ITERATOR);
- $this->assertInternalType('array', $this->csv->fetchOne());
$this->assertInternalType('array', $this->csv->fetchOne());
+ $this->assertInstanceof('\Iterator', $this->csv->fetchAssoc());
+ $this->assertInstanceof('\Iterator', $this->csv->fetchPairs());
+ $this->assertInternalType('array', $this->csv->fetchPairsWithoutDuplicates());
}
public function testReturnTypeResetBetweenCallToArrayWithEach()
@@ -582,28 +554,6 @@ class ReaderTest extends AbstractTestCase
$func = function (array $row) {
return true;
};
- $this->csv->setReturnType(Reader::TYPE_ITERATOR);
- $this->assertInternalType('int', $this->csv->each($func));
$this->assertInternalType('int', $this->csv->each($func));
}
-
- public function testReturnTypeResetBetweenCallToArrayWithFetchAssoc()
- {
- $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->fetchPairs());
- $this->assertInternalType('array', $this->csv->fetchPairs());
- $this->csv->setReturnType(Reader::TYPE_ITERATOR);
- $this->csv->toXML();
- $this->assertInternalType('array', $this->csv->fetchPairs());
- }
}