Collection.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. use Magento\Framework\Pricing\SaleableInterface;
  8. /**
  9. * Class Collection
  10. *
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Collection implements \Iterator
  15. {
  16. /**
  17. * @var Pool
  18. */
  19. protected $pool;
  20. /**
  21. * @var \Magento\Framework\Pricing\SaleableInterface
  22. */
  23. protected $saleableItem;
  24. /**
  25. * @var \Magento\Framework\Pricing\Price\Factory
  26. */
  27. protected $priceFactory;
  28. /**
  29. * @var float
  30. */
  31. protected $quantity;
  32. /**
  33. * @var array
  34. */
  35. protected $contains;
  36. /**
  37. * @var array
  38. */
  39. protected $excludes;
  40. /**
  41. * Cached price models
  42. *
  43. * @var array
  44. */
  45. protected $priceModels;
  46. /**
  47. * Constructor
  48. *
  49. * @param SaleableInterface $saleableItem
  50. * @param Factory $priceFactory
  51. * @param Pool $pool
  52. * @param float $quantity
  53. */
  54. public function __construct(
  55. SaleableInterface $saleableItem,
  56. Factory $priceFactory,
  57. Pool $pool,
  58. $quantity
  59. ) {
  60. $this->saleableItem = $saleableItem;
  61. $this->priceFactory = $priceFactory;
  62. $this->pool = $pool;
  63. $this->quantity = $quantity;
  64. $this->priceModels = [];
  65. }
  66. /**
  67. * Reset the Collection to the first element
  68. *
  69. * @return mixed|void
  70. */
  71. public function rewind()
  72. {
  73. return $this->pool->rewind();
  74. }
  75. /**
  76. * Return the current element
  77. *
  78. * @return \Magento\Framework\Pricing\Price\PriceInterface
  79. */
  80. public function current()
  81. {
  82. return $this->get($this->key());
  83. }
  84. /**
  85. * Return the key of the current element
  86. *
  87. * @return string
  88. */
  89. public function key()
  90. {
  91. return $this->pool->key();
  92. }
  93. /**
  94. * Move forward to next element
  95. *
  96. * @return mixed|void
  97. */
  98. public function next()
  99. {
  100. return $this->pool->next();
  101. }
  102. /**
  103. * Checks if current position is valid
  104. *
  105. * @return bool
  106. */
  107. public function valid()
  108. {
  109. return $this->pool->valid();
  110. }
  111. /**
  112. * Returns price model by code
  113. *
  114. * @param string $code
  115. * @return PriceInterface
  116. */
  117. public function get($code)
  118. {
  119. if (!isset($this->priceModels[$code])) {
  120. $this->priceModels[$code] = $this->priceFactory->create(
  121. $this->saleableItem,
  122. $this->pool[$code],
  123. $this->quantity
  124. );
  125. }
  126. return $this->priceModels[$code];
  127. }
  128. }