Skip to content

Commit 2679915

Browse files
committed
Add a PDO iterator object.
1 parent f519582 commit 2679915

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/PDO.Iterator.class.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
class PDOIterator implements Iterator {
3+
private $position = 0;
4+
private $pdo;
5+
private $fetchMode;
6+
private $nextResult;
7+
8+
public function __construct(PDOStatement $pdo, $fetchMode = PDO::FETCH_ASSOC) {
9+
$this->position = 0;
10+
$this->pdo = $pdo;
11+
$this->fetchMode = $fetchMode;
12+
}
13+
14+
function rewind() {
15+
$this->position = 0;
16+
$this->pdo->execute();
17+
}
18+
19+
function current() {
20+
return $this->nextResult;
21+
}
22+
23+
function key() {
24+
return $this->position;
25+
}
26+
27+
function next() {
28+
++$this->position;
29+
$this->nextResult = $this->pdo->fetch($this->fetchMode, PDO::FETCH_ORI_NEXT);
30+
}
31+
32+
function valid() {
33+
$invalid = $this->nextResult === false;
34+
if ($invalid) {
35+
$this->pdo->closeCursor();
36+
}
37+
return !$invalid;
38+
}
39+
}

0 commit comments

Comments
 (0)