Skip to content

Commit 4f207ec

Browse files
committed
MCP-288: [Load Cart Section] Replace Zend_Currency component with Intl NumberFormatter
- Make backward compatible solution;
1 parent 2925942 commit 4f207ec

File tree

1 file changed

+61
-6
lines changed

1 file changed

+61
-6
lines changed

app/code/Magento/Directory/Model/Currency.php

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ class Currency extends \Magento\Framework\Model\AbstractModel
8383
*/
8484
private $numberFormatterFactory;
8585

86+
/**
87+
* @var \Magento\Framework\NumberFormatter
88+
*/
89+
private $numberFormatter;
90+
8691
/**
8792
* @param \Magento\Framework\Model\Context $context
8893
* @param \Magento\Framework\Registry $registry
@@ -345,31 +350,81 @@ public function formatTxt($price, $options = [])
345350
*/
346351
$price = sprintf("%F", $price);
347352

353+
if ($this->useNumberFormatter($options)) {
354+
return $this->formatCurrency($price, $options);
355+
}
356+
357+
return $this->_localeCurrency->getCurrency($this->getCode())->toCurrency($price, $options);
358+
}
359+
360+
/**
361+
* Check if to use Intl.NumberFormatter to format currency.
362+
*
363+
* @param array $options
364+
* @return bool
365+
*/
366+
private function useNumberFormatter(array $options): bool
367+
{
368+
return (empty($options) || array_key_exists(LocaleCurrency::CURRENCY_OPTION_SYMBOL, $options)
369+
|| (array_key_exists(LocaleCurrency::CURRENCY_OPTION_DISPLAY, $options)
370+
&& ($options[LocaleCurrency::CURRENCY_OPTION_DISPLAY] === \Magento\Framework\Currency::NO_SYMBOL
371+
|| $options[LocaleCurrency::CURRENCY_OPTION_DISPLAY] === \Magento\Framework\Currency::USE_SYMBOL))
372+
|| array_key_exists('precision', $options));
373+
}
374+
375+
/**
376+
* Format currency.
377+
*
378+
* @param string $price
379+
* @param array $options
380+
* @return string
381+
*/
382+
private function formatCurrency(string $price, array $options): string
383+
{
348384
$customerOptions = new \Magento\Framework\DataObject([]);
385+
349386
$this->_eventManager->dispatch(
350387
'currency_display_options_forming',
351388
['currency_options' => $customerOptions, 'base_code' => $this->getCode()]
352389
);
353390
$options = array_merge($options, $customerOptions->toArray());
354391

355-
$numberFormatter = $this->numberFormatterFactory->create(
392+
$this->numberFormatter = $this->numberFormatterFactory->create(
356393
['locale' => $this->localeResolver->getLocale(), 'style' => \NumberFormatter::CURRENCY]
357394
);
358395

396+
$this->setOptions($options);
397+
398+
$formattedCurrency = $this->numberFormatter->formatCurrency($price, $this->getCode());
399+
400+
if (array_key_exists(LocaleCurrency::CURRENCY_OPTION_DISPLAY, $options)
401+
&& $options[LocaleCurrency::CURRENCY_OPTION_DISPLAY] === \Magento\Framework\Currency::NO_SYMBOL) {
402+
$formattedCurrency = str_replace(' ', '', $formattedCurrency);
403+
}
404+
405+
return $formattedCurrency;
406+
}
407+
408+
/**
409+
* Set number formatter custom options.
410+
*
411+
* @param array $options
412+
* @return void
413+
*/
414+
private function setOptions(array $options): void
415+
{
359416
if (array_key_exists(LocaleCurrency::CURRENCY_OPTION_DISPLAY, $options)
360417
&& $options[LocaleCurrency::CURRENCY_OPTION_DISPLAY] === \Magento\Framework\Currency::NO_SYMBOL) {
361-
$numberFormatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, '');
418+
$this->numberFormatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, '');
362419
}
363420
if (array_key_exists('precision', $options)) {
364-
$numberFormatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $options['precision']);
421+
$this->numberFormatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $options['precision']);
365422
}
366423
if (array_key_exists(LocaleCurrency::CURRENCY_OPTION_SYMBOL, $options)) {
367-
$numberFormatter->setSymbol(
424+
$this->numberFormatter->setSymbol(
368425
\NumberFormatter::CURRENCY_SYMBOL, $options[LocaleCurrency::CURRENCY_OPTION_SYMBOL]
369426
);
370427
}
371-
372-
return $numberFormatter->formatCurrency($price, $this->getCode());
373428
}
374429

375430
/**

0 commit comments

Comments
 (0)