Skip to content

Commit

Permalink
Respect $textAttributes arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
phansys committed Jun 7, 2020
1 parent e2c97fe commit 1ef2029
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
45 changes: 27 additions & 18 deletions src/Templating/Helper/NumberHelper.php
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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));
Expand Down Expand Up @@ -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);
}
}
4 changes: 3 additions & 1 deletion tests/Helper/NumberHelperTest.php
Expand Up @@ -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));

Expand Down Expand Up @@ -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
Expand All @@ -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));

Expand Down

0 comments on commit 1ef2029

Please sign in to comment.