You are here

class NumberFormatter in Physical Fields 8

Default number formatter.

Uses the intl NumberFormatter class, if the intl PHP extension is enabled.

Commerce swaps out this class in order to use its own NumberFormatter which does not depend on the intl extension.

Hierarchy

Expanded class hierarchy of NumberFormatter

1 string reference to 'NumberFormatter'
physical.services.yml in ./physical.services.yml
physical.services.yml
1 service uses NumberFormatter
physical.number_formatter in ./physical.services.yml
Drupal\physical\NumberFormatter

File

src/NumberFormatter.php, line 15

Namespace

Drupal\physical
View source
class NumberFormatter implements NumberFormatterInterface {

  /**
   * The intl number formatter.
   *
   * @var \NumberFormatter
   */
  protected $numberFormatter;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Constructs a new NumberFormatter object.
   *
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   */
  public function __construct(LanguageManagerInterface $language_manager) {
    if (extension_loaded('intl')) {
      $language = $language_manager
        ->getConfigOverrideLanguage() ?: $language_manager
        ->getCurrentLanguage();
      $this->numberFormatter = new \NumberFormatter($language
        ->getId(), \NumberFormatter::DECIMAL);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function format($number, array $options = []) {
    $default_options = [
      'use_grouping' => TRUE,
      'minimum_fraction_digits' => 0,
      'maximum_fraction_digits' => 6,
    ];
    $options = array_replace($default_options, $options);
    if ($this->numberFormatter) {
      $this->numberFormatter
        ->setAttribute(\NumberFormatter::GROUPING_USED, $options['use_grouping']);
      $this->numberFormatter
        ->setAttribute(\NumberFormatter::MIN_FRACTION_DIGITS, $options['minimum_fraction_digits']);
      $this->numberFormatter
        ->setAttribute(\NumberFormatter::MAX_FRACTION_DIGITS, $options['maximum_fraction_digits']);
      $number = $this->numberFormatter
        ->format($number);
    }
    else {
      if ($options['minimum_fraction_digits'] == 0) {
        $number = Calculator::trim($number);
      }
    }
    return $number;
  }

  /**
   * {@inheritdoc}
   */
  public function parse($number) {
    if ($this->numberFormatter) {
      $number = $this->numberFormatter
        ->parse($number);

      // The returned number should be a string.
      if (is_numeric($number)) {
        $number = (string) $number;
      }
    }
    elseif (!is_numeric($number)) {

      // The intl extension is missing, validate the number at least.
      $number = FALSE;
    }
    return $number;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NumberFormatter::$languageManager protected property The language manager.
NumberFormatter::$numberFormatter protected property The intl number formatter.
NumberFormatter::format public function Formats the given number for the current language. Overrides NumberFormatterInterface::format
NumberFormatter::parse public function Parses the given number. Overrides NumberFormatterInterface::parse
NumberFormatter::__construct public function Constructs a new NumberFormatter object.