*/ class RdsLogIterator implements \Iterator { /** @var QueryBuilder */ protected $queryBuilder; /** @var \common_persistence_SqlPersistence */ protected $persistence; /** @var array */ protected $current; /** @var int */ protected $initialLimit; /** @var int */ protected $firstResult; /** @var int */ protected $currentKey; /** * RdsRequestLogIterator constructor. * @param \common_persistence_SqlPersistence $persistence * @param QueryBuilder $queryBuilder */ public function __construct(\common_persistence_SqlPersistence $persistence, QueryBuilder $queryBuilder) { $this->queryBuilder = $queryBuilder; $this->persistence = $persistence; $this->initialLimit = $queryBuilder->getMaxResults(); if ($this->initialLimit === null) { $this->initialLimit = PHP_INT_MAX; } $this->firstResult = $queryBuilder->getFirstResult(); if ($this->firstResult === null) { $this->firstResult = 0; } $this->queryBuilder->setMaxResults(1); $this->rewind(); } /** * */ public function current() { return $this->current; } public function next() { if ($this->valid()) { $this->queryBuilder->setFirstResult($this->currentKey); $sql = $this->queryBuilder->getSQL(); $params = $this->queryBuilder->getParameters(); $stmt = $this->persistence->query($sql, $params); $data = $stmt->fetch(\PDO::FETCH_ASSOC); if (empty($data)) { $this->current = null; } else { $this->current = $data; } $this->currentKey++; } else { $this->current = null; } } public function key() { return $this->currentKey - 1; } /** * @return bool */ public function valid() { if ($this->currentKey === $this->firstResult) { //initial state return true; } return $this->current !== null && ($this->currentKey + $this->firstResult) < $this->initialLimit; } public function rewind() { $this->currentKey = $this->firstResult; $this->next(); } }