Skip to content

Commit

Permalink
bug fix PHP7.2 bug see #279
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Feb 6, 2018
1 parent bd614fb commit d99c1c4
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,7 @@ protected function isValidKey($value)
*/
protected function getRow($offset)
{
$fileObj = $this->getIterator();
$iterator = new LimitIterator($fileObj, $offset, 1);
$iterator->rewind();
$row = $iterator->current();
$row = $this->seekRow($offset);
if (empty($row)) {
throw new InvalidArgumentException('the specified row does not exist or is empty');
}
Expand All @@ -336,4 +333,29 @@ protected function getRow($offset)

return $row;
}

/**
* Returns the row at a given offset
*
* @param int $offset
*
* @return mixed
*/
protected function seekRow($offset)
{
$stream = $this->getIterator();
//Workaround for SplFileObject::seek bug in PHP7.2+ see https://bugs.php.net/bug.php?id=75917
if (PHP_VERSION_ID > 70200 && !$stream instanceof StreamIterator) {
while ($offset !== $stream->key() && $stream->valid()) {
$stream->next();
}

return $stream->current();
}

$iterator = new LimitIterator($stream, $offset, 1);
$iterator->rewind();

return $iterator->current();
}
}

0 comments on commit d99c1c4

Please sign in to comment.