Pool.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Pricing\Price;
  7. /**
  8. * Class Pool
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Pool implements \Iterator, \ArrayAccess
  14. {
  15. /**
  16. * @var \Magento\Framework\Pricing\Price\PriceInterface[]
  17. */
  18. protected $prices;
  19. /**
  20. * @param array $prices
  21. * @param \Iterator $target
  22. */
  23. public function __construct(
  24. array $prices,
  25. \Iterator $target = null
  26. ) {
  27. $this->prices = $prices;
  28. foreach ($target ?: [] as $code => $class) {
  29. if (empty($this->prices[$code])) {
  30. $this->prices[$code] = $class;
  31. }
  32. }
  33. }
  34. /**
  35. * Reset the Collection to the first element
  36. *
  37. * @return mixed
  38. */
  39. public function rewind()
  40. {
  41. return reset($this->prices);
  42. }
  43. /**
  44. * Return the current element
  45. *
  46. * @return mixed
  47. */
  48. public function current()
  49. {
  50. return current($this->prices);
  51. }
  52. /**
  53. * Return the key of the current element
  54. *
  55. * @return string
  56. */
  57. public function key()
  58. {
  59. return key($this->prices);
  60. }
  61. /**
  62. * Move forward to next element
  63. *
  64. * @return mixed
  65. */
  66. public function next()
  67. {
  68. return next($this->prices);
  69. }
  70. /**
  71. * Checks if current position is valid
  72. *
  73. * @return bool
  74. */
  75. public function valid()
  76. {
  77. return (bool)$this->key();
  78. }
  79. /**
  80. * Returns price class by code
  81. *
  82. * @param string $code
  83. * @return string
  84. */
  85. public function get($code)
  86. {
  87. return $this->prices[$code];
  88. }
  89. /**
  90. * The value to set.
  91. *
  92. * @param string $offset
  93. * @param string $value
  94. * @return void
  95. */
  96. public function offsetSet($offset, $value)
  97. {
  98. if ($offset === null) {
  99. $this->prices[] = $value;
  100. } else {
  101. $this->prices[$offset] = $value;
  102. }
  103. }
  104. /**
  105. * The return value will be casted to boolean if non-boolean was returned.
  106. *
  107. * @param string $offset
  108. * @return bool
  109. */
  110. public function offsetExists($offset)
  111. {
  112. return isset($this->prices[$offset]);
  113. }
  114. /**
  115. * The offset to unset.
  116. *
  117. * @param string $offset
  118. * @return void
  119. */
  120. public function offsetUnset($offset)
  121. {
  122. unset($this->prices[$offset]);
  123. }
  124. /**
  125. * The offset to retrieve.
  126. *
  127. * @param string $offset
  128. * @return string
  129. */
  130. public function offsetGet($offset)
  131. {
  132. return $this->prices[$offset] ?? null;
  133. }
  134. }