Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: PreOrderIterator.php 7490 2010-03-29 19:53:27Z 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_Node_NestedSet_PreOrderIterator
24
 *
25
 * @package     Doctrine
26
 * @subpackage  Node
27
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
28
 * @link        www.doctrine-project.org
29
 * @since       1.0
30
 * @version     $Revision: 7490 $
31
 * @author      Joe Simms <joe.simms@websites4.com>
32
 */
33
class Doctrine_Node_NestedSet_PreOrderIterator implements Iterator
34
{
35
    /**
36
     * @var Doctrine_Collection $collection
37
     */
38
    protected $collection;
39
 
40
    /**
41
     * @var array $keys
42
     */
43
    protected $keys;
44
 
45
    /**
46
     * @var mixed $key
47
     */
48
    protected $key;
49
 
50
    /**
51
     * @var integer $index
52
     */
53
    protected $index;
54
 
55
    /**
56
     * @var integer $index
57
     */
58
    protected $prevIndex;
59
 
60
    /**
61
     * @var integer $index
62
     */
63
    protected $traverseLevel;
64
 
65
    /**
66
     * @var integer $count
67
     */
68
    protected $count;
69
 
70
    public function __construct($record, $opts)
71
    {
72
        $componentName = $record->getTable()->getComponentName();
73
 
74
        $q = $record->getTable()->createQuery();
75
 
76
        $params = array($record->get('lft'), $record->get('rgt'));
77
        if (isset($opts['include_record']) && $opts['include_record']) {
78
            $query = $q->where("$componentName.lft >= ? AND $componentName.rgt <= ?", $params)->orderBy("$componentName.lft asc");
79
        } else {
80
            $query = $q->where("$componentName.lft > ? AND $componentName.rgt < ?", $params)->orderBy("$componentName.lft asc");
81
        }
82
 
83
        $query = $record->getTable()->getTree()->returnQueryWithRootId($query, $record->getNode()->getRootValue());
84
 
85
        $this->maxLevel   = isset($opts['depth']) ? ($opts['depth'] + $record->getNode()->getLevel()) : 0;
86
        $this->options    = $opts;
87
        $this->collection = isset($opts['collection']) ? $opts['collection'] : $query->execute();
88
        $this->keys       = $this->collection->getKeys();
89
        $this->count      = $this->collection->count();
90
        $this->index      = -1;
91
        $this->level      = $record->getNode()->getLevel();
92
        $this->prevLeft   = $record->getNode()->getLeftValue();
93
 
94
        // clear the table identity cache
95
        $record->getTable()->clear();
96
    }
97
 
98
    /**
99
     * rewinds the iterator
100
     *
101
     * @return void
102
     */
103
    public function rewind()
104
    {
105
        $this->index = -1;
106
        $this->key = null;
107
    }
108
 
109
    /**
110
     * returns the current key
111
     *
112
     * @return integer
113
     */
114
    public function key()
115
    {
116
        return $this->key;
117
    }
118
 
119
    /**
120
     * returns the current record
121
     *
122
     * @return Doctrine_Record
123
     */
124
    public function current()
125
    {
126
        $record = $this->collection->get($this->key);
127
        $record->getNode()->setLevel($this->level);
128
        return $record;
129
    }
130
 
131
    /**
132
     * advances the internal pointer
133
     *
134
     * @return void
135
     */
136
    public function next()
137
    {
138
        while ($current = $this->advanceIndex()) {
139
            if ($this->maxLevel && ($this->level > $this->maxLevel)) {
140
                continue;
141
            }
142
 
143
            return $current;
144
        }
145
 
146
        return false;
147
    }
148
 
149
    /**
150
     * @return boolean                          whether or not the iteration will continue
151
     */
152
    public function valid()
153
    {
154
        return ($this->index < $this->count);
155
    }
156
 
157
    public function count()
158
    {
159
        return $this->count;
160
    }
161
 
162
    private function updateLevel()
163
    {
164
        if ( ! (isset($this->options['include_record']) && $this->options['include_record'] && $this->index == 0)) {
165
            $left = $this->collection->get($this->key)->getNode()->getLeftValue();
166
            $this->level += $this->prevLeft - $left + 2;
167
            $this->prevLeft = $left;
168
        }
169
    }
170
 
171
    private function advanceIndex()
172
    {
173
        $this->index++;
174
        $i = $this->index;
175
        if (isset($this->keys[$i])) {
176
            $this->key   = $this->keys[$i];
177
            $this->updateLevel();
178
            return $this->current();
179
        }
180
 
181
        return false;
182
    }
183
}