123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Framework\Pricing\Price;
- use Magento\Framework\Pricing\Adjustment\CalculatorInterface;
- use Magento\Framework\Pricing\Amount\AmountInterface;
- use Magento\Framework\Pricing\SaleableInterface;
- use Magento\Framework\Pricing\PriceInfoInterface;
- /**
- * Class AbstractPrice
- * Should be the base for creating any Price type class
- *
- * @api
- * @since 100.0.2
- */
- abstract class AbstractPrice implements PriceInterface
- {
- /**
- * Default price type
- */
- const PRICE_CODE = 'abstract_price';
- /**
- * @var AmountInterface[]
- */
- protected $amount;
- /**
- * @var \Magento\Framework\Pricing\Adjustment\CalculatorInterface
- */
- protected $calculator;
- /**
- * @var SaleableInterface
- */
- protected $product;
- /**
- * @var string
- */
- protected $priceType;
- /**
- * @var float
- */
- protected $quantity;
- /**
- * @var PriceInfoInterface
- */
- protected $priceInfo;
- /**
- * @var bool|float
- */
- protected $value;
- /**
- * @var \Magento\Framework\Pricing\PriceCurrencyInterface
- */
- protected $priceCurrency;
- /**
- * @param SaleableInterface $saleableItem
- * @param float $quantity
- * @param CalculatorInterface $calculator
- * @param \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
- */
- public function __construct(
- SaleableInterface $saleableItem,
- $quantity,
- CalculatorInterface $calculator,
- \Magento\Framework\Pricing\PriceCurrencyInterface $priceCurrency
- ) {
- $this->product = $saleableItem;
- $this->quantity = $quantity;
- $this->calculator = $calculator;
- $this->priceCurrency = $priceCurrency;
- $this->priceInfo = $saleableItem->getPriceInfo();
- }
- /**
- * Get price value in display currency
- *
- * @return float|bool
- */
- abstract public function getValue();
- /**
- * Get Price Amount object
- *
- * @return AmountInterface
- */
- public function getAmount()
- {
- if (!isset($this->amount[$this->getValue()])) {
- $this->amount[$this->getValue()] = $this->calculator->getAmount($this->getValue(), $this->getProduct());
- }
- return $this->amount[$this->getValue()];
- }
- /**
- * @param float $amount
- * @param null|bool|string|array $exclude
- * @param null|array $context
- * @return AmountInterface|bool|float
- */
- public function getCustomAmount($amount = null, $exclude = null, $context = [])
- {
- if (null !== $amount) {
- $amount = $this->priceCurrency->convertAndRound($amount);
- } else {
- $amount = $this->getValue();
- }
- return $this->calculator->getAmount($amount, $this->getProduct(), $exclude, $context);
- }
- /**
- * Get price type code
- *
- * @return string
- */
- public function getPriceCode()
- {
- return static::PRICE_CODE;
- }
- /**
- * @return SaleableInterface
- */
- public function getProduct()
- {
- return $this->product;
- }
- /**
- * @return float
- */
- public function getQuantity()
- {
- return $this->quantity;
- }
- }
|