Skip to content

Commit

Permalink
Remove the need for FormatterTrait::getCanonicalSymbols() by making p…
Browse files Browse the repository at this point in the history
…arseNumber() smarter.
  • Loading branch information
bojanz committed Oct 4, 2020
1 parent e7c1a45 commit 7ce8ba0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 50 deletions.
19 changes: 0 additions & 19 deletions src/Formatter/CurrencyFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,23 +252,4 @@ protected function getLocalizedSymbols(NumberFormat $numberFormat): array
'%' => $numberFormat->getPercentSign(),
];
}

/**
* {@inheritdoc}
*/
protected function getCanonicalSymbols(NumberFormat $numberFormat): array
{
return [
$numberFormat->getGroupingCurrencySeparator() => '',
// Convert the localized symbols back to their original form.
$numberFormat->getDecimalCurrencySeparator() => '.',
$numberFormat->getPlusSign() => '+',
$numberFormat->getMinusSign() => '-',
$numberFormat->getPercentSign() => '%',

// Strip whitespace (spaces and non-breaking spaces).
' ' => '',
chr(0xC2) . chr(0xA0) => '',
];
}
}
25 changes: 13 additions & 12 deletions src/Formatter/FormatterTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,22 @@ protected function localizeNumber($number, NumberFormat $numberFormat)
*/
protected function parseNumber($number, NumberFormat $numberFormat)
{
$replacements = $this->getCanonicalSymbols($numberFormat);
// Convert localized symbols back to their original form.
$replacements = array_flip($this->getLocalizedSymbols($numberFormat));
// Strip whitespace (spaces and non-breaking spaces).
$replacements += [
' ' => '',
chr(0xC2) . chr(0xA0) => '',
];
$numberingSystem = $numberFormat->getNumberingSystem();
if (isset($this->digits[$numberingSystem])) {
// Convert the localized digits back to latin.
$replacements += array_flip($this->digits[$numberingSystem]);
}
$number = strtr($number, $replacements);

// Strip grouping separators.
$number = str_replace(',', '', $number);
// Convert the accounting format for negative numbers.
if (substr($number, 0, 1) == '(' && substr($number, -1, 1) == ')') {
$number = '-' . str_replace(['(', ')'], '', $number);
Expand Down Expand Up @@ -197,20 +205,13 @@ protected function getParsedPattern(NumberFormat $numberFormat, $style)
abstract protected function getAvailablePatterns(NumberFormat $numberFormat);

/**
* Returns the replacements to be used for the localizeNumber method.
* Gets the localized symbols for the provided number format.
*
* @param NumberFormat $numberFormat
* Used to localize the number in localizeNumber().
*
* @return array
*/
abstract protected function getLocalizedSymbols(NumberFormat $numberFormat): array;

/**
* Returns the replacements to be used for the parseNumber method.
*
* @param NumberFormat $numberFormat
* @param NumberFormat $numberFormat The number format.
*
* @return array
*/
abstract protected function getCanonicalSymbols(NumberFormat $numberFormat): array;
abstract protected function getLocalizedSymbols(NumberFormat $numberFormat): array;
}
19 changes: 0 additions & 19 deletions src/Formatter/NumberFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,23 +174,4 @@ protected function getLocalizedSymbols(NumberFormat $numberFormat): array
'%' => $numberFormat->getPercentSign(),
];
}

/**
* {@inheritdoc}
*/
protected function getCanonicalSymbols(NumberFormat $numberFormat): array
{
return [
$numberFormat->getGroupingSeparator() => '',
// Convert the localized symbols back to their original form.
$numberFormat->getDecimalSeparator() => '.',
$numberFormat->getPlusSign() => '+',
$numberFormat->getMinusSign() => '-',
$numberFormat->getPercentSign() => '%',

// Strip whitespace (spaces and non-breaking spaces).
' ' => '',
chr(0xC2) . chr(0xA0) => '',
];
}
}

0 comments on commit 7ce8ba0

Please sign in to comment.