From e7da2b408a09783d92296b81c52ca63f97f68b5d Mon Sep 17 00:00:00 2001 From: Kevin Papst Date: Sat, 29 Aug 2020 16:30:35 +0200 Subject: [PATCH] fixing formatting issues for money without currency (#1932) --- src/Utils/LocaleHelper.php | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/src/Utils/LocaleHelper.php b/src/Utils/LocaleHelper.php index eb830349..25184e17 100644 --- a/src/Utils/LocaleHelper.php +++ b/src/Utils/LocaleHelper.php @@ -112,6 +112,10 @@ final class LocaleHelper $withCurrency = false; } + if (false === $withCurrency) { + return $this->getMoneyFormatter($withCurrency)->format($amount, NumberFormatter::TYPE_DEFAULT); + } + return $this->getMoneyFormatter($withCurrency)->formatCurrency($amount, $currency); } @@ -126,29 +130,18 @@ final class LocaleHelper private function getMoneyFormatter(bool $withCurrency = true): NumberFormatter { - if (null === $this->moneyFormatter) { - $this->moneyFormatter = new NumberFormatter($this->locale, NumberFormatter::CURRENCY); - } - if ($withCurrency) { + if (null === $this->moneyFormatter) { + $this->moneyFormatter = new NumberFormatter($this->locale, NumberFormatter::CURRENCY); + } + return $this->moneyFormatter; } if (null === $this->moneyFormatterNoCurrency) { - // if anyone knows a better way of achieving this, please let me know! $this->moneyFormatterNoCurrency = new NumberFormatter($this->locale, NumberFormatter::CURRENCY); - - $this->moneyFormatterNoCurrency->setTextAttribute(NumberFormatter::CURRENCY_CODE, ''); - $this->moneyFormatterNoCurrency->setSymbol(NumberFormatter::INTL_CURRENCY_SYMBOL, ''); - $this->moneyFormatterNoCurrency->setSymbol(NumberFormatter::CURRENCY_SYMBOL, ''); - - // don't understand why this is needed, I'd say this shouldn't be necessary after the above calls - // even worse: the logic changes either between PHP/ICU versions - $pattern = $this->moneyFormatterNoCurrency->getPattern(); - $pattern = str_replace(['¤ ', ' ¤', '-¤', ' XXX', 'XXX '], '¤', $pattern); - $pattern = str_replace('XXX', '¤', $pattern); - $pattern = str_replace('¤', '', $pattern); - $this->moneyFormatterNoCurrency->setPattern($pattern); + $this->moneyFormatterNoCurrency->setTextAttribute(NumberFormatter::POSITIVE_PREFIX, ''); + $this->moneyFormatterNoCurrency->setTextAttribute(NumberFormatter::POSITIVE_SUFFIX, ''); } return $this->moneyFormatterNoCurrency;