public
Created

CachtingIterator for a PDOStatement with support for multiple iterations.

  • Download Gist
CachedPDOStatement.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
<?php
/**
* Class CachedPDOStatement
*
* CachingIterator for a PDOStatement
*/
class CachedPDOStatement extends CachingIterator
{
private $index;
 
public function __construct(PDOStatement $statement) {
parent::__construct(new IteratorIterator($statement), self::FULL_CACHE);
}
 
public function rewind() {
if (NULL === $this->index) {
parent::rewind();
}
$this->index = 0;
}
 
public function current() {
if ($this->offsetExists($this->index)) {
return $this->offsetGet($this->index);
}
return parent::current();
}
 
public function key() {
return $this->index;
}
 
public function next() {
$this->index++;
if (!$this->offsetExists($this->index)) {
parent::next();
}
}
 
public function valid() {
return $this->offsetExists($this->index) || parent::valid();
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.