# Source for file Node.php

Documentation is available at Node.php

1. <?php
2. /*
3. * $Id: Node.php 1949 2007-07-08 12:57:52Z romanb $
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. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
26. * @category Object Relational Mapping
27. * @link www.phpdoctrine.com
28. * @since 1.0
29. * @version $Revision: 1949 $
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;
39. /**
40. * @param array $options
41. */
42. protected $options;
44. /**
45. * @param string $iteratorType (Pre | Post | Level)
46. */
47. protected $iteratorType;
49. /**
50. * @param array $iteratorOptions
51. */
52. protected $iteratorOptions;
54. /**
55. * The tree to which the node belongs.
56. *
57. * @var unknown_type
58. */
59. protected $_tree;
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. $this->_tree = $this->record->getTable()->getTree();
72. }
74. /**
75. * factory method to return node instance based upon chosen implementation
76. *
77. * @param object $record instance of Doctrine_Record
78. * @param string $impName implementation (NestedSet, AdjacencyList, MaterializedPath)
79. * @param array $options options
80. * @return object $options instance of Doctrine_Node
81. */
82. public static function factory(Doctrine_Record $record, $implName, $options = array())
83. {
84. $class = 'Doctrine_Node_' . $implName;
86. if (!class_exists($class)) {
87. throw new Doctrine_Node_Exception("The class $class must exist and extend Doctrine_Node");
88. }
90. return new $class($record, $options);
91. }
93. /**
94. * setter for record attribute
95. *
96. * @param object $record instance of Doctrine_Record
97. */
98. public function setRecord(Doctrine_Record $record)
99. {
100. $this->record = $record;
101. }
103. /**
104. * getter for record attribute
105. *
106. * @return object instance of Doctrine_Record
107. */
108. public function getRecord()
109. {
110. return $this->record;
111. }
113. /**
114. * convenience function for getIterator
115. *
116. * @param string $type type of iterator (Pre | Post | Level)
117. * @param array $options options
118. */
119. public function traverse($type = 'Pre', $options = array())
120. {
121. return $this->getIterator($type, $options);
122. }
124. /**
125. * get iterator
126. *
127. * @param string $type type of iterator (Pre | Post | Level)
128. * @param array $options options
129. */
130. public function getIterator($type = null, $options = null)
131. {
132. if ($type === null) {
133. $type = (isset($this->iteratorType) ? $this->iteratorType : 'Pre');
134. }
136. if ($options === null) {
137. $options = (isset($this->iteratorOptions) ? $this->iteratorOptions : array());
138. }
140. $implName = $this->record->getTable()->getOption('treeImpl');
141. $iteratorClass = 'Doctrine_Node_' . $implName . '_' . ucfirst(strtolower($type)) . 'OrderIterator';
143. return new $iteratorClass($this->record, $options);
144. }
146. /**
147. * sets node's iterator type
148. *
149. * @param int
150. */
151. public function setIteratorType($type)
152. {
153. $this->iteratorType = $type;
154. }
156. /**
157. * sets node's iterator options
158. *
159. * @param int
160. */
161. public function setIteratorOptions($options)
162. {
163. $this->iteratorOptions = $options;
164. }
165. }