SieveTree.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php namespace Sieve;
  2. class SieveTree
  3. {
  4. protected $childs_;
  5. protected $parents_;
  6. protected $nodes_;
  7. protected $max_id_;
  8. protected $dump_;
  9. public function __construct($name = 'tree')
  10. {
  11. $this->childs_ = array();
  12. $this->parents_ = array();
  13. $this->nodes_ = array();
  14. $this->max_id_ = 0;
  15. $this->parents_[0] = null;
  16. $this->nodes_[0] = $name;
  17. }
  18. public function addChild(SieveDumpable $child)
  19. {
  20. return $this->addChildTo($this->max_id_, $child);
  21. }
  22. public function addChildTo($parent_id, SieveDumpable $child)
  23. {
  24. if (!is_int($parent_id)
  25. || !isset($this->nodes_[$parent_id]))
  26. return null;
  27. if (!isset($this->childs_[$parent_id]))
  28. $this->childs_[$parent_id] = array();
  29. $child_id = ++$this->max_id_;
  30. $this->nodes_[$child_id] = $child;
  31. $this->parents_[$child_id] = $parent_id;
  32. array_push($this->childs_[$parent_id], $child_id);
  33. return $child_id;
  34. }
  35. public function getRoot()
  36. {
  37. return 0;
  38. }
  39. public function getChilds($node_id)
  40. {
  41. if (!is_int($node_id)
  42. || !isset($this->nodes_[$node_id]))
  43. return null;
  44. if (!isset($this->childs_[$node_id]))
  45. return array();
  46. return $this->childs_[$node_id];
  47. }
  48. public function getNode($node_id)
  49. {
  50. if ($node_id == 0 || !is_int($node_id)
  51. || !isset($this->nodes_[$node_id]))
  52. return null;
  53. return $this->nodes_[$node_id];
  54. }
  55. public function dump()
  56. {
  57. $this->dump_ = $this->nodes_[$this->getRoot()] ."\n";
  58. $this->dumpChilds_($this->getRoot(), ' ');
  59. return $this->dump_;
  60. }
  61. protected function dumpChilds_($parent_id, $prefix)
  62. {
  63. if (!isset($this->childs_[$parent_id]))
  64. return;
  65. $childs = $this->childs_[$parent_id];
  66. $last_child = count($childs);
  67. for ($i=1; $i <= $last_child; ++$i)
  68. {
  69. $child_node = $this->nodes_[$childs[$i-1]];
  70. $infix = ($i == $last_child ? '`--- ' : '|--- ');
  71. $this->dump_ .= $prefix . $infix . $child_node->dump() . " (id:" . $childs[$i-1] . ")\n";
  72. $next_prefix = $prefix . ($i == $last_child ? ' ' : '| ');
  73. $this->dumpChilds_($childs[$i-1], $next_prefix);
  74. }
  75. }
  76. public function getText()
  77. {
  78. $this->dump_ = '';
  79. $this->childText_($this->getRoot());
  80. return $this->dump_;
  81. }
  82. protected function childText_($parent_id)
  83. {
  84. if (!isset($this->childs_[$parent_id]))
  85. return;
  86. $childs = $this->childs_[$parent_id];
  87. for ($i = 0; $i < count($childs); ++$i)
  88. {
  89. $child_node = $this->nodes_[$childs[$i]];
  90. $this->dump_ .= $child_node->text();
  91. $this->childText_($childs[$i]);
  92. }
  93. }
  94. }