Remove array(...) literals from hphp/system
[hiphop-php.git] / hphp / system / php / spl / iterators / RecursiveTreeIterator.php
blob18e1a92c070db6a593a3da9cf867810df76a138d
1 <?hh // partial
3 // This doc comment block generated by idl/sysdoc.php
4 /**
5 * ( excerpt from http://php.net/manual/en/class.recursivetreeiterator.php
6 * )
8 * Allows iterating over a RecursiveIterator to generate an ASCII graphic
9 * tree.
12 class RecursiveTreeIterator extends RecursiveIteratorIterator
13 implements OuterIterator {
15 const int BYPASS_CURRENT = 4;
16 const int BYPASS_KEY = 8;
17 const int PREFIX_LEFT = 0;
18 const int PREFIX_MID_HAS_NEXT = 1;
19 const int PREFIX_MID_LAST = 2;
20 const int PREFIX_END_HAS_NEXT = 3;
21 const int PREFIX_END_LAST = 4;
22 const int PREFIX_RIGHT = 5;
24 private $flags;
26 private $prefix = varray[
27 "", // PREFIX_LEFT
28 "| ", // PREFIX_MID_HAS_NEXT
29 " ", // PREFIX_MID_LAST
30 "|-", // PREFIX_END_HAS_NEXT
31 "\\-", // PREFIX_END_LAST
32 "", // PREFIX_RIGHT
35 private $postfix = "";
37 public function __construct(\HH\Traversable $it, $flags = null, $cit_flags = null,
38 $mode = null) {
39 if ($flags === null) $flags = self::BYPASS_KEY;
40 if ($mode === null) $mode = RecursiveIteratorIterator::SELF_FIRST;
41 if ($cit_flags === null) $cit_flags = CachingIterator::CATCH_GET_CHILD;
43 if ($it is \IteratorAggregate) {
44 $it = $it->getIterator();
47 $this->flags = $flags;
49 $cachingIterator = new RecursiveCachingIterator($it, $cit_flags);
50 parent::__construct($cachingIterator, $mode);
53 public function setPrefixPart($part, $prefix) {
54 if ($part < 0 || $part > 5) {
55 throw new OutOfRangeException(
56 "Use RecursiveTreeIterator::PREFIX_* constant"
59 $this->prefix[$part] = (string)$prefix;
62 public function getPrefix() {
63 $return = $this->prefix[self::PREFIX_LEFT];
64 $depth = $this->getDepth();
65 for ($i = 0; $i < $depth; ++$i) {
66 $hasNext = $this->getSubIterator($i)->hasNext();
67 if ($hasNext) {
68 $return .= $this->prefix[self::PREFIX_MID_HAS_NEXT];
69 } else {
70 $return .= $this->prefix[self::PREFIX_MID_LAST];
74 $hasNext = $this->getSubIterator($i)->hasNext();
75 if ($hasNext) {
76 $return .= $this->prefix[self::PREFIX_END_HAS_NEXT];
77 } else {
78 $return .= $this->prefix[self::PREFIX_END_LAST];
81 $return .= $this->prefix[self::PREFIX_RIGHT];
82 return $return;
85 public function setPostfix($postfix) {
86 $this->postfix = (string)$postfix;
89 public function getEntry() {
90 $current = $this->getInnerIterator()->current();
91 if (is_array($current)) {
92 return "Array";
93 } else {
94 return (string)$current;
98 public function getPostfix() {
99 return $this->postfix;
102 public function current() {
103 if ($this->flags & self::BYPASS_CURRENT) {
104 $depth = $this->getDepth();
105 return $this->getSubIterator($depth)->current();
108 $prefix = $this->getPrefix();
109 $entry = $this->getEntry();
110 return $prefix . $entry . $this->postfix;
113 public function key() {
114 $depth = $this->getDepth();
115 $it = $this->getSubIterator($depth);
116 $key = $it->key();
117 if ($this->flags & self::BYPASS_KEY) {
118 return $key;
121 $prefix = $this->getPrefix();
122 return $prefix . $key . $this->postfix;