Sfoglia il codice sorgente

[Yaml] fixed dumper for floats when the locale separator is not a dot

Fabien Potencier 13 anni fa
parent
commit
dbba79651a

+ 11 - 1
src/Symfony/Component/Yaml/Inline.php

@@ -87,7 +87,17 @@ class Inline
             case ctype_digit($value):
                 return is_string($value) ? "'$value'" : (int) $value;
             case is_numeric($value):
-                return is_string($value) ? "'$value'" : (is_infinite($value) ? str_ireplace('INF', '.Inf', strval($value)) : $value);
+                $locale = setlocale(LC_NUMERIC, 0);
+                if (false !== $locale) {
+                    setlocale(LC_NUMERIC, 'C');
+                }
+                $repr = is_string($value) ? "'$value'" : (is_infinite($value) ? str_ireplace('INF', '.Inf', strval($value)) : strval($value));
+
+                if (false !== $locale) {
+                    setlocale(LC_NUMERIC, $locale);
+                }
+
+                return $repr;
             case Escaper::requiresDoubleQuoting($value):
                 return Escaper::escapeWithDoubleQuotes($value);
             case Escaper::requiresSingleQuoting($value):

+ 15 - 0
tests/Symfony/Tests/Component/Yaml/InlineTest.php

@@ -40,6 +40,21 @@ class InlineTest extends \PHPUnit_Framework_TestCase
         }
     }
 
+    public function testDumpNumericValueWithLocale()
+    {
+        $locale = setlocale(LC_NUMERIC, 0);
+        if (false === $locale) {
+            $this->markTestSkipped('Your platform does not support locales.');
+        }
+
+        setlocale(LC_ALL, 'fr_FR.UTF-8', 'fr_FR.UTF8', 'fr_FR.utf-8', 'fr_FR.utf8');
+
+        $this->assertEquals('1.2', Inline::dump(1.2));
+        $this->assertContains('fr', setlocale(LC_NUMERIC, 0));
+
+        setlocale(LC_ALL, $locale);
+    }
+
     public function testHashStringsResemblingExponentialNumericsShouldNotBeChangedToINF()
     {
         $value = '686e444';