Fix number formatter in Intl extra extension

This commit is contained in:
Fabien Potencier
2019-11-15 17:42:13 +01:00
parent 208b745286
commit a6cf3f3f37
4 changed files with 48 additions and 14 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
* 2.12.3 (2019-XX-XX)
* n/a
* fixed number formatter in Intl extra extension when using a formatter prototype
* 2.12.2 (2019-11-11)
+1 -1
View File
@@ -18,7 +18,7 @@ source output
wget https://get.symfony.com/cli/installer -O - | bash
export PATH="$HOME/.symfony/bin:$PATH"
symfony server:start -d --no-tls
ENDPOINT=`symfony server:status -no-ansi | sed -E 's/^.+ http/http/'`
ENDPOINT=`symfony var:export SYMFONY_DEFAULT_ROUTE_URL`
curl -OLsS https://get.blackfire.io/blackfire-player.phar
chmod +x blackfire-player.phar
+18 -12
View File
@@ -77,7 +77,7 @@ final class IntlExtension extends AbstractExtension
'halfdown' => \NumberFormatter::ROUND_HALFDOWN,
'halfup' => \NumberFormatter::ROUND_HALFUP,
];
private const NUMBER_PADDONG_ATTRIBUTES = [
private const NUMBER_PADDING_ATTRIBUTES = [
'before_prefix' => \NumberFormatter::PAD_BEFORE_PREFIX,
'after_prefix' => \NumberFormatter::PAD_AFTER_PREFIX,
'before_suffix' => \NumberFormatter::PAD_BEFORE_SUFFIX,
@@ -305,18 +305,24 @@ final class IntlExtension extends AbstractExtension
$textAttrs = [];
$symbols = [];
if ($this->numberFormatterPrototype) {
foreach (self::NUMBER_ATTRIBUTES as $name) {
foreach (self::NUMBER_ATTRIBUTES as $name => $const) {
if (!isset($attrs[$name])) {
$attrs[$name] = $this->numberFormatterPrototype->getAttribute($name);
$value = $this->numberFormatterPrototype->getAttribute($const);
if ('rounding_mode' === $name) {
$value = array_flip(self::NUMBER_ROUNDING_ATTRIBUTES)[$value];
} elseif ('padding_position' === $name) {
$value = array_flip(self::NUMBER_PADDING_ATTRIBUTES)[$value];
}
$attrs[$name] = $value;
}
}
foreach (self::NUMBER_TEXT_ATTRIBUTES as $name) {
$textAttrs[$name] = $this->numberFormatterPrototype->getTextAttribute($name);
foreach (self::NUMBER_TEXT_ATTRIBUTES as $name => $const) {
$textAttrs[$name] = $this->numberFormatterPrototype->getTextAttribute($const);
}
foreach (self::NUMBER_SYMBOLS as $name) {
$symbols[$name] = $this->numberFormatterPrototype->getSymbol($name);
foreach (self::NUMBER_SYMBOLS as $name => $const) {
$symbols[$name] = $this->numberFormatterPrototype->getSymbol($const);
}
}
@@ -339,22 +345,22 @@ final class IntlExtension extends AbstractExtension
$value = self::NUMBER_ROUNDING_ATTRIBUTES[$value];
} elseif ('padding_position' === $name) {
if (!isset(self::NUMBER_PADDONG_ATTRIBUTES[$value])) {
throw new RuntimeError(sprintf('The number formatter padding position "%s" does not exist, known positions are: "%s".', $value, implode('", "', array_keys(self::NUMBER_PADDONG_ATTRIBUTES))));
if (!isset(self::NUMBER_PADDING_ATTRIBUTES[$value])) {
throw new RuntimeError(sprintf('The number formatter padding position "%s" does not exist, known positions are: "%s".', $value, implode('", "', array_keys(self::NUMBER_PADDING_ATTRIBUTES))));
}
$value = self::NUMBER_PADDONG_ATTRIBUTES[$value];
$value = self::NUMBER_PADDING_ATTRIBUTES[$value];
}
$this->numberFormatters[$hash]->setAttribute(self::NUMBER_ATTRIBUTES[$name], $value);
}
foreach ($textAttrs as $name => $value) {
$this->numberFormatters[$hash]->setTextAttribute($name, $value);
$this->numberFormatters[$hash]->setTextAttribute(self::NUMBER_TEXT_ATTRIBUTES[$name], $value);
}
foreach ($symbols as $name => $value) {
$this->numberFormatters[$hash]->setSymbol($name, $value);
$this->numberFormatters[$hash]->setSymbol(self::NUMBER_SYMBOLS[$name], $value);
}
return $this->numberFormatters[$hash];
@@ -0,0 +1,28 @@
<?php
/*
* This file is part of Twig.
*
* (c) Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Twig\Extra\Intl\Tests;
use PHPUnit\Framework\TestCase;
use Twig\Extra\Intl\IntlExtension;
class IntlExtensionTest extends TestCase
{
public function testFormatterProto()
{
$dateFormatterProto = new \IntlDateFormatter('fr', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL);
$numberFormatterProto = new \NumberFormatter('fr', \NumberFormatter::DECIMAL);
$numberFormatterProto->setTextAttribute(\NumberFormatter::POSITIVE_PREFIX, '++');
$numberFormatterProto->setAttribute(\NumberFormatter::FRACTION_DIGITS, 1);
$ext = new IntlExtension($dateFormatterProto, $numberFormatterProto);
$this->assertSame('++12,3', $ext->formatNumber('12.3456'));
}
}