From f17b7214baeb2475ad7860bec03ffa766690c97e Mon Sep 17 00:00:00 2001 From: Ignace Nyamagana Butera Date: Thu, 23 Apr 2015 15:50:36 +0200 Subject: [PATCH] adding Reader::execute for future use --- src/Reader.php | 79 +++++++++++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/src/Reader.php b/src/Reader.php index fd2bc8ff..1f2d65eb 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -14,6 +14,7 @@ use CallbackFilterIterator; use InvalidArgumentException; +use Iterator; use League\Csv\Modifier; use LimitIterator; @@ -79,37 +80,6 @@ public function each(callable $callable) return $index; } - /** - * Returns a single column from the CSV data - * - * The callable function will be applied to each value to be return - * - * @param int $column_index field Index - * @param callable $callable a callable function - * - * @throws \InvalidArgumentException If the column index is not a positive integer or 0 - * - * @return array - */ - public function fetchColumn($column_index = 0, callable $callable = null) - { - if (false === filter_var($column_index, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { - throw new InvalidArgumentException( - 'the column index must be a positive integer or 0' - ); - } - - $iterator = $this->query($callable); - $iterator = new CallbackFilterIterator($iterator, function ($row) use ($column_index) { - return array_key_exists($column_index, $row); - }); - $iterator = new Modifier\MapIterator($iterator, function ($row) use ($column_index) { - return $row[$column_index]; - }); - - return iterator_to_array($iterator, false); - } - /** * Returns a single row from the CSV * @@ -140,7 +110,50 @@ public function fetchOne($offset = 0) */ public function fetchAll(callable $callable = null) { - return iterator_to_array($this->query($callable), false); + return $this->execute($this->query($callable)); + } + + /** + * Transform the Iterator into an array + * + * @param Iterator $iterator + * + * @return array + */ + protected function execute(Iterator $iterator) + { + return iterator_to_array($iterator, false); + } + + /** + * Returns a single column from the CSV data + * + * The callable function will be applied to each value to be return + * + * @param int $column_index field Index + * @param callable $callable a callable function + * + * @throws \InvalidArgumentException If the column index is not a positive integer or 0 + * + * @return array + */ + public function fetchColumn($column_index = 0, callable $callable = null) + { + if (false === filter_var($column_index, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { + throw new InvalidArgumentException( + 'the column index must be a positive integer or 0' + ); + } + + $iterator = $this->query($callable); + $iterator = new CallbackFilterIterator($iterator, function ($row) use ($column_index) { + return array_key_exists($column_index, $row); + }); + $iterator = new Modifier\MapIterator($iterator, function ($row) use ($column_index) { + return $row[$column_index]; + }); + + return $this->execute($iterator); } /** @@ -176,7 +189,7 @@ public function fetchAssoc($offset_or_keys = 0, callable $callable = null) return array_combine($keys, $row); }); - return iterator_to_array($iterator, false); + return $this->execute($iterator); } /**