diff --git a/src/Templating/Helper/NumberHelper.php b/src/Templating/Helper/NumberHelper.php index c3dc683c..01df3e13 100644 --- a/src/Templating/Helper/NumberHelper.php +++ b/src/Templating/Helper/NumberHelper.php @@ -78,8 +78,9 @@ public function formatPercent($number, array $attributes = [], array $textAttrib { if ($this->intlExtension) { $attributes = self::processLegacyAttributes($attributes); + $intlExtension = $this->getIntlExtension($locale, \NumberFormatter::PERCENT, $textAttributes); - return $this->fixCharset($this->intlExtension->formatNumberStyle('percent', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale())); + return $this->fixCharset($intlExtension->formatNumberStyle('percent', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale())); } // NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method. @@ -113,8 +114,9 @@ public function formatDuration($number, array $attributes = [], array $textAttri { if ($this->intlExtension) { $attributes = self::processLegacyAttributes($attributes); + $intlExtension = $this->getIntlExtension($locale, \NumberFormatter::DURATION, $textAttributes); - return $this->fixCharset($this->intlExtension->formatNumberStyle('duration', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale())); + return $this->fixCharset($intlExtension->formatNumberStyle('duration', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale())); } // NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method. @@ -148,8 +150,9 @@ public function formatDecimal($number, array $attributes = [], array $textAttrib { if ($this->intlExtension) { $attributes = self::processLegacyAttributes($attributes); + $intlExtension = $this->getIntlExtension($locale, \NumberFormatter::DECIMAL, $textAttributes); - return $this->fixCharset($this->intlExtension->formatNumberStyle('decimal', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale())); + return $this->fixCharset($intlExtension->formatNumberStyle('decimal', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale())); } // NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method. @@ -183,8 +186,9 @@ public function formatSpellout($number, array $attributes = [], array $textAttri { if ($this->intlExtension) { $attributes = self::processLegacyAttributes($attributes); + $intlExtension = $this->getIntlExtension($locale, \NumberFormatter::SPELLOUT, $textAttributes); - return $this->fixCharset($this->intlExtension->formatNumberStyle('spellout', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale())); + return $this->fixCharset($intlExtension->formatNumberStyle('spellout', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale())); } // NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method. @@ -219,8 +223,9 @@ public function formatCurrency($number, $currency, array $attributes = [], array { if ($this->intlExtension) { $attributes = self::processLegacyAttributes($attributes); + $intlExtension = $this->getIntlExtension($locale, \NumberFormatter::CURRENCY, $textAttributes); - return $this->fixCharset($this->intlExtension->formatCurrency($number, $currency, $attributes, $locale ?: $this->localeDetector->getLocale())); + return $this->fixCharset($intlExtension->formatCurrency($number, $currency, $attributes, $locale ?: $this->localeDetector->getLocale())); } // NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method. @@ -261,8 +266,9 @@ public function formatScientific($number, array $attributes = [], array $textAtt { if ($this->intlExtension) { $attributes = self::processLegacyAttributes($attributes); + $intlExtension = $this->getIntlExtension($locale, \NumberFormatter::SCIENTIFIC, $textAttributes); - return $this->fixCharset($this->intlExtension->formatNumberStyle('scientific', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale())); + return $this->fixCharset($intlExtension->formatNumberStyle('scientific', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale())); } // NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method. @@ -296,8 +302,9 @@ public function formatOrdinal($number, array $attributes = [], array $textAttrib { if ($this->intlExtension) { $attributes = self::processLegacyAttributes($attributes); + $intlExtension = $this->getIntlExtension($locale, \NumberFormatter::ORDINAL, $textAttributes); - return $this->fixCharset($this->intlExtension->formatNumberStyle('ordinal', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale())); + return $this->fixCharset($intlExtension->formatNumberStyle('ordinal', $number, $attributes, 'default', $locale ?: $this->localeDetector->getLocale())); } // NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method. @@ -332,8 +339,9 @@ public function format($number, $style, array $attributes = [], array $textAttri { if ($this->intlExtension) { $attributes = self::processLegacyAttributes($attributes); + $intlExtension = $this->getIntlExtension($locale, $style, $textAttributes); - return $this->fixCharset($this->intlExtension->formatNumber($number, $attributes, $style, 'default', $locale ?: $this->localeDetector->getLocale())); + return $this->fixCharset($intlExtension->formatNumberStyle($style, $number, $attributes, $locale ?: $this->localeDetector->getLocale())); } // NEXT_MAJOR: Execute the previous block unconditionally and remove following lines in this method. @@ -406,13 +414,9 @@ public function getName() } /** - * NEXT_MAJOR: Remove this method. - * * Gets an instance of \NumberFormatter set with the given attributes and * style. * - * @deprecated since sonata-project/intl-bundle 2.x - * * @param string $culture The culture used by \NumberFormatter * @param string $style The style used by \NumberFormatter * @param array $attributes The attributes used by \NumberFormatter @@ -423,12 +427,6 @@ public function getName() */ protected function getFormatter($culture, $style, $attributes = [], $textAttributes = [], $symbols = []) { - @trigger_error(sprintf( - 'Method "%s()" is deprecated since sonata-project/intl-bundle 2.x.' - .' and will be removed in version 3.x.', - __METHOD__ - )); - $attributes = $this->parseAttributes(array_merge($this->attributes, $attributes)); $textAttributes = $this->parseAttributes(array_merge($this->textAttributes, $textAttributes)); $symbols = $this->parseAttributes(array_merge($this->symbols, $symbols)); @@ -529,4 +527,15 @@ private static function processLegacyAttributes(array $attributes): array return $attributes; } + + private function getIntlExtension(?string $locale = null, int $style, array $textAttributes): IntlExtension + { + if (empty($textAttributes)) { + return $this->intlExtension; + } + + $numberFormatterProto = $this->getFormatter($locale ?: $this->localeDetector->getLocale(), $style, [], $textAttributes); + + return new IntlExtension(null, $numberFormatterProto); + } } diff --git a/tests/Helper/NumberHelperTest.php b/tests/Helper/NumberHelperTest.php index 3270951a..6ec2fe60 100644 --- a/tests/Helper/NumberHelperTest.php +++ b/tests/Helper/NumberHelperTest.php @@ -76,6 +76,7 @@ public function testLocale(): void // percent $this->assertSame('10%', $helper->formatPercent(0.1)); + $this->assertSame('+ 10%', $helper->formatPercent(0.1, [], ['positive_prefix' => '+ '])); $this->assertSame('200%', $helper->formatPercent(1.999)); $this->assertSame('99%', $helper->formatPercent(0.99)); @@ -104,7 +105,7 @@ public function testLegacyLocale(): void $this->assertSame('€10.49', $helper->formatCurrency(10.49, 'EUR', [ // the fraction_digits is not supported by the currency lib, https://bugs.php.net/bug.php?id=63140 - 'fraction_digits' => 0, + 'fraction_digits' => 2, ])); // decimal @@ -130,6 +131,7 @@ public function testLegacyLocale(): void // percent $this->assertSame('10%', $helper->formatPercent(0.1)); + $this->assertSame('+ 10%', $helper->formatPercent(0.1, [], ['positive_prefix' => '+ '])); $this->assertSame('200%', $helper->formatPercent(1.999)); $this->assertSame('99%', $helper->formatPercent(0.99));