Iterator.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\Config\Structure\Element;
  7. /**
  8. * @api
  9. * @since 100.0.2
  10. */
  11. class Iterator implements \Iterator
  12. {
  13. /**
  14. * List of element data
  15. *
  16. * @var \Magento\Config\Model\Config\StructureElementInterface[]
  17. */
  18. protected $_elements;
  19. /**
  20. * Config structure element flyweight
  21. *
  22. * @var \Magento\Config\Model\Config\Structure\AbstractElement
  23. */
  24. protected $_flyweight;
  25. /**
  26. * Configuration scope
  27. *
  28. * @var string
  29. */
  30. protected $_scope;
  31. /**
  32. * Last element id
  33. *
  34. * @var string
  35. */
  36. protected $_lastId;
  37. /**
  38. * @param \Magento\Config\Model\Config\Structure\AbstractElement $element
  39. */
  40. public function __construct(\Magento\Config\Model\Config\Structure\AbstractElement $element)
  41. {
  42. $this->_flyweight = $element;
  43. }
  44. /**
  45. * Set element data
  46. *
  47. * @param array $elements
  48. * @param string $scope
  49. * @return void
  50. */
  51. public function setElements(array $elements, $scope)
  52. {
  53. $this->_elements = $elements;
  54. $this->_scope = $scope;
  55. if (count($elements)) {
  56. $lastElement = end($elements);
  57. $this->_lastId = $lastElement['id'];
  58. }
  59. }
  60. /**
  61. * Return the current element
  62. *
  63. * @return \Magento\Config\Model\Config\StructureElementInterface
  64. */
  65. public function current()
  66. {
  67. return $this->_flyweight;
  68. }
  69. /**
  70. * Move forward to next element
  71. *
  72. * @return void Any returned value is ignored.
  73. */
  74. public function next()
  75. {
  76. next($this->_elements);
  77. if (current($this->_elements)) {
  78. $this->_initFlyweight(current($this->_elements));
  79. if (!$this->current()->isVisible()) {
  80. $this->next();
  81. }
  82. }
  83. }
  84. /**
  85. * Initialize current flyweight
  86. *
  87. * @param array $element
  88. * @return void
  89. */
  90. protected function _initFlyweight(array $element)
  91. {
  92. $this->_flyweight->setData($element, $this->_scope);
  93. }
  94. /**
  95. * Return the key of the current element
  96. *
  97. * @return void
  98. */
  99. public function key()
  100. {
  101. key($this->_elements);
  102. }
  103. /**
  104. * Checks if current position is valid
  105. *
  106. * @return bool The return value will be casted to boolean and then evaluated.
  107. * Returns true on success or false on failure.
  108. */
  109. public function valid()
  110. {
  111. return (bool)current($this->_elements);
  112. }
  113. /**
  114. * Rewind the \Iterator to the first element
  115. *
  116. * @return void Any returned value is ignored.
  117. */
  118. public function rewind()
  119. {
  120. reset($this->_elements);
  121. if (current($this->_elements)) {
  122. $this->_initFlyweight(current($this->_elements));
  123. if (!$this->current()->isVisible()) {
  124. $this->next();
  125. }
  126. }
  127. }
  128. /**
  129. * Check whether element is last in list
  130. *
  131. * @param \Magento\Config\Model\Config\Structure\ElementInterface $element
  132. * @return bool
  133. */
  134. public function isLast(\Magento\Config\Model\Config\Structure\ElementInterface $element)
  135. {
  136. return $element->getId() == $this->_lastId;
  137. }
  138. }