From 9989ba1fa70b2ff77b451c11957722c459d5b689 Mon Sep 17 00:00:00 2001 From: Aaron van Geffen Date: Tue, 13 May 2025 22:51:12 +0200 Subject: [PATCH] CachedPDOIterator: introduce rewindable PDOStatement iterator --- models/CachedPDOIterator.php | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 models/CachedPDOIterator.php diff --git a/models/CachedPDOIterator.php b/models/CachedPDOIterator.php new file mode 100644 index 0000000..99297f8 --- /dev/null +++ b/models/CachedPDOIterator.php @@ -0,0 +1,56 @@ +index === null) + { + parent::rewind(); + } + $this->index = 0; + } + + public function current(): mixed + { + if ($this->offsetExists($this->index)) + { + return $this->offsetGet($this->index); + } + return parent::current(); + } + + public function key(): mixed + { + return $this->index; + } + + public function next(): void + { + $this->index++; + if (!$this->offsetExists($this->index)) + { + parent::next(); + } + } + + public function valid(): bool + { + return $this->offsetExists($this->index) || parent::valid(); + } +}