Intl.php in Currency 8.3
File
modules/currency_intl/src/Plugin/Currency/AmountFormatter/Intl.php
View source
<?php
namespace Drupal\currency_intl\Plugin\Currency\AmountFormatter;
use Commercie\Currency\CurrencyInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\currency\LocaleResolverInterface;
use Drupal\currency\Plugin\Currency\AmountFormatter\AmountFormatterInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Intl extends PluginBase implements AmountFormatterInterface, ContainerFactoryPluginInterface {
protected $localeDelegator;
public function __construct(array $configuration, $plugin_id, array $plugin_definition, LocaleResolverInterface $locale_delegator) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->localeDelegator = $locale_delegator;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('currency.locale_resolver'));
}
public function formatAmount(CurrencyInterface $currency, $amount, $language_type = LanguageInterface::TYPE_CONTENT) {
$currency_locale = $this->localeDelegator
->resolveCurrencyLocale();
$decimal_position = strpos($amount, '.');
$number_of_decimals = $decimal_position !== FALSE ? strlen(substr($amount, $decimal_position + 1)) : 0;
$formatter = new \NumberFormatter($currency_locale
->getLocale(), \NumberFormatter::PATTERN_DECIMAL, $currency_locale
->getPattern());
$formatter
->setAttribute(\NumberFormatter::FRACTION_DIGITS, $number_of_decimals);
$formatter
->setSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL, $currency_locale
->getDecimalSeparator());
$formatter
->setSymbol(\NumberFormatter::MONETARY_SEPARATOR_SYMBOL, $currency_locale
->getDecimalSeparator());
$formatter
->setSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL, $currency_locale
->getGroupingSeparator());
$formatter
->setSymbol(\NumberFormatter::MONETARY_GROUPING_SEPARATOR_SYMBOL, $currency_locale
->getGroupingSeparator());
$formatter
->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $currency
->getSign());
$formatter
->setSymbol(\NumberFormatter::INTL_CURRENCY_SYMBOL, $currency
->getCurrencyCode());
return $formatted = $formatter
->format($amount);
}
}
Classes
Name |
Description |
Intl |
Formats amounts using PHP's Intl extension. |