Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractSource.php
Go to the documentation of this file.
1 <?php
7 
9 
16 abstract class AbstractSource implements \SeekableIterator
17 {
21  protected $_colNames = [];
22 
28  protected $_colQty;
29 
35  protected $_row = [];
36 
44  protected $_key = -1;
45 
49  protected $_foundWrongQuoteFlag = false;
50 
57  public function __construct(array $colNames)
58  {
59  if (empty($colNames)) {
60  throw new \InvalidArgumentException('Empty column names');
61  }
62  if (count(array_unique($colNames)) != count($colNames)) {
63  throw new \InvalidArgumentException('Duplicates found in column names: ' . var_export($colNames, 1));
64  }
65  $this->_colNames = $colNames;
66  $this->_colQty = count($colNames);
67  }
68 
74  public function getColNames()
75  {
76  return $this->_colNames;
77  }
78 
86  public function current()
87  {
88  $row = $this->_row;
89  if (count($row) != $this->_colQty) {
90  if ($this->_foundWrongQuoteFlag) {
91  throw new \InvalidArgumentException(AbstractEntity::ERROR_CODE_WRONG_QUOTES);
92  } else {
93  throw new \InvalidArgumentException(AbstractEntity::ERROR_CODE_COLUMNS_NUMBER);
94  }
95  }
96  return array_combine($this->_colNames, $row);
97  }
98 
104  public function next()
105  {
106  $this->_key++;
107  $row = $this->_getNextRow();
108  if (false === $row || [] === $row) {
109  $this->_row = [];
110  $this->_key = -1;
111  } else {
112  $this->_row = $row;
113  }
114  }
115 
123  abstract protected function _getNextRow();
124 
130  public function key()
131  {
132  return $this->_key;
133  }
134 
140  public function valid()
141  {
142  return -1 !== $this->_key;
143  }
144 
150  public function rewind()
151  {
152  $this->_key = -1;
153  $this->_row = [];
154  $this->next();
155  }
156 
164  public function seek($position)
165  {
166  if ($position == $this->_key) {
167  return;
168  }
169  if (0 == $position || $position < $this->_key) {
170  $this->rewind();
171  }
172  if ($position > 0) {
173  do {
174  $this->next();
175  if ($this->_key == $position) {
176  return;
177  }
178  } while ($this->_key != -1);
179  }
180  throw new \OutOfBoundsException('Please correct the seek position.');
181  }
182 }