fork download
  1. <?php
  2.  
  3. class Node {
  4.  
  5. private $parentNode = NULL;
  6. private $child = array();
  7. protected $name;
  8.  
  9.  
  10. public function __construct($name) {
  11. $this->name = $name;
  12. }
  13. public function __toString() {
  14. $node = "\nNameNode: " . $this->name . "\n";
  15. $node .= "ParentName: " . $this->parentNode->getName() . "\n";
  16. foreach ($this->child as $child) {
  17. $node .= "ChildName: " . $child->getName() ."\n";
  18. }
  19. return $node;
  20. }
  21. public function getName() {
  22. return $this->name;
  23. }
  24.  
  25. public function getParentNode(){
  26. if (isset($this->parentNode)) {
  27. return $this->parentNode;
  28. }
  29. return NULL;
  30. }
  31. public function getChildrenNode() {
  32. return $this->child;
  33. }
  34.  
  35. public function getAllParents(){
  36. if (NULL == $this->getParentNode()) {
  37. return array();
  38. } else {
  39. $parent = $this->getParentNode();
  40. $parents = $parent->getAllParents();
  41. $parents[] = $parent;
  42. return $parents;
  43. }
  44. }
  45. public function getAllChildren(){
  46. $childrenNode = $this->getChildrenNode();
  47. $children = $childrenNode;
  48. foreach ($childrenNode as $child) {
  49. $children = array_merge($children, $child->getAllChildren());
  50. }
  51.  
  52. return $children;
  53. }
  54.  
  55. public function getDepth() {
  56. $children = $this->child;
  57. $depth = -1;
  58. //echo $this->name() . '->';
  59. foreach ($children as $child) {
  60. $depth = max($depth, $seed->getDepth());
  61. }
  62. $depth++;
  63. return $depth;
  64. }
  65.  
  66. public function getDescendantsCount(){
  67. $DescendantsCount = 0;
  68. $children = $this->child;
  69. echo $this->getName() . '->';
  70. foreach ($children as $child) {
  71. $DescendantsCount++;
  72. $DescendantsCount += $child->getDescendantsCount();
  73. }
  74. return $DescendantsCount;
  75. }
  76.  
  77. public function appendChild(Node $child) {
  78. if ($this == $child) {
  79. try {
  80. throw new Exception("ОШИБКА! Нельзя делать обьект " . $this->getName() . " родителем самого себя.\n");
  81. } catch(Exception $e) {
  82. echo $e->getMessage();
  83. }
  84. }
  85. $child->remove();
  86.  
  87. $this->child[] = $child;
  88. $child->parentNode = $this;
  89. }
  90.  
  91. public function remove(){
  92. if (isset($this->parentNode)) {
  93. echo "\nУдаление\n";
  94. $parentNode = $this->parentNode;
  95.  
  96. $children = $parentNode->child;
  97. $key = array_search($this , $children);
  98. unset($parentNode->child[$key]);
  99.  
  100. $this->parentNode = null;
  101. }
  102. }
  103.  
  104. public function getNextSibling(){
  105. $parentNode = $this->parentNode;
  106. if (NULL ==$parentNode) {
  107. return NULL;
  108. }
  109. $siblings = $parentNode->getChildrenNode();
  110.  
  111. $key = array_search( $siblings, $this);
  112. if (isset($siblings[$key+1])) {
  113. return $siblings[$key+1];
  114. } else {
  115. return NULL;
  116. }
  117. }
  118. public function getPreviousSibling(){
  119. $parentNode = $this->parentNode;
  120. if (NULL ==$parentNode) {
  121. return NULL;
  122. }
  123. $siblings = $parentNode->getChildrenNode();
  124. $key = array_search( $siblings, $this);
  125. if (isset($siblings[$key-1])) {
  126. return $siblings[$key-1];
  127. } else {
  128. return NULL;
  129. }
  130. }
  131.  
  132.  
  133. public function walk($function) {
  134. $allChildren = $this->getAllChildren();
  135. $return = array();
  136. $callback =
  137. function ( $child ) use ( $function, &$return) {
  138. $return[] = "Имя узла: " . $child->getName() . " Количество потомков: "
  139. . $child->$function();
  140. };
  141. array_walk( $allChildren, $callback );
  142. print_r($return);
  143. }
  144. // обходит всех потомков $node, для каждого вызывая функцию
  145.  
  146.  
  147.  
  148. }
  149.  
  150.  
  151. // Проверим работу nextSibling
  152. $node1 = new Node('node1');
  153.  
  154. $node10 = new Node('node10');
  155. $node11 = new Node('node11');
  156. $node12 = new Node('node12');
  157. $node13 = new Node('node13');
  158.  
  159. $node1->appendChild($node10);
  160. $node1->appendChild($node11);
  161. $node1->appendChild($node12);
  162. $node1->appendChild($node13);
  163.  
  164. // провеим кто идет за node10
  165. $next = $node10->getNextSibling();
  166. echo $next;
  167.  
  168. $node12->remove();
  169. $node11->remove();
  170.  
  171. // проверим — должно получиться node13
  172. $next2 = $node10->getNextSibling();
  173. var_dump($next2);
  174.  
  175.  
  176.  
Success #stdin #stdout #stderr 0.01s 20568KB
stdin
Standard input is empty
stdout
NameNode: node11
ParentName: node1

Удаление

Удаление
NULL
stderr
PHP Warning:  array_search() expects parameter 2 to be array, object given in /home/DTJN6t/prog.php on line 112
PHP Warning:  array_search() expects parameter 2 to be array, object given in /home/DTJN6t/prog.php on line 112