AbstractAdjustment.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Pricing\Render;
  7. use Magento\Framework\Pricing\SaleableInterface;
  8. use Magento\Framework\Pricing\Price\PriceInterface;
  9. use Magento\Framework\Pricing\PriceCurrencyInterface;
  10. use Magento\Framework\View\Element\Template;
  11. /**
  12. * Adjustment render abstract
  13. *
  14. * @method string getZone()
  15. */
  16. abstract class AbstractAdjustment extends Template implements AdjustmentRenderInterface
  17. {
  18. /**
  19. * @var PriceCurrencyInterface
  20. */
  21. protected $priceCurrency;
  22. /**
  23. * @var AmountRenderInterface
  24. */
  25. protected $amountRender;
  26. /**
  27. * @param Template\Context $context
  28. * @param PriceCurrencyInterface $priceCurrency
  29. * @param array $data
  30. */
  31. public function __construct(
  32. Template\Context $context,
  33. PriceCurrencyInterface $priceCurrency,
  34. array $data = []
  35. ) {
  36. $this->priceCurrency = $priceCurrency;
  37. parent::__construct($context, $data);
  38. }
  39. /**
  40. * @param AmountRenderInterface $amountRender
  41. * @param array $arguments
  42. * @return string
  43. */
  44. public function render(AmountRenderInterface $amountRender, array $arguments = [])
  45. {
  46. $this->amountRender = $amountRender;
  47. $origArguments = $this->getData();
  48. $this->setData(array_replace($origArguments, $arguments));
  49. $html = $this->apply();
  50. // restore original block arguments
  51. $this->setData($origArguments);
  52. return $html;
  53. }
  54. /**
  55. * @return AmountRenderInterface
  56. */
  57. public function getAmountRender()
  58. {
  59. return $this->amountRender;
  60. }
  61. /**
  62. * @param string $priceCode
  63. * @return PriceInterface
  64. */
  65. public function getPriceType($priceCode)
  66. {
  67. return $this->getSaleableItem()->getPriceInfo()->getPrice($priceCode);
  68. }
  69. /**
  70. * @return \Magento\Framework\Pricing\Price\PriceInterface
  71. */
  72. public function getPrice()
  73. {
  74. return $this->amountRender->getPrice();
  75. }
  76. /**
  77. * @return SaleableInterface
  78. */
  79. public function getSaleableItem()
  80. {
  81. return $this->amountRender->getSaleableItem();
  82. }
  83. /**
  84. * Format price value
  85. *
  86. * @param float $amount
  87. * @param bool $includeContainer
  88. * @param int $precision
  89. * @return string
  90. */
  91. public function formatCurrency(
  92. $amount,
  93. $includeContainer = true,
  94. $precision = PriceCurrencyInterface::DEFAULT_PRECISION
  95. ) {
  96. return $this->priceCurrency->format($amount, $includeContainer, $precision);
  97. }
  98. /**
  99. * Convert and format price value
  100. *
  101. * @param float $amount
  102. * @param bool $includeContainer
  103. * @param int $precision
  104. * @return string
  105. */
  106. public function convertAndFormatCurrency(
  107. $amount,
  108. $includeContainer = true,
  109. $precision = PriceCurrencyInterface::DEFAULT_PRECISION
  110. ) {
  111. return $this->priceCurrency->convertAndFormat($amount, $includeContainer, $precision);
  112. }
  113. /**
  114. * @return \Magento\Framework\Pricing\Adjustment\AdjustmentInterface
  115. */
  116. public function getAdjustment()
  117. {
  118. return $this->getSaleableItem()->getPriceInfo()->getAdjustment($this->getAdjustmentCode());
  119. }
  120. /**
  121. * @return string
  122. */
  123. abstract protected function apply();
  124. }