Node.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Server
  17. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Node Tree class for Zend_Server reflection operations
  22. *
  23. * @category Zend
  24. * @package Zend_Server
  25. * @subpackage Reflection
  26. * @version $Id: Node.php 2504 2011-12-28 07:35:29Z liu21st $
  27. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Server_Reflection_Node
  31. {
  32. /**
  33. * Node value
  34. * @var mixed
  35. */
  36. protected $_value = null;
  37. /**
  38. * Array of child nodes (if any)
  39. * @var array
  40. */
  41. protected $_children = array();
  42. /**
  43. * Parent node (if any)
  44. * @var Zend_Server_Reflection_Node
  45. */
  46. protected $_parent = null;
  47. /**
  48. * Constructor
  49. *
  50. * @param mixed $value
  51. * @param Zend_Server_Reflection_Node $parent Optional
  52. * @return Zend_Server_Reflection_Node
  53. */
  54. public function __construct($value, Zend_Server_Reflection_Node $parent = null)
  55. {
  56. $this->_value = $value;
  57. if (null !== $parent) {
  58. $this->setParent($parent, true);
  59. }
  60. return $this;
  61. }
  62. /**
  63. * Set parent node
  64. *
  65. * @param Zend_Server_Reflection_Node $node
  66. * @param boolean $new Whether or not the child node is newly created
  67. * and should always be attached
  68. * @return void
  69. */
  70. public function setParent(Zend_Server_Reflection_Node $node, $new = false)
  71. {
  72. $this->_parent = $node;
  73. if ($new) {
  74. $node->attachChild($this);
  75. return;
  76. }
  77. }
  78. /**
  79. * Create and attach a new child node
  80. *
  81. * @param mixed $value
  82. * @access public
  83. * @return Zend_Server_Reflection_Node New child node
  84. */
  85. public function createChild($value)
  86. {
  87. $child = new self($value, $this);
  88. return $child;
  89. }
  90. /**
  91. * Attach a child node
  92. *
  93. * @param Zend_Server_Reflection_Node $node
  94. * @return void
  95. */
  96. public function attachChild(Zend_Server_Reflection_Node $node)
  97. {
  98. $this->_children[] = $node;
  99. if ($node->getParent() !== $this) {
  100. $node->setParent($this);
  101. }
  102. }
  103. /**
  104. * Return an array of all child nodes
  105. *
  106. * @return array
  107. */
  108. public function getChildren()
  109. {
  110. return $this->_children;
  111. }
  112. /**
  113. * Does this node have children?
  114. *
  115. * @return boolean
  116. */
  117. public function hasChildren()
  118. {
  119. return count($this->_children) > 0;
  120. }
  121. /**
  122. * Return the parent node
  123. *
  124. * @return null|Zend_Server_Reflection_Node
  125. */
  126. public function getParent()
  127. {
  128. return $this->_parent;
  129. }
  130. /**
  131. * Return the node's current value
  132. *
  133. * @return mixed
  134. */
  135. public function getValue()
  136. {
  137. return $this->_value;
  138. }
  139. /**
  140. * Set the node value
  141. *
  142. * @param mixed $value
  143. * @return void
  144. */
  145. public function setValue($value)
  146. {
  147. $this->_value = $value;
  148. }
  149. /**
  150. * Retrieve the bottommost nodes of this node's tree
  151. *
  152. * Retrieves the bottommost nodes of the tree by recursively calling
  153. * getEndPoints() on all children. If a child is null, it returns the parent
  154. * as an end point.
  155. *
  156. * @return array
  157. */
  158. public function getEndPoints()
  159. {
  160. $endPoints = array();
  161. if (!$this->hasChildren()) {
  162. return $endPoints;
  163. }
  164. foreach ($this->_children as $child) {
  165. $value = $child->getValue();
  166. if (null === $value) {
  167. $endPoints[] = $this;
  168. } elseif ((null !== $value)
  169. && $child->hasChildren())
  170. {
  171. $childEndPoints = $child->getEndPoints();
  172. if (!empty($childEndPoints)) {
  173. $endPoints = array_merge($endPoints, $childEndPoints);
  174. }
  175. } elseif ((null !== $value) && !$child->hasChildren()) {
  176. $endPoints[] = $child;
  177. }
  178. }
  179. return $endPoints;
  180. }
  181. }