recursivetreeiterator.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /** @file recursivetreeiterator.inc
  3. * @ingroup SPL
  4. * @brief class RecursiveTreeIterator
  5. * @author Marcus Boerger, Johannes Schlueter
  6. * @date 2005 - 2009
  7. *
  8. * SPL - Standard PHP Library
  9. */
  10. /** @ingroup SPL
  11. * @brief RecursiveIteratorIterator to generate ASCII graphic trees for the
  12. * entries in a RecursiveIterator
  13. * @author Marcus Boerger, Johannes Schlueter
  14. * @version 1.1
  15. * @since PHP 5.3
  16. */
  17. class RecursiveTreeIterator extends RecursiveIteratorIterator
  18. {
  19. const BYPASS_CURRENT = 0x00000004;
  20. const BYPASS_KEY = 0x00000008;
  21. private $rit_flags;
  22. /**
  23. * @param it iterator to use as inner iterator
  24. * @param rit_flags flags passed to RecursiveIteratoIterator (parent)
  25. * @param cit_flags flags passed to RecursiveCachingIterator (for hasNext)
  26. * @param mode mode passed to RecursiveIteratoIterator (parent)
  27. */
  28. function __construct(RecursiveIterator $it, $rit_flags = self::BYPASS_KEY, $cit_flags = CachingIterator::CATCH_GET_CHILD, $mode = self::SELF_FIRST)
  29. {
  30. parent::__construct(new RecursiveCachingIterator($it, $cit_flags), $mode, $rit_flags);
  31. $this->rit_flags = $rit_flags;
  32. }
  33. private $prefix = array(0=>'', 1=>'| ', 2=>' ', 3=>'|-', 4=>'\-', 5=>'');
  34. /** Prefix used to start elements. */
  35. const PREFIX_LEFT = 0;
  36. /** Prefix used if $level < depth and hasNext($level) == true. */
  37. const PREFIX_MID_HAS_NEXT = 1;
  38. /** Prefix used if $level < depth and hasNext($level) == false. */
  39. const PREFIX_MID_LAST = 2;
  40. /** Prefix used if $level == depth and hasNext($level) == true. */
  41. const PREFIX_END_HAS_NEXT = 3;
  42. /** Prefix used if $level == depth and hasNext($level) == false. */
  43. const PREFIX_END_LAST = 4;
  44. /** Prefix used right in front of the current element. */
  45. const PREFIX_RIGHT = 5;
  46. /**
  47. * Set prefix part as used in getPrefix() and stored in $prefix.
  48. * @param $part any PREFIX_* const.
  49. * @param $value new prefix string for specified part.
  50. * @throws OutOfRangeException if 0 > $part or $part > 5.
  51. */
  52. function setPrefixPart($part, $value)
  53. {
  54. if (0 > $part || $part > 5) {
  55. throw new OutOfRangeException();
  56. }
  57. $this->prefix[$part] = (string)$value;
  58. }
  59. /** @return string to place in front of current element
  60. */
  61. function getPrefix()
  62. {
  63. $tree = '';
  64. for ($level = 0; $level < $this->getDepth(); $level++)
  65. {
  66. $tree .= $this->getSubIterator($level)->hasNext() ? $this->prefix[1] : $this->prefix[2];
  67. }
  68. $tree .= $this->getSubIterator($level)->hasNext() ? $this->prefix[3] : $this->prefix[4];
  69. return $this->prefix[0] . $tree . $this->prefix[5];
  70. }
  71. /** @return string presentation build for current element
  72. */
  73. function getEntry()
  74. {
  75. return @(string)parent::current();
  76. }
  77. /** @return string to place after the current element
  78. */
  79. function getPostfix()
  80. {
  81. return '';
  82. }
  83. /** @return the current element prefixed and postfixed
  84. */
  85. function current()
  86. {
  87. if ($this->rit_flags & self::BYPASS_CURRENT)
  88. {
  89. return parent::current();
  90. }
  91. else
  92. {
  93. return $this->getPrefix() . $this->getEntry() . $this->getPostfix();
  94. }
  95. }
  96. /** @return the current key prefixed and postfixed
  97. */
  98. function key()
  99. {
  100. if ($this->rit_flags & self::BYPASS_KEY)
  101. {
  102. return parent::key();
  103. }
  104. else
  105. {
  106. return $this->getPrefix() . parent::key() . $this->getPostfix();
  107. }
  108. }
  109. /** Aggregates the inner iterator
  110. */
  111. function __call($func, $params)
  112. {
  113. return call_user_func_array(array($this->getSubIterator(), $func), $params);
  114. }
  115. }
  116. ?>