1 |
<?php
|
2 |
/*
|
3 |
* $Id: Node.php 2963 2007-10-21 06:23:59Z Jonathan.Wage $
|
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.phpdoctrine.com>.
|
20 |
*/
|
21 |
|
22 |
/**
|
23 |
* Doctrine_Node
|
24 |
*
|
25 |
* @package Doctrine
|
26 |
* @subpackage Node
|
27 |
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
|
28 |
* @link www.phpdoctrine.com
|
29 |
* @since 1.0
|
30 |
* @version $Revision: 2963 $
|
31 |
* @author Joe Simms <joe.simms@websites4.com>
|
32 |
*/
|
33 |
class Doctrine_Node implements IteratorAggregate
|
34 |
{
|
35 |
/**
|
36 |
* @param object $record reference to associated Doctrine_Record instance
|
37 |
*/
|
38 |
protected $record;
|
39 |
|
40 |
/**
|
41 |
* @param array $options
|
42 |
*/
|
43 |
protected $options;
|
44 |
|
45 |
/**
|
46 |
* @param string $iteratorType (Pre | Post | Level)
|
47 |
*/
|
48 |
protected $iteratorType;
|
49 |
|
50 |
/**
|
51 |
* @param array $iteratorOptions
|
52 |
*/
|
53 |
protected $iteratorOptions;
|
54 |
|
55 |
/**
|
56 |
* The tree to which the node belongs.
|
57 |
*
|
58 |
* @var unknown_type
|
59 |
*/
|
60 |
protected $_tree;
|
61 |
|
62 |
/**
|
63 |
* contructor, creates node with reference to record and any options
|
64 |
*
|
65 |
* @param object $record instance of Doctrine_Record
|
66 |
* @param array $options options
|
67 |
*/
|
68 |
public function __construct(Doctrine_Record $record, $options)
|
69 |
{
|
70 |
$this->record = $record;
|
71 |
$this->options = $options;
|
72 |
|
73 |
// Make sure that the tree object of the root component is used in the case
|
74 |
// of column aggregation inheritance.
|
75 |
$class = $record->getTable()->getComponentName();
|
76 |
$table = $record->getTable();
|
77 |
if ($table->getOption('inheritanceMap')) {
|
78 |
$subclasses = $table->getOption('subclasses');
|
79 |
while (in_array($class, $subclasses)) {
|
80 |
$class = get_parent_class($class);
|
81 |
}
|
82 |
}
|
83 |
if ($class != $table->getComponentName()) {
|
84 |
$this->_tree = $table->getConnection()->getTable($class)->getTree();
|
85 |
} else {
|
86 |
$this->_tree = $table->getTree();
|
87 |
}
|
88 |
}
|
89 |
|
90 |
/**
|
91 |
* factory method to return node instance based upon chosen implementation
|
92 |
*
|
93 |
* @param object $record instance of Doctrine_Record
|
94 |
* @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath)
|
95 |
* @param array $options options
|
96 |
* @return object $options instance of Doctrine_Node
|
97 |
*/
|
98 |
public static function factory(Doctrine_Record $record, $implName, $options = array())
|
99 |
{
|
100 |
$class = 'Doctrine_Node_' . $implName;
|
101 |
|
102 |
if ( ! class_exists($class)) {
|
103 |
throw new Doctrine_Node_Exception("The class $class must exist and extend Doctrine_Node");
|
104 |
}
|
105 |
|
106 |
return new $class($record, $options);
|
107 |
}
|
108 |
|
109 |
/**
|
110 |
* setter for record attribute
|
111 |
*
|
112 |
* @param object $record instance of Doctrine_Record
|
113 |
*/
|
114 |
public function setRecord(Doctrine_Record $record)
|
115 |
{
|
116 |
$this->record = $record;
|
117 |
}
|
118 |
|
119 |
/**
|
120 |
* getter for record attribute
|
121 |
*
|
122 |
* @return object instance of Doctrine_Record
|
123 |
*/
|
124 |
public function getRecord()
|
125 |
{
|
126 |
return $this->record;
|
127 |
}
|
128 |
|
129 |
/**
|
130 |
* convenience function for getIterator
|
131 |
*
|
132 |
* @param string $type type of iterator (Pre | Post | Level)
|
133 |
* @param array $options options
|
134 |
*/
|
135 |
public function traverse($type = 'Pre', $options = array())
|
136 |
{
|
137 |
return $this->getIterator($type, $options);
|
138 |
}
|
139 |
|
140 |
/**
|
141 |
* get iterator
|
142 |
*
|
143 |
* @param string $type type of iterator (Pre | Post | Level)
|
144 |
* @param array $options options
|
145 |
*/
|
146 |
public function getIterator($type = null, $options = null)
|
147 |
{
|
148 |
if ($type === null) {
|
149 |
$type = (isset($this->iteratorType) ? $this->iteratorType : 'Pre');
|
150 |
}
|
151 |
|
152 |
if ($options === null) {
|
153 |
$options = (isset($this->iteratorOptions) ? $this->iteratorOptions : array());
|
154 |
}
|
155 |
|
156 |
$implName = $this->record->getTable()->getOption('treeImpl');
|
157 |
$iteratorClass = 'Doctrine_Node_' . $implName . '_' . ucfirst(strtolower($type)) . 'OrderIterator';
|
158 |
|
159 |
return new $iteratorClass($this->record, $options);
|
160 |
}
|
161 |
|
162 |
/**
|
163 |
* sets node's iterator type
|
164 |
*
|
165 |
* @param int
|
166 |
*/
|
167 |
public function setIteratorType($type)
|
168 |
{
|
169 |
$this->iteratorType = $type;
|
170 |
}
|
171 |
|
172 |
/**
|
173 |
* sets node's iterator options
|
174 |
*
|
175 |
* @param int
|
176 |
*/
|
177 |
public function setIteratorOptions($options)
|
178 |
{
|
179 |
$this->iteratorOptions = $options;
|
180 |
}
|
181 |
} |