Created
March 13, 2013 13:35
-
-
Save hakre/5152090 to your computer and use it in GitHub Desktop.
CachtingIterator for a PDOStatement with support for multiple iterations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment