/app/code/Magento/ImportExport/Model/Import/AbstractSource.php

https://gitlab.com/crazybutterfly815/magento2 · PHP · 179 lines · 82 code · 15 blank · 82 comment · 20 complexity · 7caa3074067a0879a20be90eafcca19c MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ImportExport\Model\Import;
  7. use Magento\ImportExport\Model\Import\AbstractEntity;
  8. /**
  9. * Data source with columns for Magento_ImportExport
  10. */
  11. abstract class AbstractSource implements \SeekableIterator
  12. {
  13. /**
  14. * @var array
  15. */
  16. protected $_colNames = [];
  17. /**
  18. * Quantity of columns
  19. *
  20. * @var int
  21. */
  22. protected $_colQty;
  23. /**
  24. * Current row
  25. *
  26. * @var array
  27. */
  28. protected $_row = [];
  29. /**
  30. * Current row number
  31. *
  32. * -1 means "out of bounds"
  33. *
  34. * @var int
  35. */
  36. protected $_key = -1;
  37. /**
  38. * @var bool
  39. */
  40. protected $_foundWrongQuoteFlag = false;
  41. /**
  42. * Get and validate column names
  43. *
  44. * @param array $colNames
  45. * @throws \InvalidArgumentException
  46. */
  47. public function __construct(array $colNames)
  48. {
  49. if (empty($colNames)) {
  50. throw new \InvalidArgumentException('Empty column names');
  51. }
  52. if (count(array_unique($colNames)) != count($colNames)) {
  53. throw new \InvalidArgumentException('Duplicates found in column names: ' . var_export($colNames, 1));
  54. }
  55. $this->_colNames = $colNames;
  56. $this->_colQty = count($colNames);
  57. }
  58. /**
  59. * Column names getter.
  60. *
  61. * @return array
  62. */
  63. public function getColNames()
  64. {
  65. return $this->_colNames;
  66. }
  67. /**
  68. * Return the current element
  69. *
  70. * Returns the row in associative array format: array(<col_name> => <value>, ...)
  71. *
  72. * @return array
  73. */
  74. public function current()
  75. {
  76. $row = $this->_row;
  77. if (count($row) != $this->_colQty) {
  78. if ($this->_foundWrongQuoteFlag) {
  79. throw new \InvalidArgumentException(AbstractEntity::ERROR_CODE_WRONG_QUOTES);
  80. } else {
  81. throw new \InvalidArgumentException(AbstractEntity::ERROR_CODE_COLUMNS_NUMBER);
  82. }
  83. }
  84. return array_combine($this->_colNames, $row);
  85. }
  86. /**
  87. * Move forward to next element (\Iterator interface)
  88. *
  89. * @return void
  90. */
  91. public function next()
  92. {
  93. $this->_key++;
  94. $row = $this->_getNextRow();
  95. if (false === $row || [] === $row) {
  96. $this->_row = [];
  97. $this->_key = -1;
  98. } else {
  99. $this->_row = $row;
  100. }
  101. }
  102. /**
  103. * Render next row
  104. *
  105. * Return array or false on error
  106. *
  107. * @return array|false
  108. */
  109. abstract protected function _getNextRow();
  110. /**
  111. * Return the key of the current element (\Iterator interface)
  112. *
  113. * @return int -1 if out of bounds, 0 or more otherwise
  114. */
  115. public function key()
  116. {
  117. return $this->_key;
  118. }
  119. /**
  120. * Checks if current position is valid (\Iterator interface)
  121. *
  122. * @return bool
  123. */
  124. public function valid()
  125. {
  126. return -1 !== $this->_key;
  127. }
  128. /**
  129. * Rewind the \Iterator to the first element (\Iterator interface)
  130. *
  131. * @return void
  132. */
  133. public function rewind()
  134. {
  135. $this->_key = -1;
  136. $this->_row = [];
  137. $this->next();
  138. }
  139. /**
  140. * Seeks to a position (Seekable interface)
  141. *
  142. * @param int $position The position to seek to 0 or more
  143. * @return void
  144. * @throws \OutOfBoundsException
  145. */
  146. public function seek($position)
  147. {
  148. if ($position == $this->_key) {
  149. return;
  150. }
  151. if (0 == $position || $position < $this->_key) {
  152. $this->rewind();
  153. }
  154. if ($position > 0) {
  155. do {
  156. $this->next();
  157. if ($this->_key == $position) {
  158. return;
  159. }
  160. } while ($this->_key != -1);
  161. }
  162. throw new \OutOfBoundsException('Please correct the seek position.');
  163. }
  164. }