merged branch eriksencosta/locale-fixes-2.0 (PR #3947)
Commits -------b1ea552
[Locale] micro-optimization663d218
[Locale] changed method namebb61e09
[Locale] use the correct way for Intl error Discussion ---------- [2.0][Locale] rebased PR 3765 plus few changes Bug fix: yes Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: - Todo: - #3765 was right but was made in master. Cherry-picked and rebased for 2.0. Tests are passing.
This commit is contained in:
commit
48af0ba722
@ -155,7 +155,7 @@ class FullTransformer
|
|||||||
}
|
}
|
||||||
|
|
||||||
// behave like the intl extension
|
// behave like the intl extension
|
||||||
StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR);
|
StubIntl::setError(StubIntl::U_PARSE_ERROR, 'Date parsing failed');
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -292,7 +292,7 @@ class FullTransformer
|
|||||||
|
|
||||||
// If month is false, return immediately (intl behavior)
|
// If month is false, return immediately (intl behavior)
|
||||||
if (false === $month) {
|
if (false === $month) {
|
||||||
StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR);
|
StubIntl::setError(StubIntl::U_PARSE_ERROR, 'Date parsing failed');
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -45,20 +45,9 @@ abstract class StubIntl
|
|||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
private static $errorCodes = array(
|
private static $errorCodes = array(
|
||||||
self::U_ZERO_ERROR,
|
|
||||||
self::U_ILLEGAL_ARGUMENT_ERROR,
|
|
||||||
self::U_PARSE_ERROR,
|
|
||||||
);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The error messages of all known error codes
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
private static $errorMessages = array(
|
|
||||||
self::U_ZERO_ERROR => 'U_ZERO_ERROR',
|
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_ILLEGAL_ARGUMENT_ERROR => 'U_ILLEGAL_ARGUMENT_ERROR',
|
||||||
self::U_PARSE_ERROR => 'Date parsing failed: U_PARSE_ERROR',
|
self::U_PARSE_ERROR => 'U_PARSE_ERROR',
|
||||||
);
|
);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -68,6 +57,13 @@ abstract class StubIntl
|
|||||||
*/
|
*/
|
||||||
private static $errorCode = self::U_ZERO_ERROR;
|
private static $errorCode = self::U_ZERO_ERROR;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The error code of the last operation
|
||||||
|
*
|
||||||
|
* @var integer
|
||||||
|
*/
|
||||||
|
private static $errorMessage = 'U_ZERO_ERROR';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns whether the error code indicates a failure
|
* Returns whether the error code indicates a failure
|
||||||
*
|
*
|
||||||
@ -77,8 +73,8 @@ abstract class StubIntl
|
|||||||
*/
|
*/
|
||||||
static public function isFailure($errorCode)
|
static public function isFailure($errorCode)
|
||||||
{
|
{
|
||||||
return in_array($errorCode, self::$errorCodes, true)
|
return isset(self::$errorCodes[$errorCode])
|
||||||
&& $errorCode !== self::U_ZERO_ERROR;
|
&& $errorCode > self::U_ZERO_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,22 +98,24 @@ abstract class StubIntl
|
|||||||
*/
|
*/
|
||||||
static public function getErrorMessage()
|
static public function getErrorMessage()
|
||||||
{
|
{
|
||||||
return self::$errorMessages[self::$errorCode];
|
return self::$errorMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the current error code
|
* Sets the current error
|
||||||
*
|
*
|
||||||
* @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
|
* @throws \InvalidArgumentException If the code is not one of the error constants in this class
|
||||||
*/
|
*/
|
||||||
static public function setErrorCode($code)
|
static public function setError($code, $message = '')
|
||||||
{
|
{
|
||||||
if (!isset(self::$errorMessages[$code])) {
|
if (!isset(self::$errorCodes[$code])) {
|
||||||
throw new \InvalidArgumentException(sprintf('No such error code: "%s"', $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;
|
self::$errorCode = $code;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -26,14 +26,18 @@ use Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedExceptio
|
|||||||
class StubIntlDateFormatter
|
class StubIntlDateFormatter
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Constants defined by the intl extension, not class constants in IntlDateFormatter
|
* The error code from the last operation
|
||||||
* TODO: remove if the Form component drop the call to the intl_is_failure() function
|
|
||||||
*
|
*
|
||||||
* @see StubIntlDateFormatter::getErrorCode()
|
* @var integer
|
||||||
* @see StubIntlDateFormatter::getErrorMessage()
|
|
||||||
*/
|
*/
|
||||||
const U_ZERO_ERROR = 0;
|
protected $errorCode = StubIntl::U_ZERO_ERROR;
|
||||||
const U_ZERO_ERROR_MESSAGE = 'U_ZERO_ERROR';
|
|
||||||
|
/**
|
||||||
|
* The error message from the last operation
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $errorMessage = 'U_ZERO_ERROR';
|
||||||
|
|
||||||
/* date/time format types */
|
/* date/time format types */
|
||||||
const NONE = -1;
|
const NONE = -1;
|
||||||
@ -176,7 +180,7 @@ class StubIntlDateFormatter
|
|||||||
|
|
||||||
// behave like the intl extension
|
// behave like the intl extension
|
||||||
if (!is_int($timestamp) && version_compare(\PHP_VERSION, '5.3.4', '<')) {
|
if (!is_int($timestamp) && version_compare(\PHP_VERSION, '5.3.4', '<')) {
|
||||||
StubIntl::setErrorCode(StubIntl::U_ILLEGAL_ARGUMENT_ERROR);
|
StubIntl::setError(StubIntl::U_ILLEGAL_ARGUMENT_ERROR, 'datefmt_format: takes either an array or an integer timestamp value ');
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -225,7 +229,7 @@ class StubIntlDateFormatter
|
|||||||
*/
|
*/
|
||||||
public function getErrorCode()
|
public function getErrorCode()
|
||||||
{
|
{
|
||||||
return self::U_ZERO_ERROR;
|
return $this->errorCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -237,7 +241,7 @@ class StubIntlDateFormatter
|
|||||||
*/
|
*/
|
||||||
public function getErrorMessage()
|
public function getErrorMessage()
|
||||||
{
|
{
|
||||||
return self::U_ZERO_ERROR_MESSAGE;
|
return $this->errorMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -350,12 +354,17 @@ class StubIntlDateFormatter
|
|||||||
throw new MethodArgumentNotImplementedException(__METHOD__, 'position');
|
throw new MethodArgumentNotImplementedException(__METHOD__, 'position');
|
||||||
}
|
}
|
||||||
|
|
||||||
StubIntl::setErrorCode(StubIntl::U_ZERO_ERROR);
|
|
||||||
|
|
||||||
$dateTime = $this->createDateTime(0);
|
$dateTime = $this->createDateTime(0);
|
||||||
$transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId());
|
$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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -24,32 +24,19 @@ use Symfony\Component\Locale\Exception\MethodArgumentValueNotImplementedExceptio
|
|||||||
*/
|
*/
|
||||||
class StubNumberFormatter
|
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
|
|
||||||
*
|
|
||||||
* @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
|
* The error code from the last operation
|
||||||
*
|
*
|
||||||
* @var integer
|
* @var integer
|
||||||
*/
|
*/
|
||||||
protected $errorCode = self::U_ZERO_ERROR;
|
protected $errorCode = StubIntl::U_ZERO_ERROR;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The error message from the last operation
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $errorMessage = 'U_ZERO_ERROR';
|
||||||
|
|
||||||
/** Format style constants */
|
/** Format style constants */
|
||||||
const PATTERN_DECIMAL = 0;
|
const PATTERN_DECIMAL = 0;
|
||||||
@ -403,7 +390,7 @@ class StubNumberFormatter
|
|||||||
*/
|
*/
|
||||||
public function getErrorMessage()
|
public function getErrorMessage()
|
||||||
{
|
{
|
||||||
return $this->errorMessages[$this->errorCode];
|
return $this->errorMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -514,7 +501,9 @@ class StubNumberFormatter
|
|||||||
|
|
||||||
// Any string before the numeric value causes error in the parsing
|
// Any string before the numeric value causes error in the parsing
|
||||||
if (isset($matches[1]) && !empty($matches[1])) {
|
if (isset($matches[1]) && !empty($matches[1])) {
|
||||||
$this->errorCode = self::U_PARSE_ERROR;
|
StubIntl::setError(StubIntl::U_PARSE_ERROR);
|
||||||
|
$this->errorCode = StubIntl::getErrorCode();
|
||||||
|
$this->errorMessage = StubIntl::getErrorMessage();
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@ -63,6 +63,9 @@ class StubIntlDateFormatterTest extends LocaleTestCase
|
|||||||
$this->assertSame($errorMessage, StubIntl::getErrorMessage());
|
$this->assertSame($errorMessage, StubIntl::getErrorMessage());
|
||||||
$this->assertSame($errorCode, StubIntl::getErrorCode());
|
$this->assertSame($errorCode, StubIntl::getErrorCode());
|
||||||
$this->assertSame($errorCode != 0, StubIntl::isFailure(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()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -482,13 +485,13 @@ class StubIntlDateFormatterTest extends LocaleTestCase
|
|||||||
public function testGetErrorCode()
|
public function testGetErrorCode()
|
||||||
{
|
{
|
||||||
$formatter = $this->createStubFormatter();
|
$formatter = $this->createStubFormatter();
|
||||||
$this->assertEquals(StubIntlDateFormatter::U_ZERO_ERROR, $formatter->getErrorCode());
|
$this->assertEquals(StubIntl::getErrorCode(), $formatter->getErrorCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetErrorMessage()
|
public function testGetErrorMessage()
|
||||||
{
|
{
|
||||||
$formatter = $this->createStubFormatter();
|
$formatter = $this->createStubFormatter();
|
||||||
$this->assertEquals(StubIntlDateFormatter::U_ZERO_ERROR_MESSAGE, $formatter->getErrorMessage());
|
$this->assertEquals(StubIntl::getErrorMessage(), $formatter->getErrorMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetLocale()
|
public function testGetLocale()
|
||||||
@ -556,6 +559,9 @@ class StubIntlDateFormatterTest extends LocaleTestCase
|
|||||||
$this->assertSame($errorMessage, StubIntl::getErrorMessage());
|
$this->assertSame($errorMessage, StubIntl::getErrorMessage());
|
||||||
$this->assertSame($errorCode, StubIntl::getErrorCode());
|
$this->assertSame($errorCode, StubIntl::getErrorCode());
|
||||||
$this->assertSame($errorCode != 0, StubIntl::isFailure(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()
|
public function parseProvider()
|
||||||
@ -711,7 +717,7 @@ class StubIntlDateFormatterTest extends LocaleTestCase
|
|||||||
|
|
||||||
$this->skipIfIntlExtensionIsNotLoaded();
|
$this->skipIfIntlExtensionIsNotLoaded();
|
||||||
$formatter = $this->createIntlFormatter($pattern);
|
$formatter = $this->createIntlFormatter($pattern);
|
||||||
$this->assertSame(false, $formatter->parse($value));
|
$this->assertFalse($formatter->parse($value));
|
||||||
$this->assertSame($errorMessage, intl_get_error_message());
|
$this->assertSame($errorMessage, intl_get_error_message());
|
||||||
$this->assertSame($errorCode, intl_get_error_code());
|
$this->assertSame($errorCode, intl_get_error_code());
|
||||||
$this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code()));
|
$this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code()));
|
||||||
@ -726,10 +732,13 @@ class StubIntlDateFormatterTest extends LocaleTestCase
|
|||||||
$errorMessage = 'Date parsing failed: U_PARSE_ERROR';
|
$errorMessage = 'Date parsing failed: U_PARSE_ERROR';
|
||||||
|
|
||||||
$formatter = $this->createStubFormatter($pattern);
|
$formatter = $this->createStubFormatter($pattern);
|
||||||
$this->assertSame(false, $formatter->parse($value));
|
$this->assertFalse($formatter->parse($value));
|
||||||
$this->assertSame($errorMessage, StubIntl::getErrorMessage());
|
$this->assertSame($errorMessage, StubIntl::getErrorMessage());
|
||||||
$this->assertSame($errorCode, StubIntl::getErrorCode());
|
$this->assertSame($errorCode, StubIntl::getErrorCode());
|
||||||
$this->assertSame($errorCode != 0, StubIntl::isFailure(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()
|
public function parseErrorProvider()
|
||||||
|
@ -14,6 +14,7 @@ namespace Symfony\Tests\Component\Locale\Stub;
|
|||||||
require_once __DIR__.'/../TestCase.php';
|
require_once __DIR__.'/../TestCase.php';
|
||||||
|
|
||||||
use Symfony\Component\Locale\Locale;
|
use Symfony\Component\Locale\Locale;
|
||||||
|
use Symfony\Component\Locale\Stub\StubIntl;
|
||||||
use Symfony\Component\Locale\Stub\StubNumberFormatter;
|
use Symfony\Component\Locale\Stub\StubNumberFormatter;
|
||||||
use Symfony\Tests\Component\Locale\TestCase as LocaleTestCase;
|
use Symfony\Tests\Component\Locale\TestCase as LocaleTestCase;
|
||||||
|
|
||||||
@ -613,7 +614,7 @@ class StubNumberFormatterTest extends LocaleTestCase
|
|||||||
public function testGetErrorCode()
|
public function testGetErrorCode()
|
||||||
{
|
{
|
||||||
$formatter = $this->getStubFormatterWithDecimalStyle();
|
$formatter = $this->getStubFormatterWithDecimalStyle();
|
||||||
$this->assertEquals(StubNumberFormatter::U_ZERO_ERROR, $formatter->getErrorCode());
|
$this->assertEquals(StubIntl::U_ZERO_ERROR, $formatter->getErrorCode());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGetLocale()
|
public function testGetLocale()
|
||||||
@ -668,9 +669,9 @@ class StubNumberFormatterTest extends LocaleTestCase
|
|||||||
$this->assertSame($expected, $parsedValue, $message);
|
$this->assertSame($expected, $parsedValue, $message);
|
||||||
|
|
||||||
if ($expected === false) {
|
if ($expected === false) {
|
||||||
$this->assertSame($formatter::U_PARSE_ERROR, $formatter->getErrorCode());
|
$this->assertSame(StubIntl::U_PARSE_ERROR, $formatter->getErrorCode());
|
||||||
} else {
|
} else {
|
||||||
$this->assertEquals($formatter::U_ZERO_ERROR, $formatter->getErrorCode());
|
$this->assertEquals(StubIntl::U_ZERO_ERROR, $formatter->getErrorCode());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user