Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractIterator.php
Go to the documentation of this file.
1 <?php
8 
14 abstract class AbstractIterator implements \Iterator, \Countable
15 {
21  protected $data = [];
22 
23  // @codingStandardsIgnoreStart
29  protected $current;
30 
36  abstract public function current();
37  // @codingStandardsIgnoreEnd
38 
44  protected $key;
45 
51  abstract protected function isValid();
52 
58  public function rewind()
59  {
60  reset($this->data);
61  if (!$this->isValid()) {
62  $this->next();
63  }
64  }
65 
71  public function next()
72  {
73  $this->current = next($this->data);
74 
75  if ($this->current !== false) {
76  if (!$this->isValid()) {
77  $this->next();
78  }
79  } else {
80  $this->key = null;
81  }
82  }
83 
89  public function valid()
90  {
91  $current = current($this->data);
92  if ($current === false || $current === null) {
93  return false;
94  } else {
95  return true;
96  }
97  }
98 
104  public function key()
105  {
106  return key($this->data);
107  }
108 
114  public function count()
115  {
116  return count($this->data);
117  }
118 
124  protected function initFirstElement()
125  {
126  if ($this->data) {
127  $this->current = reset($this->data);
128  if (!$this->isValid()) {
129  $this->next();
130  }
131  }
132  }
133 }