Iterator.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\MessageQueue\Topology\Config\ExchangeConfigItem;
  7. use Magento\Framework\MessageQueue\Topology\Config\Data;
  8. use Magento\Framework\MessageQueue\Topology\Config\ExchangeConfigItem;
  9. use Magento\Framework\MessageQueue\Topology\Config\ExchangeConfigItemFactory;
  10. /**
  11. * Exchange config data iterator.
  12. */
  13. class Iterator implements \Iterator, \ArrayAccess
  14. {
  15. /**
  16. * Exchange config.
  17. *
  18. * @var ExchangeConfigItem
  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 ExchangeConfigItemFactory $itemFactory
  32. */
  33. public function __construct(Data $configData, ExchangeConfigItemFactory $itemFactory)
  34. {
  35. $this->data = $configData->get();
  36. $this->object = $itemFactory->create();
  37. $this->rewind();
  38. }
  39. /**
  40. * Get current item.
  41. *
  42. * @return ExchangeConfigItem
  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. }
  57. }
  58. /**
  59. * Initialize object.
  60. *
  61. * @param array $data
  62. * @return void
  63. */
  64. private function initObject(array $data)
  65. {
  66. $this->object->setData($data);
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. public function key()
  72. {
  73. key($this->data);
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function valid()
  79. {
  80. return (bool)current($this->data);
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function rewind()
  86. {
  87. reset($this->data);
  88. if (current($this->data)) {
  89. $this->initObject(current($this->data));
  90. }
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function offsetExists($offset)
  96. {
  97. return array_key_exists($offset, $this->data);
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function offsetGet($offset)
  103. {
  104. if (!$this->offsetExists($offset)) {
  105. return null;
  106. }
  107. $item = clone $this->object;
  108. $item->setData($this->data[$offset]);
  109. return $item;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function offsetSet($offset, $value)
  115. {
  116. $this->data[$offset] = $value;
  117. }
  118. /**
  119. * {@inheritdoc}
  120. */
  121. public function offsetUnset($offset)
  122. {
  123. unset($this->data[$offset]);
  124. }
  125. }