mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-18 05:16:30 +00:00
Fix number formatter in Intl extra extension
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
* 2.12.3 (2019-XX-XX)
|
* 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)
|
* 2.12.2 (2019-11-11)
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -18,7 +18,7 @@ source output
|
|||||||
wget https://get.symfony.com/cli/installer -O - | bash
|
wget https://get.symfony.com/cli/installer -O - | bash
|
||||||
export PATH="$HOME/.symfony/bin:$PATH"
|
export PATH="$HOME/.symfony/bin:$PATH"
|
||||||
symfony server:start -d --no-tls
|
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
|
curl -OLsS https://get.blackfire.io/blackfire-player.phar
|
||||||
chmod +x blackfire-player.phar
|
chmod +x blackfire-player.phar
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ final class IntlExtension extends AbstractExtension
|
|||||||
'halfdown' => \NumberFormatter::ROUND_HALFDOWN,
|
'halfdown' => \NumberFormatter::ROUND_HALFDOWN,
|
||||||
'halfup' => \NumberFormatter::ROUND_HALFUP,
|
'halfup' => \NumberFormatter::ROUND_HALFUP,
|
||||||
];
|
];
|
||||||
private const NUMBER_PADDONG_ATTRIBUTES = [
|
private const NUMBER_PADDING_ATTRIBUTES = [
|
||||||
'before_prefix' => \NumberFormatter::PAD_BEFORE_PREFIX,
|
'before_prefix' => \NumberFormatter::PAD_BEFORE_PREFIX,
|
||||||
'after_prefix' => \NumberFormatter::PAD_AFTER_PREFIX,
|
'after_prefix' => \NumberFormatter::PAD_AFTER_PREFIX,
|
||||||
'before_suffix' => \NumberFormatter::PAD_BEFORE_SUFFIX,
|
'before_suffix' => \NumberFormatter::PAD_BEFORE_SUFFIX,
|
||||||
@@ -305,18 +305,24 @@ final class IntlExtension extends AbstractExtension
|
|||||||
$textAttrs = [];
|
$textAttrs = [];
|
||||||
$symbols = [];
|
$symbols = [];
|
||||||
if ($this->numberFormatterPrototype) {
|
if ($this->numberFormatterPrototype) {
|
||||||
foreach (self::NUMBER_ATTRIBUTES as $name) {
|
foreach (self::NUMBER_ATTRIBUTES as $name => $const) {
|
||||||
if (!isset($attrs[$name])) {
|
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) {
|
foreach (self::NUMBER_TEXT_ATTRIBUTES as $name => $const) {
|
||||||
$textAttrs[$name] = $this->numberFormatterPrototype->getTextAttribute($name);
|
$textAttrs[$name] = $this->numberFormatterPrototype->getTextAttribute($const);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (self::NUMBER_SYMBOLS as $name) {
|
foreach (self::NUMBER_SYMBOLS as $name => $const) {
|
||||||
$symbols[$name] = $this->numberFormatterPrototype->getSymbol($name);
|
$symbols[$name] = $this->numberFormatterPrototype->getSymbol($const);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -339,22 +345,22 @@ final class IntlExtension extends AbstractExtension
|
|||||||
|
|
||||||
$value = self::NUMBER_ROUNDING_ATTRIBUTES[$value];
|
$value = self::NUMBER_ROUNDING_ATTRIBUTES[$value];
|
||||||
} elseif ('padding_position' === $name) {
|
} elseif ('padding_position' === $name) {
|
||||||
if (!isset(self::NUMBER_PADDONG_ATTRIBUTES[$value])) {
|
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_PADDONG_ATTRIBUTES))));
|
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);
|
$this->numberFormatters[$hash]->setAttribute(self::NUMBER_ATTRIBUTES[$name], $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($textAttrs as $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) {
|
foreach ($symbols as $name => $value) {
|
||||||
$this->numberFormatters[$hash]->setSymbol($name, $value);
|
$this->numberFormatters[$hash]->setSymbol(self::NUMBER_SYMBOLS[$name], $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->numberFormatters[$hash];
|
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'));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user