Iterator.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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\QueueConfigItem;
  7. use Magento\Framework\MessageQueue\Topology\Config\QueueConfigItem;
  8. use Magento\Framework\MessageQueue\Topology\Config\QueueConfigItemFactory;
  9. /**
  10. * Queue config item iterator.
  11. */
  12. class Iterator implements \Iterator, \ArrayAccess
  13. {
  14. /**
  15. * Consumer config item.
  16. *
  17. * @var QueueConfigItem
  18. */
  19. private $object;
  20. /**
  21. * Config data.
  22. *
  23. * @var array
  24. */
  25. private $data;
  26. /**
  27. * Initialize dependencies.
  28. *
  29. * @param DataMapper $configData
  30. * @param QueueConfigItemFactory $itemFactory
  31. */
  32. public function __construct(DataMapper $configData, QueueConfigItemFactory $itemFactory)
  33. {
  34. $this->data = $configData->getMappedData();
  35. $this->object = $itemFactory->create();
  36. $this->rewind();
  37. }
  38. /**
  39. * Get current item.
  40. *
  41. * @return QueueConfigItem
  42. */
  43. public function current()
  44. {
  45. return $this->object;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function next()
  51. {
  52. next($this->data);
  53. if (current($this->data)) {
  54. $this->initObject(current($this->data));
  55. }
  56. }
  57. /**
  58. * Initialize object.
  59. *
  60. * @param array $data
  61. * @return void
  62. */
  63. private function initObject(array $data)
  64. {
  65. $this->object->setData($data);
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function key()
  71. {
  72. key($this->data);
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function valid()
  78. {
  79. return (bool)current($this->data);
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function rewind()
  85. {
  86. reset($this->data);
  87. if (current($this->data)) {
  88. $this->initObject(current($this->data));
  89. }
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function offsetExists($offset)
  95. {
  96. return array_key_exists($offset, $this->data);
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function offsetGet($offset)
  102. {
  103. if (!$this->offsetExists($offset)) {
  104. return null;
  105. }
  106. $item = clone $this->object;
  107. $item->setData($this->data[$offset]);
  108. return $item;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function offsetSet($offset, $value)
  114. {
  115. $this->data[$offset] = $value;
  116. }
  117. /**
  118. * {@inheritdoc}
  119. */
  120. public function offsetUnset($offset)
  121. {
  122. unset($this->data[$offset]);
  123. }
  124. }