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