Iterator.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\MessageQueue\Publisher\Config\PublisherConfigItem;
  7. use Magento\Framework\MessageQueue\Publisher\Config\Data;
  8. use Magento\Framework\MessageQueue\Publisher\Config\PublisherConfigItem;
  9. use Magento\Framework\MessageQueue\Publisher\Config\PublisherConfigItemFactory;
  10. /**
  11. * Publisher config data iterator.
  12. */
  13. class Iterator implements \Iterator, \ArrayAccess
  14. {
  15. /**
  16. * Publisher config item.
  17. *
  18. * @var PublisherConfigItem
  19. */
  20. private $object;
  21. /**
  22. * Config data.
  23. *
  24. * @var array
  25. */
  26. private $data;
  27. /**
  28. * Initialize dependencies.
  29. *
  30. * @param Data $configData
  31. * @param PublisherConfigItemFactory $itemFactory
  32. */
  33. public function __construct(Data $configData, PublisherConfigItemFactory $itemFactory)
  34. {
  35. $this->data = $configData->get();
  36. $this->object = $itemFactory->create();
  37. $this->rewind();
  38. }
  39. /**
  40. * Get current item.
  41. *
  42. * @return PublisherConfigItem
  43. */
  44. public function current()
  45. {
  46. return $this->object;
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function next()
  52. {
  53. next($this->data);
  54. if (current($this->data)) {
  55. $this->initObject(current($this->data));
  56. if ($this->current()->isDisabled()) {
  57. $this->next();
  58. }
  59. }
  60. }
  61. /**
  62. * Initialize object.
  63. *
  64. * @param array $data
  65. * @return void
  66. */
  67. private function initObject(array $data)
  68. {
  69. $this->object->setData($data);
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function key()
  75. {
  76. key($this->data);
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function valid()
  82. {
  83. return (bool)current($this->data);
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function rewind()
  89. {
  90. reset($this->data);
  91. if (current($this->data)) {
  92. $this->initObject(current($this->data));
  93. if ($this->current()->isDisabled()) {
  94. $this->next();
  95. }
  96. }
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function offsetExists($offset)
  102. {
  103. return array_key_exists($offset, $this->data);
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function offsetGet($offset)
  109. {
  110. if (!$this->offsetExists($offset) || $this->data[$offset]['disabled'] == true) {
  111. return null;
  112. }
  113. $item = clone $this->object;
  114. $item->setData($this->data[$offset]);
  115. return $item;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function offsetSet($offset, $value)
  121. {
  122. $this->data[$offset] = $value;
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function offsetUnset($offset)
  128. {
  129. unset($this->data[$offset]);
  130. }
  131. }