Browse code

Fix number formatter in Intl extra extension

Fabien Potencier authored on 15/11/2019 16:42:13
Showing 4 changed files
... ...
@@ -1,6 +1,6 @@
1 1
 * 2.12.3 (2019-XX-XX)
2 2
 
3
- * n/a
3
+ * fixed number formatter in Intl extra extension when using a formatter prototype
4 4
 
5 5
 * 2.12.2 (2019-11-11)
6 6
 
... ...
@@ -18,7 +18,7 @@ source output
18 18
 wget https://get.symfony.com/cli/installer -O - | bash
19 19
 export PATH="$HOME/.symfony/bin:$PATH"
20 20
 symfony server:start -d --no-tls
21
-ENDPOINT=`symfony server:status -no-ansi | sed -E 's/^.+ http/http/'`
21
+ENDPOINT=`symfony var:export SYMFONY_DEFAULT_ROUTE_URL`
22 22
 
23 23
 curl -OLsS https://get.blackfire.io/blackfire-player.phar
24 24
 chmod +x blackfire-player.phar
... ...
@@ -77,7 +77,7 @@ final class IntlExtension extends AbstractExtension
77 77
         'halfdown' => \NumberFormatter::ROUND_HALFDOWN,
78 78
         'halfup' => \NumberFormatter::ROUND_HALFUP,
79 79
     ];
80
-    private const NUMBER_PADDONG_ATTRIBUTES = [
80
+    private const NUMBER_PADDING_ATTRIBUTES = [
81 81
         'before_prefix' => \NumberFormatter::PAD_BEFORE_PREFIX,
82 82
         'after_prefix' => \NumberFormatter::PAD_AFTER_PREFIX,
83 83
         'before_suffix' => \NumberFormatter::PAD_BEFORE_SUFFIX,
... ...
@@ -305,18 +305,24 @@ final class IntlExtension extends AbstractExtension
305 305
         $textAttrs = [];
306 306
         $symbols = [];
307 307
         if ($this->numberFormatterPrototype) {
308
-            foreach (self::NUMBER_ATTRIBUTES as $name) {
308
+            foreach (self::NUMBER_ATTRIBUTES as $name => $const) {
309 309
                 if (!isset($attrs[$name])) {
310
-                    $attrs[$name] = $this->numberFormatterPrototype->getAttribute($name);
310
+                    $value = $this->numberFormatterPrototype->getAttribute($const);
311
+                    if ('rounding_mode' === $name) {
312
+                        $value = array_flip(self::NUMBER_ROUNDING_ATTRIBUTES)[$value];
313
+                    } elseif ('padding_position' === $name) {
314
+                        $value = array_flip(self::NUMBER_PADDING_ATTRIBUTES)[$value];
315
+                    }
316
+                    $attrs[$name] = $value;
311 317
                 }
312 318
             }
313 319
 
314
-            foreach (self::NUMBER_TEXT_ATTRIBUTES as $name) {
315
-                $textAttrs[$name] = $this->numberFormatterPrototype->getTextAttribute($name);
320
+            foreach (self::NUMBER_TEXT_ATTRIBUTES as $name => $const) {
321
+                $textAttrs[$name] = $this->numberFormatterPrototype->getTextAttribute($const);
316 322
             }
317 323
 
318
-            foreach (self::NUMBER_SYMBOLS as $name) {
319
-                $symbols[$name] = $this->numberFormatterPrototype->getSymbol($name);
324
+            foreach (self::NUMBER_SYMBOLS as $name => $const) {
325
+                $symbols[$name] = $this->numberFormatterPrototype->getSymbol($const);
320 326
             }
321 327
         }
322 328
 
... ...
@@ -339,22 +345,22 @@ final class IntlExtension extends AbstractExtension
339 345
 
340 346
                 $value = self::NUMBER_ROUNDING_ATTRIBUTES[$value];
341 347
             } elseif ('padding_position' === $name) {
342
-                if (!isset(self::NUMBER_PADDONG_ATTRIBUTES[$value])) {
343
-                    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))));
348
+                if (!isset(self::NUMBER_PADDING_ATTRIBUTES[$value])) {
349
+                    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))));
344 350
                 }
345 351
 
346
-                $value = self::NUMBER_PADDONG_ATTRIBUTES[$value];
352
+                $value = self::NUMBER_PADDING_ATTRIBUTES[$value];
347 353
             }
348 354
 
349 355
             $this->numberFormatters[$hash]->setAttribute(self::NUMBER_ATTRIBUTES[$name], $value);
350 356
         }
351 357
 
352 358
         foreach ($textAttrs as $name => $value) {
353
-            $this->numberFormatters[$hash]->setTextAttribute($name, $value);
359
+            $this->numberFormatters[$hash]->setTextAttribute(self::NUMBER_TEXT_ATTRIBUTES[$name], $value);
354 360
         }
355 361
 
356 362
         foreach ($symbols as $name => $value) {
357
-            $this->numberFormatters[$hash]->setSymbol($name, $value);
363
+            $this->numberFormatters[$hash]->setSymbol(self::NUMBER_SYMBOLS[$name], $value);
358 364
         }
359 365
 
360 366
         return $this->numberFormatters[$hash];
361 367
new file mode 100644
... ...
@@ -0,0 +1,28 @@
1
+<?php
2
+
3
+/*
4
+ * This file is part of Twig.
5
+ *
6
+ * (c) Fabien Potencier
7
+ *
8
+ * For the full copyright and license information, please view the LICENSE
9
+ * file that was distributed with this source code.
10
+ */
11
+
12
+namespace Twig\Extra\Intl\Tests;
13
+
14
+use PHPUnit\Framework\TestCase;
15
+use Twig\Extra\Intl\IntlExtension;
16
+
17
+class IntlExtensionTest extends TestCase
18
+{
19
+    public function testFormatterProto()
20
+    {
21
+        $dateFormatterProto = new \IntlDateFormatter('fr', \IntlDateFormatter::FULL, \IntlDateFormatter::FULL);
22
+        $numberFormatterProto = new \NumberFormatter('fr', \NumberFormatter::DECIMAL);
23
+        $numberFormatterProto->setTextAttribute(\NumberFormatter::POSITIVE_PREFIX, '++');
24
+        $numberFormatterProto->setAttribute(\NumberFormatter::FRACTION_DIGITS, 1);
25
+        $ext = new IntlExtension($dateFormatterProto, $numberFormatterProto);
26
+        $this->assertSame('++12,3', $ext->formatNumber('12.3456'));
27
+    }
28
+}