Hoa central
Sampler.php
Go to the documentation of this file.
1 <?php
2 
37 namespace Hoa\Praspel\Iterator;
38 
39 use Hoa\Iterator;
40 use Hoa\Praspel;
41 
50 class Sampler implements Iterator
51 {
58 
65 
71  protected $_declaration = null;
72 
78  protected $_keyType = null;
79 
85  protected $_variables = [];
86 
92  protected $_key = -1;
93 
99  protected $_current = null;
100 
106  protected $_coverage = null;
107 
108 
109 
120  public function __construct(
121  Praspel\Model\Declaration $declaration,
122  $keyType = self::KEY_AS_VARIABLE_NAME
123  ) {
124  $this->_declaration = $declaration;
125  $this->_keyType = $keyType;
126 
127  return;
128  }
129 
139  public function extract()
140  {
141  foreach (func_get_args() as $variable) {
142  $this->_variables[] = $this->_declaration[$variable];
143  }
144 
145  return $this;
146  }
147 
153  public function current()
154  {
155  return $this->_current;
156  }
157 
163  public function key()
164  {
165  return $this->_key;
166  }
167 
173  public function next()
174  {
175  $this->_coverage->next();
176  $this->_current();
177 
178  return $this->current();
179  }
180 
186  protected function _current()
187  {
188  $current = $this->_coverage->current();
189  $handle = [];
190 
191  if (self::KEY_AS_VARIABLE_NAME === $this->_keyType) {
192  foreach ($current as $name => $domain) {
193  $handle[$name] = $domain->sample();
194  }
195  } else {
196  foreach ($current as $domain) {
197  $handle[] = $domain->sample();
198  }
199  }
200 
201  ++$this->_key;
202  $this->_current = $handle;
203 
204  return;
205  }
206 
212  public function rewind()
213  {
214  $this->_key = -1;
215  $this->_current = null;
216 
217  if (null === $this->_coverage) {
218  if (empty($this->_variables)) {
219  $this->_variables = $this->_declaration->getLocalVariables();
220  }
221 
222  $this->_coverage = new Coverage\Domain($this->_variables);
223  }
224 
225  $this->_coverage->rewind();
226  $this->_current();
227 
228  return;
229  }
230 
236  public function valid()
237  {
238  return $this->_coverage->valid();
239  }
240 }
__construct(Praspel\Model\Declaration $declaration, $keyType=self::KEY_AS_VARIABLE_NAME)
Definition: Sampler.php:120