Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

#72 ICU workaround to support formatting currencies different than th… #73

Merged
merged 1 commit into from
May 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/View/Helper/CurrencyFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@ class CurrencyFormat extends AbstractHelper
*/
protected $showDecimals = true;

/**
* Special condition to be checked due to ICU bug:
* http://bugs.icu-project.org/trac/ticket/10997
*
* @var bool
*/
protected $correctionNeeded = false;



/**
* @throws Exception\ExtensionNotLoadedException if ext/intl is not present
*/
Expand Down Expand Up @@ -132,11 +142,16 @@ protected function formatCurrency(

if ($showDecimals) {
$this->formatters[$formatterId]->setAttribute(NumberFormatter::FRACTION_DIGITS, 2);
$this->correctionNeeded = false;
} else {
$this->formatters[$formatterId]->setAttribute(NumberFormatter::FRACTION_DIGITS, 0);
$defaultCurrencyCode = $this->formatters[$formatterId]->getTextAttribute(NumberFormatter::CURRENCY_CODE);
$this->correctionNeeded = $defaultCurrencyCode !== $currencyCode;
}

return $this->formatters[$formatterId]->formatCurrency($number, $currencyCode);
$formattedNumber = $this->formatters[$formatterId]->formatCurrency($number, $currencyCode);

return $this->correctionNeeded ? $this->correctICUBug($formattedNumber, $this->formatters[$formatterId]) : $formattedNumber;
}

/**
Expand Down Expand Up @@ -230,4 +245,18 @@ public function shouldShowDecimals()
{
return $this->showDecimals;
}



/**
* @param string $formattedNumber
* @param NumberFormatter $formatter
* @return string
*/
private function correctICUBug($formattedNumber, NumberFormatter $formatter)
{
$pattern = sprintf('/\%s\d+$/', $formatter->getSymbol(NumberFormatter::DECIMAL_SEPARATOR_SYMBOL));

return preg_replace($pattern, '', $formattedNumber);
}
}
2 changes: 2 additions & 0 deletions test/View/Helper/CurrencyFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function currencyProvider()
//array('en_US', 'EUR', false, 1234567.891234567890000, null, '€1,234,568'),
//array('en_US', 'EUR', false, 1234567.891234567890000, null, '€1,234,568'),
['en_US', 'USD', false, 1234567.891234567890000, null, '$1,234,568'],
/* @see http://bugs.icu-project.org/trac/ticket/10997 */
['en_US', 'EUR', false, 1234567.891234567890000, null, '€1,234,567'],
];
}

Expand Down