summaryrefslogtreecommitdiffstats
path: root/src
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 /src
parent5bc6dec95b61123dda7ffae9758325a55ec6988d (diff)
downloadcsv-8096c4df88a4ad3a652dbed1ce8ca70bc6bea9f5.zip
csv-8096c4df88a4ad3a652dbed1ce8ca70bc6bea9f5.tar.gz
csv-8096c4df88a4ad3a652dbed1ce8ca70bc6bea9f5.tar.bz2
adding fetchPairsWithoutDuplicates method
Diffstat (limited to 'src')
-rw-r--r--src/AbstractCsv.php11
-rw-r--r--src/Modifier/QueryFilter.php49
-rw-r--r--src/Reader.php36
3 files changed, 27 insertions, 69 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;
}
/**