node.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. <?php
  2. /**
  3. * Tree data structure, used for rendering purposes.
  4. *
  5. * @author Taddeus Kroes
  6. * @date 13-07-2012
  7. */
  8. namespace WebBasics;
  9. require_once 'base.php';
  10. /**
  11. * Tree node.
  12. *
  13. * Each tree node has a (non-unique) name, a list of variables, and zero or
  14. * more children.
  15. *
  16. * @package WebBasics
  17. */
  18. class Node extends Base {
  19. /**
  20. * The number of Node instances, used to create unique node id's.
  21. *
  22. * @var int
  23. */
  24. private static $count = 0;
  25. /**
  26. * The unique id of this Bloc.
  27. *
  28. * @var int
  29. */
  30. private $id;
  31. /**
  32. * The node's name.
  33. *
  34. * @var string
  35. */
  36. private $name;
  37. /**
  38. * An optional parent node.
  39. *
  40. * If NULL, this node is the root of the data tree.
  41. *
  42. * @var Node
  43. */
  44. private $parent_node;
  45. /**
  46. * Child nodes.
  47. *
  48. * @var array
  49. */
  50. private $children = array();
  51. /**
  52. * Variables in this node.
  53. *
  54. * All variables in a node are also available in its descendants through
  55. * {@link get()}.
  56. *
  57. * @var array
  58. */
  59. private $variables = array();
  60. /**
  61. * Constructor.
  62. *
  63. * The id of the node is determined by the node counter.
  64. *
  65. * @param string $name The node's name.
  66. * @param Node|null &$parent_node A parent node (optional).
  67. * @param int|null $id An id to assign. If none is specified, a new unique
  68. * id is generated.
  69. * @uses $count
  70. */
  71. function __construct($name='', Node &$parent_node=null, $id=null) {
  72. $this->id = $id ? $id : ++self::$count;
  73. $this->name = $name;
  74. $this->parent_node = $parent_node;
  75. }
  76. /**
  77. * Get the node's unique id.
  78. *
  79. * @return int The node's id.
  80. */
  81. function get_id() {
  82. return $this->id;
  83. }
  84. /**
  85. * Get the node's name.
  86. *
  87. * @return string The node's name.
  88. */
  89. function get_name() {
  90. return $this->name;
  91. }
  92. /**
  93. * Get the node's parent.
  94. *
  95. * @return Node|null The parent node if any, NULL otherwise.
  96. */
  97. function get_parent() {
  98. return $this->parent_node;
  99. }
  100. /**
  101. * Get the node's children.
  102. *
  103. * @return array A list of child nodes.
  104. */
  105. function get_children() {
  106. return $this->children;
  107. }
  108. /**
  109. * Check if a node is the same instance or a copy of this node.
  110. *
  111. * @param Node $node The node to compare this node to.
  112. * @return bool Whether the nodes have the same unique id.
  113. */
  114. function is(Node $node) {
  115. return $node->get_id() == $this->id;
  116. }
  117. /**
  118. * Check if this node is the root node of the tree.
  119. *
  120. * A node is the root node if it has no parent.
  121. *
  122. * @return bool Whether this node is the root node.
  123. */
  124. function is_root() {
  125. return $this->parent_node === null;
  126. }
  127. /**
  128. * Check if this node is a leaf node of the tree.
  129. *
  130. * A node is a leaf if it has no children.
  131. *
  132. * @return bool Whether this node is a leaf node.
  133. */
  134. function is_leaf() {
  135. return !count($this->children);
  136. }
  137. /**
  138. * Add a child node.
  139. *
  140. * @param Node &$node The child node to add.
  141. * @param bool $set_parent Whether to set this node as the child's parent
  142. * (defaults to TRUE).
  143. */
  144. function add_child(Node &$node, $set_parent=true) {
  145. $this->children[] = $node;
  146. $set_parent && $node->set_parent($this);
  147. }
  148. /**
  149. * Add a child node.
  150. *
  151. * @param string $name The name of the node to add.
  152. * @param array $data Data to set in the created node (optional).
  153. * @return Node The created node.
  154. */
  155. function add($name, array $data=array()) {
  156. $node = new self($name, $this);
  157. $this->add_child($node, false);
  158. return $node->set($data);
  159. }
  160. /**
  161. * Remove a child node.
  162. *
  163. * @param Node &$child The node to remove.
  164. */
  165. function remove_child(Node &$child) {
  166. foreach( $this->children as $i => $node )
  167. $node->is($child) && array_splice($this->children, $i, 1);
  168. }
  169. /**
  170. * Remove this node from its parent.
  171. *
  172. * @throws \RuntimeException If the node has no parent.
  173. * @return Node This node.
  174. */
  175. function remove() {
  176. if( $this->is_root() )
  177. throw new \RuntimeException('Cannot remove the root node of a tree.');
  178. $this->parent_node->remove_child($this);
  179. foreach( $this->children as $child )
  180. $child->set_parent(null);
  181. return $this;
  182. }
  183. /**
  184. * Set the node's parent.
  185. *
  186. * Removes this node as child of the original parent, if a parent was
  187. * already set.
  188. *
  189. * @param Node|null $parent The parent node to set.
  190. * @return Node This node.
  191. */
  192. function set_parent($parent) {
  193. if( $this->parent_node !== null )
  194. $this->parent_node->remove_child($this);
  195. $this->parent_node = &$parent;
  196. return $this;
  197. }
  198. /**
  199. * Set the value of one or more variables in the node.
  200. *
  201. * @param string|array $name Either a single variable name, or a set of name/value pairs.
  202. * @param mixed $value The value of a single variable to set.
  203. * @return Node This node.
  204. */
  205. function set($name, $value=null) {
  206. if( is_array($name) ) {
  207. foreach( $name as $var => $val )
  208. $this->variables[$var] = $val;
  209. } else {
  210. $this->variables[$name] = $value;
  211. }
  212. return $this;
  213. }
  214. /**
  215. * Get the value of a variable.
  216. *
  217. * @param string $name The name of the variable to get the value of.
  218. * @return mixed The value of the variable if it exists, NULL otherwise.
  219. */
  220. function get($name) {
  221. // Variable inside this node?
  222. if( isset($this->variables[$name]) )
  223. return $this->variables[$name];
  224. // Variable in one of ancestors?
  225. if( $this->parent_node !== null )
  226. return $this->parent_node->get($name);
  227. // All nodes up to the tree's root node do not contain the variable
  228. return null;
  229. }
  230. /**
  231. * Set the value of a variable.
  232. *
  233. * This method provides a shortcut for {@link set()}.
  234. *
  235. * @param string $name The name of the variable to set the value of.
  236. * @param mixed $value The value to set.
  237. */
  238. function __set($name, $value) {
  239. $this->set($name, $value);
  240. }
  241. /**
  242. * Get the value of a variable.
  243. *
  244. * This method provides a shortcut for {@link get()}.
  245. *
  246. * @param string $name The name of the variable to get the value of.
  247. * @return mixed The value of the variable if it exists, NULL otherwise.
  248. */
  249. function __get($name) {
  250. return $this->get($name);
  251. }
  252. /**
  253. * Find all child nodes that have the specified name.
  254. *
  255. * @param string $name The name of the nodes to find.
  256. * @return array The positively matched nodes.
  257. */
  258. function find($name) {
  259. $has_name = function($child) use ($name) {
  260. return $child->get_name() == $name;
  261. };
  262. return array_values(array_filter($this->children, $has_name));
  263. }
  264. /**
  265. * Create a copy of this node.
  266. *
  267. * The copy will have the same list of children and variables. In case of
  268. * a 'deep copy', the list of children is also cloned recursively.
  269. *
  270. * @param bool $deep Whether to create a deep copy.
  271. * @return Node A copy of this node.
  272. */
  273. function copy($deep=false) {
  274. $copy = new self($this->name, $this->parent_node, $this->id);
  275. $copy->set($this->variables);
  276. foreach( $this->children as $child ) {
  277. if( $deep ) {
  278. $child_copy = $child->copy(true);
  279. $copy->add_child($child_copy);
  280. } else {
  281. $copy->add_child($child, false);
  282. }
  283. }
  284. return $copy;
  285. }
  286. }
  287. ?>