You are here

final class Price in Price 8

Same name in this branch
  1. 8 src/Price.php \Drupal\price\Price
  2. 8 src/Element/Price.php \Drupal\price\Element\Price
Same name and namespace in other branches
  1. 3.x src/Price.php \Drupal\price\Price
  2. 2.0.x src/Price.php \Drupal\price\Price
  3. 2.x src/Price.php \Drupal\price\Price
  4. 3.0.x src/Price.php \Drupal\price\Price

Provides a value object for monetary values.

Use the price.currency_formatter service to format prices.

Hierarchy

  • class \Drupal\price\Price

Expanded class hierarchy of Price

2 files declare their use of Price
PriceItem.php in src/Plugin/Field/FieldType/PriceItem.php
PriceTwigExtension.php in src/TwigExtension/PriceTwigExtension.php
2 string references to 'Price'
price.info.yml in ./price.info.yml
price.info.yml
price.schema.yml in config/schema/price.schema.yml
config/schema/price.schema.yml

File

src/Price.php, line 12

Namespace

Drupal\price
View source
final class Price {

  /**
   * The number.
   *
   * @var string
   */
  protected $number;

  /**
   * The currency code.
   *
   * @var string
   */
  protected $currencyCode;

  /**
   * Constructs a new Price object.
   *
   * @param string $number
   *   The number.
   * @param string $currency_code
   *   The currency code.
   */
  public function __construct($number, $currency_code) {
    Calculator::assertNumberFormat($number);
    $this
      ->assertCurrencyCodeFormat($currency_code);
    $this->number = (string) $number;
    $this->currencyCode = strtoupper($currency_code);
  }

  /**
   * Gets the number.
   *
   * @return string
   *   The number.
   */
  public function getNumber() {
    return $this->number;
  }

  /**
   * Gets the currency code.
   *
   * @return string
   *   The currency code.
   */
  public function getCurrencyCode() {
    return $this->currencyCode;
  }

  /**
   * Gets the string representation of the price.
   *
   * @return string
   *   The string representation of the price.
   */
  public function __toString() {
    return Calculator::trim($this->number) . ' ' . $this->currencyCode;
  }

  /**
   * Gets the array representation of the price.
   *
   * @return array
   *   The array representation of the price.
   */
  public function toArray() {
    return [
      'number' => $this->number,
      'currency_code' => $this->currencyCode,
    ];
  }

  /**
   * Converts the current price to the given currency.
   *
   * @param string $currency_code
   *   The currency code.
   * @param string $rate
   *   A currency rate corresponding to the currency code.
   *
   * @return static
   *   The resulting price.
   */
  public function convert($currency_code, $rate = '1') {
    $new_number = Calculator::multiply($this->number, $rate);
    return new static($new_number, $currency_code);
  }

  /**
   * Adds the given price to the current price.
   *
   * @param \Drupal\price\Price $price
   *   The price.
   *
   * @return static
   *   The resulting price.
   */
  public function add(Price $price) {
    $this
      ->assertSameCurrency($this, $price);
    $new_number = Calculator::add($this->number, $price
      ->getNumber());
    return new static($new_number, $this->currencyCode);
  }

  /**
   * Subtracts the given price from the current price.
   *
   * @param \Drupal\price\Price $price
   *   The price.
   *
   * @return static
   *   The resulting price.
   */
  public function subtract(Price $price) {
    $this
      ->assertSameCurrency($this, $price);
    $new_number = Calculator::subtract($this->number, $price
      ->getNumber());
    return new static($new_number, $this->currencyCode);
  }

  /**
   * Multiplies the current price by the given number.
   *
   * @param string $number
   *   The number.
   *
   * @return static
   *   The resulting price.
   */
  public function multiply($number) {
    $new_number = Calculator::multiply($this->number, $number);
    return new static($new_number, $this->currencyCode);
  }

  /**
   * Divides the current price by the given number.
   *
   * @param string $number
   *   The number.
   *
   * @return static
   *   The resulting price.
   */
  public function divide($number) {
    $new_number = Calculator::divide($this->number, $number);
    return new static($new_number, $this->currencyCode);
  }

  /**
   * Compares the current price with the given price.
   *
   * @param \Drupal\price\Price $price
   *   The price.
   *
   * @return int
   *   0 if both prices are equal, 1 if the first one is greater, -1 otherwise.
   */
  public function compareTo(Price $price) {
    $this
      ->assertSameCurrency($this, $price);
    return Calculator::compare($this->number, $price
      ->getNumber());
  }

  /**
   * Gets whether the current price is zero.
   *
   * @return bool
   *   TRUE if the price is zero, FALSE otherwise.
   */
  public function isZero() {
    return Calculator::compare($this->number, '0') == 0;
  }

  /**
   * Gets whether the current price is equivalent to the given price.
   *
   * @param \Drupal\price\Price $price
   *   The price.
   *
   * @return bool
   *   TRUE if the prices are equal, FALSE otherwise.
   */
  public function equals(Price $price) {
    return $this
      ->compareTo($price) == 0;
  }

  /**
   * Gets whether the current price is greater than the given price.
   *
   * @param \Drupal\price\Price $price
   *   The price.
   *
   * @return bool
   *   TRUE if the current price is greater than the given price,
   *   FALSE otherwise.
   */
  public function greaterThan(Price $price) {
    return $this
      ->compareTo($price) == 1;
  }

  /**
   * Gets whether the current price is greater than or equal to the given price.
   *
   * @param \Drupal\price\Price $price
   *   The price.
   *
   * @return bool
   *   TRUE if the current price is greater than or equal to the given price,
   *   FALSE otherwise.
   */
  public function greaterThanOrEqual(Price $price) {
    return $this
      ->greaterThan($price) || $this
      ->equals($price);
  }

  /**
   * Gets whether the current price is lesser than the given price.
   *
   * @param \Drupal\price\Price $price
   *   The price.
   *
   * @return bool
   *   TRUE if the current price is lesser than the given price,
   *   FALSE otherwise.
   */
  public function lessThan(Price $price) {
    return $this
      ->compareTo($price) == -1;
  }

  /**
   * Gets whether the current price is lesser than or equal to the given price.
   *
   * @param \Drupal\price\Price $price
   *   The price.
   *
   * @return bool
   *   TRUE if the current price is lesser than or equal to the given price,
   *   FALSE otherwise.
   */
  public function lessThanOrEqual(Price $price) {
    return $this
      ->lessThan($price) || $this
      ->equals($price);
  }

  /**
   * Asserts that the currency code is in the right format.
   *
   * Serves only as a basic sanity check.
   *
   * @param string $currency_code
   *   The currency code.
   *
   * @throws \InvalidArgumentException
   *   Thrown when the currency code is not in the right format.
   */
  protected function assertCurrencyCodeFormat($currency_code) {
    if (strlen($currency_code) != '3') {
      throw new \InvalidArgumentException();
    }
  }

  /**
   * Asserts that the two prices have the same currency.
   *
   * @param \Drupal\price\Price $first_price
   *   The first price.
   * @param \Drupal\price\Price $second_price
   *   The second price.
   *
   * @throws \Drupal\price\Exception\CurrencyMismatchException
   *   Thrown when the prices do not have the same currency.
   */
  protected function assertSameCurrency(Price $first_price, Price $second_price) {
    if ($first_price
      ->getCurrencyCode() != $second_price
      ->getCurrencyCode()) {
      throw new CurrencyMismatchException();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Price::$currencyCode protected property The currency code.
Price::$number protected property The number.
Price::add public function Adds the given price to the current price.
Price::assertCurrencyCodeFormat protected function Asserts that the currency code is in the right format.
Price::assertSameCurrency protected function Asserts that the two prices have the same currency.
Price::compareTo public function Compares the current price with the given price.
Price::convert public function Converts the current price to the given currency.
Price::divide public function Divides the current price by the given number.
Price::equals public function Gets whether the current price is equivalent to the given price.
Price::getCurrencyCode public function Gets the currency code.
Price::getNumber public function Gets the number.
Price::greaterThan public function Gets whether the current price is greater than the given price.
Price::greaterThanOrEqual public function Gets whether the current price is greater than or equal to the given price.
Price::isZero public function Gets whether the current price is zero.
Price::lessThan public function Gets whether the current price is lesser than the given price.
Price::lessThanOrEqual public function Gets whether the current price is lesser than or equal to the given price.
Price::multiply public function Multiplies the current price by the given number.
Price::subtract public function Subtracts the given price from the current price.
Price::toArray public function Gets the array representation of the price.
Price::__construct public function Constructs a new Price object.
Price::__toString public function Gets the string representation of the price.