diff --git a/src/Result/BufferedResultAdapter.php b/src/Result/BufferedResultAdapter.php index a82aa53f..34ca5099 100644 --- a/src/Result/BufferedResultAdapter.php +++ b/src/Result/BufferedResultAdapter.php @@ -6,16 +6,18 @@ use ArrayIterator; use Nextras\Dbal\Exception\InvalidArgumentException; use OutOfBoundsException; -use function assert; +/** + * @internal + */ class BufferedResultAdapter implements IResultAdapter { /** @var IResultAdapter */ - private $adapter; + protected $adapter; /** @var ArrayIterator|null */ - private $data; + protected $data; public function __construct(IResultAdapter $adapter) @@ -42,18 +44,15 @@ public function toUnbuffered(): IResultAdapter public function seek(int $index): void { - if ($this->data === null) { - $this->init(); - } - assert($this->data !== null); + $data = $this->getData(); if ($index === 0) { - $this->data->rewind(); + $data->rewind(); return; } try { - $this->data->seek($index); + $data->seek($index); } catch (OutOfBoundsException $e) { throw new InvalidArgumentException("Unable to seek in row set to {$index} index.", 0, $e); } @@ -62,13 +61,9 @@ public function seek(int $index): void public function fetch(): ?array { - if ($this->data === null) { - $this->init(); - } - assert($this->data !== null); - - $fetched = $this->data->valid() ? $this->data->current() : null; - $this->data->next(); + $data = $this->getData(); + $fetched = $data->valid() ? $data->current() : null; + $data->next(); return $fetched; } @@ -80,22 +75,32 @@ public function getTypes(): array public function getRowsCount(): int + { + return $this->getData()->count(); + } + + + /** + * @return ArrayIterator + */ + protected function getData(): ArrayIterator { if ($this->data === null) { - $this->init(); + $this->data = $this->fetchData(); } - assert($this->data !== null); - - return $this->data->count(); + return $this->data; } - private function init(): void + /** + * @return ArrayIterator + */ + protected function fetchData(): ArrayIterator { $rows = []; while (($row = $this->adapter->fetch()) !== null) { $rows[] = $row; } - $this->data = new ArrayIterator($rows); + return new ArrayIterator($rows); } }