Base.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Pricing\Amount;
  7. use Magento\Framework\Pricing\Adjustment\AdjustmentInterface;
  8. /**
  9. * Amount base model
  10. */
  11. class Base implements AmountInterface
  12. {
  13. /**
  14. * @var float
  15. */
  16. protected $amount;
  17. /**
  18. * @var float
  19. */
  20. protected $baseAmount;
  21. /**
  22. * @var float
  23. */
  24. protected $totalAdjustmentAmount;
  25. /**
  26. * @var float[]
  27. */
  28. protected $adjustmentAmounts = [];
  29. /**
  30. * @var AdjustmentInterface[]
  31. */
  32. protected $adjustments = [];
  33. /**
  34. * @param float $amount
  35. * @param array $adjustmentAmounts
  36. */
  37. public function __construct(
  38. $amount,
  39. array $adjustmentAmounts = []
  40. ) {
  41. $this->amount = $amount;
  42. $this->adjustmentAmounts = $adjustmentAmounts;
  43. }
  44. /**
  45. * Return full amount value
  46. *
  47. * @param null|string|array $exclude
  48. * @return float
  49. */
  50. public function getValue($exclude = null)
  51. {
  52. if ($exclude === null) {
  53. return $this->amount;
  54. } else {
  55. if (!is_array($exclude)) {
  56. $exclude = [(string)$exclude];
  57. }
  58. $amount = $this->amount;
  59. foreach ($exclude as $code) {
  60. if ($this->hasAdjustment($code)) {
  61. $amount -= $this->adjustmentAmounts[$code];
  62. }
  63. }
  64. return $amount;
  65. }
  66. }
  67. /**
  68. * Return full amount value in string format
  69. *
  70. * @return string
  71. */
  72. public function __toString()
  73. {
  74. return (string) $this->getValue();
  75. }
  76. /**
  77. * Return base amount part value
  78. *
  79. * @return float|null
  80. */
  81. public function getBaseAmount()
  82. {
  83. if ($this->baseAmount === null) {
  84. $this->calculateAmounts();
  85. }
  86. return $this->baseAmount;
  87. }
  88. /**
  89. * Return adjustment amount part value by adjustment code
  90. *
  91. * @param string $adjustmentCode
  92. * @return bool|float
  93. */
  94. public function getAdjustmentAmount($adjustmentCode)
  95. {
  96. return $this->adjustmentAmounts[$adjustmentCode] ?? false;
  97. }
  98. /**
  99. * Return sum amount of all applied adjustments
  100. *
  101. * @return float|null
  102. */
  103. public function getTotalAdjustmentAmount()
  104. {
  105. if ($this->totalAdjustmentAmount === null) {
  106. $this->calculateAmounts();
  107. }
  108. return $this->totalAdjustmentAmount;
  109. }
  110. /**
  111. * Return all applied adjustments as array
  112. *
  113. * @return float[]
  114. */
  115. public function getAdjustmentAmounts()
  116. {
  117. return $this->adjustmentAmounts;
  118. }
  119. /**
  120. * Check if adjustment is contained in amount object
  121. *
  122. * @param string $adjustmentCode
  123. * @return bool
  124. */
  125. public function hasAdjustment($adjustmentCode)
  126. {
  127. return array_key_exists($adjustmentCode, $this->adjustmentAmounts);
  128. }
  129. /**
  130. * Calculate base amount
  131. *
  132. * @return void
  133. */
  134. protected function calculateAmounts()
  135. {
  136. $this->baseAmount = $this->amount;
  137. $this->totalAdjustmentAmount = 0.;
  138. foreach ($this->adjustmentAmounts as $amount) {
  139. $this->baseAmount -= $amount;
  140. $this->totalAdjustmentAmount += $amount;
  141. }
  142. }
  143. }