You are here

class LegacyNumberFormatter in Price 8

Same name and namespace in other branches
  1. 3.x src/LegacyNumberFormatter.php \Drupal\price\LegacyNumberFormatter
  2. 3.0.x src/LegacyNumberFormatter.php \Drupal\price\LegacyNumberFormatter

Provides a legacy number formatter for the deprecated NumberFormatterFactory.

Wraps the new CurrencyFormatter for compatibility with old code.

Hierarchy

Expanded class hierarchy of LegacyNumberFormatter

File

src/LegacyNumberFormatter.php, line 13

Namespace

Drupal\price
View source
class LegacyNumberFormatter {

  /**
   * The currency formatter.
   *
   * @var \CommerceGuys\Intl\Formatter\CurrencyFormatterInterface
   */
  protected $currencyFormatter;

  /**
   * The formatting options.
   *
   * @var array
   */
  protected $options = [];

  /**
   * Constructs a new LegacyNumberFormatter object.
   *
   * @param \CommerceGuys\Intl\Formatter\CurrencyFormatterInterface $currency_formatter
   *   The currency formatter.
   */
  public function __construct(CurrencyFormatterInterface $currency_formatter) {
    $this->currencyFormatter = $currency_formatter;
  }

  /**
   * Formats a number.
   *
   * @param string $number
   *   The number.
   *
   * @return string
   *   The formatted number
   */
  public function format($number) {
    $options = [
      'currency_display' => 'none',
    ] + $this->options;
    return $this->currencyFormatter
      ->format($number, 'XXX', $options);
  }

  /**
   * Formats a currency amount.
   *
   * @param string $number
   *   The number.
   * @param \Drupal\price\Entity\CurrencyInterface $currency
   *   The currency.
   *
   * @return string
   *   The formatted currency amount.
   */
  public function formatCurrency($number, CurrencyInterface $currency) {
    return $this->currencyFormatter
      ->format($number, $currency
      ->id(), $this->options);
  }

  /**
   * Parses a formatted number.
   *
   * @param string $number
   *   The formatted number.
   *
   * @return string|false
   *   The parsed number, or FALSE on failure.
   */
  public function parse($number) {
    return $this->currencyFormatter
      ->parse($number, 'XXX');
  }

  /**
   * Parses a formatted currency amount.
   *
   * @param string $number
   *   The formatted number.
   * @param \Drupal\price\Entity\CurrencyInterface $currency
   *   The currency.
   *
   * @return string|false
   *   The parsed currency amount, or FALSE on failure.
   */
  public function parseCurrency($number, CurrencyInterface $currency) {
    return $this->currencyFormatter
      ->parse($number, $currency
      ->id());
  }

  /**
   * Sets the minimum number of fraction digits.
   *
   * @param int $minimum_fraction_digits
   *   The minimum number of fraction digits.
   *
   * @return $this
   */
  public function setMinimumFractionDigits($minimum_fraction_digits) {
    $this->options['minimum_fraction_digits'] = $minimum_fraction_digits;
    return $this;
  }

  /**
   * Sets the maximum number of fraction digits.
   *
   * @param int $maximum_fraction_digits
   *   The maximum number of fraction digits.
   *
   * @return $this
   */
  public function setMaximumFractionDigits($maximum_fraction_digits) {
    $this->options['maximum_fraction_digits'] = $maximum_fraction_digits;
    return $this;
  }

  /**
   * Sets whether grouping is used.
   *
   * @param bool $grouping_used
   *   Whether grouping is used.
   *
   * @return $this
   */
  public function setGroupingUsed($grouping_used) {
    $this->options['use_grouping'] = $grouping_used;
    return $this;
  }

  /**
   * Sets the currency display.
   *
   * Allowed values: 'symbol', 'code', 'none'.
   *
   * @param string $currency_display
   *   The currency display.
   *
   * @return $this
   */
  public function setCurrencyDisplay($currency_display) {
    $this->options['currency_display'] = $currency_display;
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LegacyNumberFormatter::$currencyFormatter protected property The currency formatter.
LegacyNumberFormatter::$options protected property The formatting options.
LegacyNumberFormatter::format public function Formats a number.
LegacyNumberFormatter::formatCurrency public function Formats a currency amount.
LegacyNumberFormatter::parse public function Parses a formatted number.
LegacyNumberFormatter::parseCurrency public function Parses a formatted currency amount.
LegacyNumberFormatter::setCurrencyDisplay public function Sets the currency display.
LegacyNumberFormatter::setGroupingUsed public function Sets whether grouping is used.
LegacyNumberFormatter::setMaximumFractionDigits public function Sets the maximum number of fraction digits.
LegacyNumberFormatter::setMinimumFractionDigits public function Sets the minimum number of fraction digits.
LegacyNumberFormatter::__construct public function Constructs a new LegacyNumberFormatter object.