AbstractPrice.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\Adjustment\CalculatorInterface;
  8. use Magento\Framework\Pricing\Amount\AmountInterface;
  9. use Magento\Framework\Pricing\SaleableInterface;
  10. use Magento\Framework\Pricing\PriceInfoInterface;
  11. /**
  12. * Class AbstractPrice
  13. * Should be the base for creating any Price type class
  14. *
  15. * @api
  16. * @since 100.0.2
  17. */
  18. abstract class AbstractPrice implements PriceInterface
  19. {
  20. /**
  21. * Default price type
  22. */
  23. const PRICE_CODE = 'abstract_price';
  24. /**
  25. * @var AmountInterface[]
  26. */
  27. protected $amount;
  28. /**
  29. * @var \Magento\Framework\Pricing\Adjustment\CalculatorInterface
  30. */
  31. protected $calculator;
  32. /**
  33. * @var SaleableInterface
  34. */
  35. protected $product;
  36. /**
  37. * @var string
  38. */
  39. protected $priceType;
  40. /**
  41. * @var float
  42. */
  43. protected $quantity;
  44. /**
  45. * @var PriceInfoInterface
  46. */
  47. protected $priceInfo;
  48. /**
  49. * @var bool|float
  50. */
  51. protected $value;
  52. /**
  53. * @var \Magento\Framework\Pricing\PriceCurrencyInterface
  54. */
  55. protected $priceCurrency;
  56. /**
  57. * @param SaleableInterface $saleableItem
  58. * @param float $quantity
  59. * @param CalculatorInterface $calculator
  60. * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  61. */
  62. public function __construct(
  63. SaleableInterface $saleableItem,
  64. $quantity,
  65. CalculatorInterface $calculator,
  66. \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
  67. ) {
  68. $this->product = $saleableItem;
  69. $this->quantity = $quantity;
  70. $this->calculator = $calculator;
  71. $this->priceCurrency = $priceCurrency;
  72. $this->priceInfo = $saleableItem->getPriceInfo();
  73. }
  74. /**
  75. * Get price value in display currency
  76. *
  77. * @return float|bool
  78. */
  79. abstract public function getValue();
  80. /**
  81. * Get Price Amount object
  82. *
  83. * @return AmountInterface
  84. */
  85. public function getAmount()
  86. {
  87. if (!isset($this->amount[$this->getValue()])) {
  88. $this->amount[$this->getValue()] = $this->calculator->getAmount($this->getValue(), $this->getProduct());
  89. }
  90. return $this->amount[$this->getValue()];
  91. }
  92. /**
  93. * @param float $amount
  94. * @param null|bool|string|array $exclude
  95. * @param null|array $context
  96. * @return AmountInterface|bool|float
  97. */
  98. public function getCustomAmount($amount = null, $exclude = null, $context = [])
  99. {
  100. if (null !== $amount) {
  101. $amount = $this->priceCurrency->convertAndRound($amount);
  102. } else {
  103. $amount = $this->getValue();
  104. }
  105. return $this->calculator->getAmount($amount, $this->getProduct(), $exclude, $context);
  106. }
  107. /**
  108. * Get price type code
  109. *
  110. * @return string
  111. */
  112. public function getPriceCode()
  113. {
  114. return static::PRICE_CODE;
  115. }
  116. /**
  117. * @return SaleableInterface
  118. */
  119. public function getProduct()
  120. {
  121. return $this->product;
  122. }
  123. /**
  124. * @return float
  125. */
  126. public function getQuantity()
  127. {
  128. return $this->quantity;
  129. }
  130. }