Преглед изворни кода

Set error code when number cannot be parsed. Fixes #2389

Miquel Rodríguez Telep / Michael Rodríguez-Torrent пре 13 година
родитељ
комит
89cd64a4a5

+ 21 - 3
src/Symfony/Component/Locale/Stub/StubNumberFormatter.php

@@ -32,7 +32,24 @@ class StubNumberFormatter
      * @see StubNumberFormatter::getErrorMessage()
      */
     const U_ZERO_ERROR = 0;
-    const U_ZERO_ERROR_MESSAGE = 'U_ZERO_ERROR';
+    const U_PARSE_ERROR = 9;
+
+    /**
+     * The error messages for each error code
+     *
+     * @var array
+     */
+    protected $errorMessages = array(
+        self::U_ZERO_ERROR => 'U_ZERO_ERROR',
+        self::U_PARSE_ERROR => 'Number parsing failed: U_PARSE_ERROR',
+    );
+
+    /**
+     * The error code from the last operation
+     * 
+     * @var integer
+     */
+    protected $errorCode = self::U_ZERO_ERROR;
 
     /** Format style constants */
     const PATTERN_DECIMAL   = 0;
@@ -346,7 +363,7 @@ class StubNumberFormatter
      */
     public function getErrorCode()
     {
-        return self::U_ZERO_ERROR;
+        return $this->errorCode;
     }
 
     /**
@@ -357,7 +374,7 @@ class StubNumberFormatter
      */
     public function getErrorMessage()
     {
-        return self::U_ZERO_ERROR_MESSAGE;
+        return $this->errorMessages[$this->errorCode];
     }
 
     /**
@@ -458,6 +475,7 @@ class StubNumberFormatter
 
         // Any string before the numeric value causes error in the parsing
         if (isset($matches[1]) && !empty($matches[1])) {
+            $this->errorCode = self::U_PARSE_ERROR;
             return false;
         }
 

+ 12 - 6
tests/Symfony/Tests/Component/Locale/Stub/StubNumberFormatterTest.php

@@ -616,12 +616,6 @@ class StubNumberFormatterTest extends LocaleTestCase
         $this->assertEquals(StubNumberFormatter::U_ZERO_ERROR, $formatter->getErrorCode());
     }
 
-    public function testGetErrorMessage()
-    {
-        $formatter = $this->getStubFormatterWithDecimalStyle();
-        $this->assertEquals(StubNumberFormatter::U_ZERO_ERROR_MESSAGE, $formatter->getErrorMessage());
-    }
-
     public function testGetLocale()
     {
         $formatter = $this->getStubFormatterWithDecimalStyle();
@@ -672,6 +666,12 @@ class StubNumberFormatterTest extends LocaleTestCase
         $formatter = $this->getStubFormatterWithDecimalStyle();
         $parsedValue = $formatter->parse($value, StubNumberFormatter::TYPE_DOUBLE);
         $this->assertSame($expected, $parsedValue, $message);
+
+        if ($expected === false) {
+            $this->assertSame($formatter::U_PARSE_ERROR, $formatter->getErrorCode());
+        } else {
+            $this->assertEquals($formatter::U_ZERO_ERROR, $formatter->getErrorCode());
+        }
     }
 
     /**
@@ -684,6 +684,12 @@ class StubNumberFormatterTest extends LocaleTestCase
         $formatter = $this->getIntlFormatterWithDecimalStyle();
         $parsedValue = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE);
         $this->assertSame($expected, $parsedValue, $message);
+
+        if ($expected === false) {
+            $this->assertSame(U_PARSE_ERROR, $formatter->getErrorCode());
+        } else {
+            $this->assertEquals(U_ZERO_ERROR, $formatter->getErrorCode());
+        }
     }
 
     public function parseProvider()