From d99c1c499e301dce0acffa1990154fbdf009545a Mon Sep 17 00:00:00 2001 From: Ignace Nyamagana Butera Date: Tue, 6 Feb 2018 09:13:57 +0100 Subject: [PATCH] bug fix PHP7.2 bug see #279 --- src/Reader.php | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) 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(); + } }