diff --git a/src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php b/src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php index 5cb50e76a9d9a..c13b1a330c55c 100644 --- a/src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php +++ b/src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php @@ -155,7 +155,7 @@ public function parse(\DateTime $dateTime, $value) } // behave like the intl extension - StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR); + StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR, 'Date parsing failed'); return false; } @@ -292,7 +292,7 @@ protected function calculateUnixTimestamp(\DateTime $dateTime, array $options) // If month is false, return immediately (intl behavior) if (false === $month) { - StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR); + StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR, 'Date parsing failed'); return false; } diff --git a/src/Symfony/Component/Locale/Stub/StubIntl.php b/src/Symfony/Component/Locale/Stub/StubIntl.php index bb00ade862dce..509e1ee494c2b 100644 --- a/src/Symfony/Component/Locale/Stub/StubIntl.php +++ b/src/Symfony/Component/Locale/Stub/StubIntl.php @@ -45,28 +45,24 @@ abstract class StubIntl * @var array */ private static $errorCodes = array( - self::U_ZERO_ERROR, - self::U_ILLEGAL_ARGUMENT_ERROR, - self::U_PARSE_ERROR, + self::U_ZERO_ERROR => 'U_ZERO_ERROR', + self::U_ILLEGAL_ARGUMENT_ERROR => 'U_ILLEGAL_ARGUMENT_ERROR', + self::U_PARSE_ERROR => 'U_PARSE_ERROR', ); /** - * The error messages of all known error codes + * The error code of the last operation * - * @var array + * @var integer */ - private static $errorMessages = array( - self::U_ZERO_ERROR => 'U_ZERO_ERROR', - self::U_ILLEGAL_ARGUMENT_ERROR => 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR', - self::U_PARSE_ERROR => 'Date parsing failed: U_PARSE_ERROR', - ); + private static $errorCode = self::U_ZERO_ERROR; /** * The error code of the last operation * * @var integer */ - private static $errorCode = self::U_ZERO_ERROR; + private static $errorMessage = 'U_ZERO_ERROR'; /** * Returns whether the error code indicates a failure @@ -77,8 +73,8 @@ abstract class StubIntl */ static public function isFailure($errorCode) { - return in_array($errorCode, self::$errorCodes, true) - && $errorCode !== self::U_ZERO_ERROR; + return array_key_exists($errorCode, self::$errorCodes) + && $errorCode > self::U_ZERO_ERROR; } /** @@ -102,22 +98,24 @@ static public function getErrorCode() */ static public function getErrorMessage() { - return self::$errorMessages[self::$errorCode]; + return self::$errorMessage; } /** * Sets the current error code * - * @param integer $code One of the error constants in this class + * @param integer $code One of the error constants in this class + * @param string $message The ICU class error message * * @throws \InvalidArgumentException If the code is not one of the error constants in this class */ - static public function setErrorCode($code) + static public function setErrorCode($code, $message = '') { - if (!isset(self::$errorMessages[$code])) { + if (!isset(self::$errorCodes[$code])) { throw new \InvalidArgumentException(sprintf('No such error code: "%s"', $code)); } + self::$errorMessage = $message ? sprintf('%s: %s', $message, self::$errorCodes[$code]) : self::$errorCodes[$code]; self::$errorCode = $code; } } diff --git a/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php b/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php index b53d55967af9c..d8d2a5f5968be 100644 --- a/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php +++ b/src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php @@ -26,14 +26,18 @@ class StubIntlDateFormatter { /** - * Constants defined by the intl extension, not class constants in IntlDateFormatter - * TODO: remove if the Form component drop the call to the intl_is_failure() function + * The error code from the last operation * - * @see StubIntlDateFormatter::getErrorCode() - * @see StubIntlDateFormatter::getErrorMessage() + * @var integer */ - const U_ZERO_ERROR = 0; - const U_ZERO_ERROR_MESSAGE = 'U_ZERO_ERROR'; + protected $errorCode = StubIntl::U_ZERO_ERROR; + + /** + * The error message from the last operation + * + * @var string + */ + protected $errorMessage = 'U_ZERO_ERROR'; /* date/time format types */ const NONE = -1; @@ -176,7 +180,9 @@ public function format($timestamp) if (!is_int($timestamp)) { // behave like the intl extension - StubIntl::setErrorCode(StubIntl::U_ILLEGAL_ARGUMENT_ERROR); + StubIntl::setErrorCode(StubIntl::U_ILLEGAL_ARGUMENT_ERROR, 'datefmt_format: takes either an array or an integer timestamp value '); + $this->errorCode = StubIntl::getErrorCode(); + $this->errorMessage = StubIntl::getErrorMessage(); return false; } @@ -220,7 +226,7 @@ public function getDateType() */ public function getErrorCode() { - return self::U_ZERO_ERROR; + return $this->errorCode; } /** @@ -232,7 +238,7 @@ public function getErrorCode() */ public function getErrorMessage() { - return self::U_ZERO_ERROR_MESSAGE; + return $this->errorMessage; } /** @@ -345,12 +351,17 @@ public function parse($value, &$position = null) throw new MethodArgumentNotImplementedException(__METHOD__, 'position'); } - StubIntl::setErrorCode(StubIntl::U_ZERO_ERROR); - $dateTime = $this->createDateTime(0); $transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId()); - return $transformer->parse($dateTime, $value); + $timestamp = $transformer->parse($dateTime, $value); + + if (false === $timestamp) { + $this->errorCode = StubIntl::getErrorCode(); + $this->errorMessage = StubIntl::getErrorMessage(); + } + + return $timestamp; } /** diff --git a/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php b/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php index d7accea3cc580..ad72c2183d6cc 100644 --- a/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php +++ b/src/Symfony/Component/Locale/Stub/StubNumberFormatter.php @@ -25,31 +25,18 @@ class StubNumberFormatter { /** - * Constants defined by the intl extension, not class constants in NumberFormatter - * TODO: remove if the Form component drop the call to the intl_is_failure() function - * - * @see StubNumberFormatter::getErrorCode() - * @see StubNumberFormatter::getErrorMessage() - */ - const U_ZERO_ERROR = 0; - const U_PARSE_ERROR = 9; - - /** - * The error messages for each error code + * The error code from the last operation * - * @var array + * @var integer */ - protected $errorMessages = array( - self::U_ZERO_ERROR => 'U_ZERO_ERROR', - self::U_PARSE_ERROR => 'Number parsing failed: U_PARSE_ERROR', - ); + protected $errorCode = StubIntl::U_ZERO_ERROR; /** - * The error code from the last operation + * The error message from the last operation * - * @var integer + * @var string */ - protected $errorCode = self::U_ZERO_ERROR; + protected $errorMessage = 'U_ZERO_ERROR'; /** Format style constants */ const PATTERN_DECIMAL = 0; @@ -403,7 +390,7 @@ public function getErrorCode() */ public function getErrorMessage() { - return $this->errorMessages[$this->errorCode]; + return $this->errorMessage; } /** @@ -514,7 +501,9 @@ public function parse($value, $type = self::TYPE_DOUBLE, &$position = null) // Any string before the numeric value causes error in the parsing if (isset($matches[1]) && !empty($matches[1])) { - $this->errorCode = self::U_PARSE_ERROR; + StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR); + $this->errorCode = StubIntl::getErrorCode(); + $this->errorMessage = StubIntl::getErrorMessage(); return false; } diff --git a/src/Symfony/Component/Locale/Tests/Stub/StubIntlDateFormatterTest.php b/src/Symfony/Component/Locale/Tests/Stub/StubIntlDateFormatterTest.php index 121fc9740c3dc..07f0af174a9ee 100644 --- a/src/Symfony/Component/Locale/Tests/Stub/StubIntlDateFormatterTest.php +++ b/src/Symfony/Component/Locale/Tests/Stub/StubIntlDateFormatterTest.php @@ -61,6 +61,9 @@ public function testFormatStub($pattern, $timestamp, $expected, $errorCode = 0, $this->assertSame($errorMessage, StubIntl::getErrorMessage()); $this->assertSame($errorCode, StubIntl::getErrorCode()); $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); + $this->assertSame($errorMessage, $formatter->getErrorMessage()); + $this->assertSame($errorCode, $formatter->getErrorCode()); + $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode())); } /** @@ -467,13 +470,13 @@ public function testGetDateType() public function testGetErrorCode() { $formatter = $this->createStubFormatter(); - $this->assertEquals(StubIntlDateFormatter::U_ZERO_ERROR, $formatter->getErrorCode()); + $this->assertEquals(StubIntl::getErrorCode(), $formatter->getErrorCode()); } public function testGetErrorMessage() { $formatter = $this->createStubFormatter(); - $this->assertEquals(StubIntlDateFormatter::U_ZERO_ERROR_MESSAGE, $formatter->getErrorMessage()); + $this->assertEquals(StubIntl::getErrorMessage(), $formatter->getErrorMessage()); } public function testGetLocale() @@ -541,6 +544,9 @@ public function testParseStub($pattern, $value, $expected) $this->assertSame($errorMessage, StubIntl::getErrorMessage()); $this->assertSame($errorCode, StubIntl::getErrorCode()); $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); + $this->assertSame($errorMessage, $formatter->getErrorMessage()); + $this->assertSame($errorCode, $formatter->getErrorCode()); + $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode())); } public function parseProvider() @@ -715,6 +721,9 @@ public function testParseErrorStub($pattern, $value) $this->assertSame($errorMessage, StubIntl::getErrorMessage()); $this->assertSame($errorCode, StubIntl::getErrorCode()); $this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode())); + $this->assertSame($errorMessage, $formatter->getErrorMessage()); + $this->assertSame($errorCode, $formatter->getErrorCode()); + $this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode())); } public function parseErrorProvider() diff --git a/src/Symfony/Component/Locale/Tests/Stub/StubNumberFormatterTest.php b/src/Symfony/Component/Locale/Tests/Stub/StubNumberFormatterTest.php index a55c90bc847e3..d56cc78d215c9 100644 --- a/src/Symfony/Component/Locale/Tests/Stub/StubNumberFormatterTest.php +++ b/src/Symfony/Component/Locale/Tests/Stub/StubNumberFormatterTest.php @@ -12,6 +12,7 @@ namespace Symfony\Component\Locale\Tests\Stub; use Symfony\Component\Locale\Locale; +use Symfony\Component\Locale\Stub\StubIntl; use Symfony\Component\Locale\Stub\StubNumberFormatter; use Symfony\Component\Locale\Tests\TestCase as LocaleTestCase; @@ -611,7 +612,7 @@ public function formatRoundingModeRoundHalfEvenProvider() public function testGetErrorCode() { $formatter = $this->getStubFormatterWithDecimalStyle(); - $this->assertEquals(StubNumberFormatter::U_ZERO_ERROR, $formatter->getErrorCode()); + $this->assertEquals(StubIntl::U_ZERO_ERROR, $formatter->getErrorCode()); } public function testGetLocale() @@ -666,9 +667,9 @@ public function testParseStub($value, $expected, $message = '') $this->assertSame($expected, $parsedValue, $message); if ($expected === false) { - $this->assertSame($formatter::U_PARSE_ERROR, $formatter->getErrorCode()); + $this->assertSame(StubIntl::U_PARSE_ERROR, $formatter->getErrorCode()); } else { - $this->assertEquals($formatter::U_ZERO_ERROR, $formatter->getErrorCode()); + $this->assertEquals(StubIntl::U_ZERO_ERROR, $formatter->getErrorCode()); } }