Remove array(...) literals from hphp/system
[hiphop-php.git] / hphp / system / php / spl / iterators / IteratorIterator.php
blobf9b45db403b32ffa38d6ac6985f2ca9dca2b14c1
1 <?hh // partial
3 // This doc comment block generated by idl/sysdoc.php
4 /**
5 * ( excerpt from http://php.net/manual/en/class.iteratoriterator.php )
7 * This iterator wrapper allows the conversion of anything that is
8 * Traversable into an Iterator. It is important to understand that most
9 * classes that do not implement Iterators have reasons as most likely they
10 * do not allow the full Iterator feature set. If so, techniques should be
11 * provided to prevent misuse, otherwise expect exceptions or fatal errors.
14 class IteratorIterator implements OuterIterator {
15 private $iterator;
16 private $current;
17 private $key;
18 private $position;
20 // This doc comment block generated by idl/sysdoc.php
21 /**
22 * ( excerpt from http://php.net/manual/en/iteratoriterator.construct.php )
24 * Creates an iterator from anything that is traversable.
26 * @iterator mixed The traversable iterator.
28 * @return mixed No value is returned.
30 public function __construct($iterator) {
31 while ($iterator is IteratorAggregate) {
32 $iterator = $iterator->getIterator();
34 if ($iterator is \HH\Iterator) {
35 $this->iterator = $iterator;
36 } else if ($iterator is \SimpleXMLElement) {
37 $this->iterator = $iterator->getIterator();
38 } else {
39 throw new Exception(
40 "Need to pass a Traversable that is convertable to an iterator");
44 // This doc comment block generated by idl/sysdoc.php
45 /**
46 * ( excerpt from
47 * http://php.net/manual/en/iteratoriterator.getinneriterator.php )
49 * Get the inner iterator.
51 * @return mixed The inner iterator as passed to
52 * IteratorIterator::__construct().
54 public function getInnerIterator() {
55 return $this->iterator;
58 // This doc comment block generated by idl/sysdoc.php
59 /**
60 * ( excerpt from http://php.net/manual/en/iteratoriterator.valid.php )
62 * Checks if the iterator is valid.
64 * @return mixed Returns TRUE if the iterator is valid, otherwise
65 * FALSE
67 public function valid() {
68 return $this->iterator->valid();
71 // This doc comment block generated by idl/sysdoc.php
72 /**
73 * ( excerpt from http://php.net/manual/en/iteratoriterator.key.php )
75 * Get the key of the current element.
77 * @return mixed The key of the current element.
79 public function key() {
80 return $this->key;
83 // This doc comment block generated by idl/sysdoc.php
84 /**
85 * ( excerpt from http://php.net/manual/en/iteratoriterator.current.php )
87 * Get the value of the current element.
89 * @return mixed The value of the current element.
91 public function current() {
92 return $this->current;
95 // This doc comment block generated by idl/sysdoc.php
96 /**
97 * ( excerpt from http://php.net/manual/en/iteratoriterator.next.php )
99 * Forward to the next element.
101 * @return mixed No value is returned.
103 public function next() {
104 $this->iterator->next();
105 $this->position++;
106 $this->_fetch(true);
107 return;
110 // This doc comment block generated by idl/sysdoc.php
112 * ( excerpt from http://php.net/manual/en/iteratoriterator.rewind.php )
114 * Rewinds to the first element.
116 * @return mixed No value is returned.
118 public function rewind() {
119 $this->iterator->rewind();
120 $this->position = 0;
121 $this->_fetch(true);
122 return;
125 public function __call($func, $params) {
126 return call_user_func_array(varray[$this->iterator, $func], $params);
130 * This function appears in the php source in spl_iterators.c as
131 * spl_dual_it_fetch. Apparently, all iterators that store other
132 * iterators are forced to do this layer of caching. If you call
133 * next(), these "dual" iterators will need to get the key and
134 * current value out of the underlying iterator and store it.
136 * Basically, if you see a call to spl_dual_it_fetch in the
137 * PHP source, it's very likely that you should call this.
139 protected function _fetch($check) {
140 if (!$check || $this->iterator->valid()) {
141 $this->current = $this->iterator->current();
142 $key = $this->iterator->key();
143 $this->key = is_null($key) ? $this->position : $key;
144 return true;
145 } else {
146 $this->current = null;
147 $this->key = null;
148 return false;
152 protected function _getPosition() {
153 return $this->position;
156 protected function _setPosition($position) {
157 $this->position = $position;