| 1 |
lars |
1 |
<?php
|
|
|
2 |
/*
|
|
|
3 |
* $Id: Iterator.php 3884 2008-02-22 18:26:35Z jwage $
|
|
|
4 |
*
|
|
|
5 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
6 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
7 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
8 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
9 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
10 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
11 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
12 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
13 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
14 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
15 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
16 |
*
|
|
|
17 |
* This software consists of voluntary contributions made by many individuals
|
|
|
18 |
* and is licensed under the LGPL. For more information, see
|
|
|
19 |
* <http://www.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* Doctrine_Collection_OnDemand
|
|
|
24 |
* iterates through Doctrine_Records hydrating one at a time
|
|
|
25 |
*
|
|
|
26 |
* @package Doctrine
|
|
|
27 |
* @subpackage Collection
|
|
|
28 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
|
|
29 |
* @link www.doctrine-project.org
|
|
|
30 |
* @since 1.1
|
|
|
31 |
* @version $Revision$
|
|
|
32 |
* @author Geoff Davis <geoff.davis@gmedia.com.au>
|
|
|
33 |
*/
|
|
|
34 |
class Doctrine_Collection_OnDemand implements Iterator
|
|
|
35 |
{
|
|
|
36 |
protected $_stmt;
|
|
|
37 |
protected $_current;
|
|
|
38 |
protected $_tableAliasMap;
|
|
|
39 |
protected $_hydrator;
|
|
|
40 |
protected $index;
|
|
|
41 |
|
|
|
42 |
public function __construct($stmt, $hydrator, $tableAliasMap)
|
|
|
43 |
{
|
|
|
44 |
$this->_stmt = $stmt;
|
|
|
45 |
$this->_hydrator = $hydrator;
|
|
|
46 |
$this->_tableAliasMap = $tableAliasMap;
|
|
|
47 |
$this->_current = null;
|
|
|
48 |
$this->index = 0;
|
|
|
49 |
|
|
|
50 |
$this->_hydrateCurrent();
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
private function _hydrateCurrent()
|
|
|
54 |
{
|
|
|
55 |
$record = $this->_hydrator->hydrateResultSet($this->_stmt);
|
|
|
56 |
if ($record instanceof Doctrine_Collection) {
|
|
|
57 |
$this->_current = $record->getFirst();
|
|
|
58 |
} else if (is_array($record) && count($record) == 0) {
|
|
|
59 |
$this->_current = null;
|
|
|
60 |
} else if (is_array($record) && isset($record[0])) {
|
|
|
61 |
$this->_current = $record[0];
|
|
|
62 |
} else {
|
|
|
63 |
$this->_current = $record;
|
|
|
64 |
}
|
|
|
65 |
}
|
|
|
66 |
|
|
|
67 |
public function rewind()
|
|
|
68 |
{
|
|
|
69 |
$this->index = 0;
|
|
|
70 |
$this->_stmt->closeCursor();
|
|
|
71 |
$this->_stmt->execute();
|
|
|
72 |
$this->_hydrator->onDemandReset();
|
|
|
73 |
$this->_hydrateCurrent();
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
public function key()
|
|
|
77 |
{
|
|
|
78 |
return $this->index;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
public function current()
|
|
|
82 |
{
|
|
|
83 |
return $this->_current;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
public function next()
|
|
|
87 |
{
|
|
|
88 |
$this->_current = null;
|
|
|
89 |
$this->index++;
|
|
|
90 |
$this->_hydrateCurrent();
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
public function valid()
|
|
|
94 |
{
|
|
|
95 |
if ( ! is_null($this->_current) && $this->_current !== false) {
|
|
|
96 |
return true;
|
|
|
97 |
}
|
|
|
98 |
return false;
|
|
|
99 |
}
|
|
|
100 |
}
|