Hoa central
Repeater.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Iterator;
38 
47 class Repeater implements Iterator
48 {
54  protected $_iterator = null;
55 
61  protected $_n = 1;
62 
68  protected $_i = 1;
69 
75  protected $_body = null;
76 
77 
78 
88  public function __construct(\Traversable $iterator, $n, $body = null)
89  {
90  if (0 >= $n) {
91  throw new Exception(
92  'n must be greater than 0, given %d.',
93  0,
94  $n
95  );
96  }
97 
98  if ($iterator instanceof \IteratorAggregate) {
99  $iterator = $iterator->getIterator();
100  }
101 
102  $this->_iterator = $iterator;
103  $this->_n = $n;
104  $this->_body = $body;
105 
106  return;
107  }
108 
114  public function current()
115  {
116  return $this->_iterator->current();
117  }
118 
124  public function key()
125  {
126  return $this->_iterator->key();
127  }
128 
134  public function next()
135  {
136  return $this->_iterator->next();
137  }
138 
144  public function rewind()
145  {
146  return $this->_iterator->rewind();
147  }
148 
154  public function valid()
155  {
156  $valid = $this->_iterator->valid();
157 
158  if (true === $valid) {
159  return true;
160  }
161 
162  if (null !== $this->_body) {
163  $handle = &$this->_body;
164  $handle($this->_i);
165  }
166 
167  $this->rewind();
168 
169  if ($this->_n <= $this->_i++) {
170  $this->_i = 1;
171 
172  return false;
173  }
174 
175  return true;
176  }
177 }
__construct(\Traversable $iterator, $n, $body=null)
Definition: Repeater.php:88