/
axPDOStatementIterator.class.php
83 lines (74 loc) · 2.15 KB
/
axPDOStatementIterator.class.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
/**
* @brief PDOStatement iterator class file
* @file axPDOStatementIterator.class.php
*/
/**
* @brief PDO Statement Iterator Class
*
* This class is a PDOStatement decorator to enable its use as an iterator instead of a traversable instance.
*
* @class axPDOStatementIterator
* @author Delespierre
* @ingroup Model
* @copyright Copyright 2010-2011, Benjamin Delespierre (http://bdelespierre.fr)
* @license http://www.gnu.org/licenses/lgpl.html Lesser General Public Licence version 3
*/
class axPDOStatementIterator extends IteratorIterator implements SeekableIterator, Countable {
/**
* @brief Internal statement reference
* @property PDOStatement $_statement
*/
protected $_statement;
/**
* @brief Internal counter
* @property integer $_count
*/
protected $_count;
/**
* @brief Constructor
* @param PDOStatement $statement
*/
public function __construct (PDOStatement $statement) {
parent::__construct($this->_statement = $statement);
}
/**
* @brief SeekableIterator::seek() Implementation
* @param integer $position The position to seek
* @return void
*/
public function seek ($position) {
if ($position > $this->count() || $position < $this->key())
throw new OutOfBoundsException("Cannot seek to $position", 2008);
for ($i = $this->key(); $i < $position; $i++)
$this->next();
}
/**
* @breif Countable::count() implementation
*
* Get the number of items in the iterator.
*
* @return integer
*/
public function count () {
if (!isset($this->_count))
return $this->_count = $this->_statement->rowCount();
return $this->_count;
}
/**
* @brief Get the first item
* @return mixed
*/
public function first () {
$this->rewind();
return $this->valid() ? $this->current() : null;
}
/**
* @brief Get the last item
* @return mixed
*/
public function last () {
$this->seek($this->count());
return $this->valid() ? $this->current() : null;
}
}