diff --git a/src/Reader.php b/src/Reader.php index ad51c6b7..6f85f8cc 100644 --- a/src/Reader.php +++ b/src/Reader.php @@ -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'); } @@ -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(); + } }