Skip to content

Commit

Permalink
Improve internal code
Browse files Browse the repository at this point in the history
- Improve ResultSet::getColumnIndex
- Improve StreamIterator::__construct
  • Loading branch information
nyamsprod committed May 23, 2017
1 parent 8913e71 commit c6a91fc
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/Reader.php
Expand Up @@ -133,7 +133,7 @@ protected function setHeader(int $offset): array
}

if (0 === $offset) {
$header = $this->removeBOM($header, mb_strlen($this->getInputBOM()), $this->enclosure);
return $this->removeBOM($header, mb_strlen($this->getInputBOM()), $this->enclosure);
}

return $header;
Expand Down
56 changes: 42 additions & 14 deletions src/ResultSet.php
Expand Up @@ -171,7 +171,7 @@ public function fetchOne(int $offset = 0): array
*/
public function fetchColumn($index = 0): Generator
{
$offset = $this->getColumnIndex($index, 'The column index `%s` value is invalid');
$offset = $this->getColumnIndex($index, 'The column index `%s` is invalid or does not exist');
$filter = function (array $record) use ($offset): bool {
return isset($record[$offset]);
};
Expand All @@ -191,32 +191,60 @@ public function fetchColumn($index = 0): Generator
* @param string|int $field the field name or the field index
* @param string $error_message the associated error message
*
* @throws RuntimeException if the field is invalid
* @throws RuntimeException if the column is not found
*
* @return string|int
*/
protected function getColumnIndex($field, string $error_message)
{
if (false !== array_search($field, $this->column_names, true)) {
return $field;
$method = 'getColumnIndexByKey';
if (is_string($field)) {
$method = 'getColumnIndexByValue';
}

if (is_string($field)) {
throw new RuntimeException(sprintf($error_message, $field));
return $this->$method($field, $error_message);
}

/**
* Returns the selected column name
*
* @param string $value
* @param string $error_message
*
* @throws RuntimeException if the column is not found
*
* @return string
*/
protected function getColumnIndexByValue(string $value, string $error_message): string
{
if (false !== array_search($value, $this->column_names, true)) {
return $value;
}

$index = $this->filterMinRange($field, 0, $error_message);
throw new RuntimeException(sprintf($error_message, $value));
}

/**
* Returns the selected column name according to its offset
*
* @param int $offset
* @param string $error_message
*
* @throws RuntimeException if the field is invalid or not found
*
* @return int|string
*/
protected function getColumnIndexByKey(int $offset, string $error_message)
{
$offset = $this->filterMinRange($offset, 0, $error_message);
if (empty($this->column_names)) {
return $index;
return $offset;
}

$index = array_search($index, array_flip($this->column_names), true);
if (false !== $index) {
return $index;
$value = array_search($offset, array_flip($this->column_names), true);
if (false !== $value) {
return $value;
}

throw new RuntimeException(sprintf($error_message, $field));
throw new RuntimeException(sprintf($error_message, $offset));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/StreamIterator.php
Expand Up @@ -106,7 +106,7 @@ class StreamIterator implements SeekableIterator
public function __construct($stream)
{
if (!is_resource($stream)) {
throw new InvalidArgumentException(sprintf('Argument passed must be a seekable stream resource, %s given', is_object($stream) ? get_class($stream) : gettype($stream)));
throw new InvalidArgumentException(sprintf('Argument passed must be a seekable stream resource, %s given', gettype($stream)));
}

if ('stream' !== ($type = get_resource_type($stream))) {
Expand Down
8 changes: 8 additions & 0 deletions tests/ResultSetTest.php
Expand Up @@ -200,6 +200,8 @@ public function testOrderByWithEquity()
/**
* @covers ::fetchColumn
* @covers ::getColumnIndex
* @covers ::getColumnIndexByValue
* @covers ::getColumnIndexByKey
* @covers ::iteratorToGenerator
* @covers ::__destruct
* @covers League\Csv\Exception\RuntimeException
Expand Down Expand Up @@ -297,6 +299,8 @@ public function testPreserveOffset()
/**
* @covers ::fetchColumn
* @covers ::getColumnIndex
* @covers ::getColumnIndexByValue
* @covers ::getColumnIndexByKey
* @covers League\Csv\MapIterator
*/
public function testFetchColumnWithColumnname()
Expand All @@ -312,6 +316,8 @@ public function testFetchColumnWithColumnname()
/**
* @covers ::fetchColumn
* @covers ::getColumnIndex
* @covers ::getColumnIndexByValue
* @covers ::getColumnIndexByKey
* @covers League\Csv\MapIterator
*/
public function testFetchColumn()
Expand All @@ -324,6 +330,7 @@ public function testFetchColumn()
* @covers ::fetchColumn
* @covers ::iteratorToGenerator
* @covers ::getColumnIndex
* @covers ::getColumnIndexByKey
* @covers League\Csv\MapIterator
*/
public function testFetchColumnInconsistentColumnCSV()
Expand All @@ -345,6 +352,7 @@ public function testFetchColumnInconsistentColumnCSV()
/**
* @covers ::fetchColumn
* @covers ::getColumnIndex
* @covers ::getColumnIndexByKey
* @covers League\Csv\MapIterator
*/
public function testFetchColumnEmptyCol()
Expand Down

0 comments on commit c6a91fc

Please sign in to comment.