Skip to content

Commit

Permalink
adding Reader::execute for future use
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Apr 23, 2015
1 parent bbeecf3 commit f17b721
Showing 1 changed file with 46 additions and 33 deletions.
79 changes: 46 additions & 33 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use CallbackFilterIterator;
use InvalidArgumentException;
use Iterator;
use League\Csv\Modifier;
use LimitIterator;

Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit f17b721

Please sign in to comment.