buffer === -2) { throw new RuntimeException('Buffering must be enabled before iteration is started'); } elseif ($this->buffer === null) { $this->buffer = []; if ($this->dataSource instanceof ResultInterface) { $this->dataSource->rewind(); } } return $this; } /** * Countable: return count of rows */ #[Override] #[ReturnTypeWillChange] public function count(): ?int { if ($this->count !== null) { return $this->count; } if ($this->dataSource instanceof Countable) { $this->count = count($this->dataSource); } return $this->count; } /** * Iterator: get current item */ #[Override] public function current(): array|object|null { if (-1 === $this->buffer) { // datasource was an array when the resultset was initialized return $this->dataSource->current(); } if ($this->buffer === null) { $this->buffer = -2; // implicitly disable buffering from here on } elseif (is_array($this->buffer) && isset($this->buffer[$this->position])) { return $this->buffer[$this->position]; } $data = $this->dataSource->current(); if (is_array($this->buffer)) { $this->buffer[$this->position] = $data; } return is_array($data) ? $data : null; } /** * Get the data source used to create the result set */ public function getDataSource(): ResultInterface|IteratorAggregate|Iterator|null { return $this->dataSource; } /** * Retrieve count of fields in individual rows of the result set */ #[Override] public function getFieldCount(): int { if (null !== $this->fieldCount) { return $this->fieldCount; } $dataSource = $this->getDataSource(); if (null === $dataSource) { return 0; } $dataSource->rewind(); if (! $dataSource->valid()) { $this->fieldCount = 0; return 0; } $row = $dataSource->current(); if ($row instanceof Countable) { $this->fieldCount = $row->count(); return $this->fieldCount; } $row = (array) $row; $this->fieldCount = count($row); return $this->fieldCount; } /** * Set the data source for the result set * * @throws InvalidArgumentException|Exception */ #[Override] public function initialize(iterable $dataSource): ResultSetInterface { // reset buffering if (is_array($this->buffer)) { $this->buffer = []; } if ($dataSource instanceof ResultInterface) { $this->fieldCount = $dataSource->getFieldCount(); $this->dataSource = $dataSource; if ($dataSource->isBuffered()) { $this->buffer = -1; } if (is_array($this->buffer)) { $this->dataSource->rewind(); } return $this; } if (is_array($dataSource)) { // its safe to get numbers from an array $first = current($dataSource); reset($dataSource); $this->fieldCount = $first === false ? 0 : count($first); $this->dataSource = new ArrayIterator($dataSource); $this->buffer = -1; // array's are a natural buffer } elseif ($dataSource instanceof IteratorAggregate) { /** @phpstan-ignore assign.propertyType */ $this->dataSource = $dataSource->getIterator(); } else { /** @phpstan-ignore assign.propertyType */ $this->dataSource = $dataSource; } return $this; } public function isBuffered(): bool { return $this->buffer === -1 || is_array($this->buffer); } /** * Iterator: retrieve current key */ #[Override] public function key(): int { return $this->position; } /** * Iterator: move pointer to next item */ #[Override] public function next(): void { if ($this->buffer === null) { $this->buffer = -2; // implicitly disable buffering from here on } if (! is_array($this->buffer) || $this->position === $this->dataSource->key()) { $this->dataSource->next(); } $this->position++; } /** * Iterator: rewind */ #[Override] public function rewind(): void { if (! is_array($this->buffer)) { $this->dataSource->rewind(); } $this->position = 0; } /** * Iterator: is pointer valid? */ #[Override] public function valid(): bool { if (is_array($this->buffer) && isset($this->buffer[$this->position])) { return true; } return $this->dataSource->valid(); } }