Hoa central
Counter.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Iterator;
38 
47 class Counter implements Iterator
48 {
54  protected $_from = 0;
55 
61  protected $_key = 0;
62 
68  protected $_i = 0;
69 
75  protected $_to = 0;
76 
82  protected $_step = 0;
83 
84 
85 
97  public function __construct($from, $to, $step)
98  {
99  if ($step <= 0) {
100  throw new Exception(
101  'The step must be non-negative; given %d.',
102  0,
103  $step
104  );
105  }
106 
107  $this->_from = $from;
108  $this->_to = $to;
109  $this->_step = $step;
110 
111  return;
112  }
113 
119  public function current()
120  {
121  return $this->_i;
122  }
123 
129  public function key()
130  {
131  return $this->_key;
132  }
133 
139  public function next()
140  {
141  ++$this->_key;
142  $this->_i += $this->_step;
143 
144  return;
145  }
146 
152  public function rewind()
153  {
154  $this->_key = 0;
155  $this->_i = $this->_from;
156 
157  return;
158  }
159 
165  public function valid()
166  {
167  return $this->_i < $this->_to;
168  }
169 }
__construct($from, $to, $step)
Definition: Counter.php:97