Skip to content

Commit bb61e09

Browse files
stealth35eriksencosta
authored andcommitted
[Locale] use the correct way for Intl error
1 parent 0078faa commit bb61e09

File tree

6 files changed

+64
-58
lines changed

6 files changed

+64
-58
lines changed

src/Symfony/Component/Locale/Stub/DateFormat/FullTransformer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function parse(\DateTime $dateTime, $value)
155155
}
156156

157157
// behave like the intl extension
158-
StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR);
158+
StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR, 'Date parsing failed');
159159

160160
return false;
161161
}
@@ -292,7 +292,7 @@ protected function calculateUnixTimestamp(\DateTime $dateTime, array $options)
292292

293293
// If month is false, return immediately (intl behavior)
294294
if (false === $month) {
295-
StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR);
295+
StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR, 'Date parsing failed');
296296

297297
return false;
298298
}

src/Symfony/Component/Locale/Stub/StubIntl.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,28 +45,24 @@ abstract class StubIntl
4545
* @var array
4646
*/
4747
private static $errorCodes = array(
48-
self::U_ZERO_ERROR,
49-
self::U_ILLEGAL_ARGUMENT_ERROR,
50-
self::U_PARSE_ERROR,
48+
self::U_ZERO_ERROR => 'U_ZERO_ERROR',
49+
self::U_ILLEGAL_ARGUMENT_ERROR => 'U_ILLEGAL_ARGUMENT_ERROR',
50+
self::U_PARSE_ERROR => 'U_PARSE_ERROR',
5151
);
5252

5353
/**
54-
* The error messages of all known error codes
54+
* The error code of the last operation
5555
*
56-
* @var array
56+
* @var integer
5757
*/
58-
private static $errorMessages = array(
59-
self::U_ZERO_ERROR => 'U_ZERO_ERROR',
60-
self::U_ILLEGAL_ARGUMENT_ERROR => 'datefmt_format: takes either an array or an integer timestamp value : U_ILLEGAL_ARGUMENT_ERROR',
61-
self::U_PARSE_ERROR => 'Date parsing failed: U_PARSE_ERROR',
62-
);
58+
private static $errorCode = self::U_ZERO_ERROR;
6359

6460
/**
6561
* The error code of the last operation
6662
*
6763
* @var integer
6864
*/
69-
private static $errorCode = self::U_ZERO_ERROR;
65+
private static $errorMessage = 'U_ZERO_ERROR';
7066

7167
/**
7268
* Returns whether the error code indicates a failure
@@ -77,8 +73,8 @@ abstract class StubIntl
7773
*/
7874
static public function isFailure($errorCode)
7975
{
80-
return in_array($errorCode, self::$errorCodes, true)
81-
&& $errorCode !== self::U_ZERO_ERROR;
76+
return array_key_exists($errorCode, self::$errorCodes)
77+
&& $errorCode > self::U_ZERO_ERROR;
8278
}
8379

8480
/**
@@ -102,22 +98,24 @@ static public function getErrorCode()
10298
*/
10399
static public function getErrorMessage()
104100
{
105-
return self::$errorMessages[self::$errorCode];
101+
return self::$errorMessage;
106102
}
107103

108104
/**
109105
* Sets the current error code
110106
*
111-
* @param integer $code One of the error constants in this class
107+
* @param integer $code One of the error constants in this class
108+
* @param string $message The ICU class error message
112109
*
113110
* @throws \InvalidArgumentException If the code is not one of the error constants in this class
114111
*/
115-
static public function setErrorCode($code)
112+
static public function setErrorCode($code, $message = '')
116113
{
117-
if (!isset(self::$errorMessages[$code])) {
114+
if (!isset(self::$errorCodes[$code])) {
118115
throw new \InvalidArgumentException(sprintf('No such error code: "%s"', $code));
119116
}
120117

118+
self::$errorMessage = $message ? sprintf('%s: %s', $message, self::$errorCodes[$code]) : self::$errorCodes[$code];
121119
self::$errorCode = $code;
122120
}
123121
}

src/Symfony/Component/Locale/Stub/StubIntlDateFormatter.php

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,18 @@
2626
class StubIntlDateFormatter
2727
{
2828
/**
29-
* Constants defined by the intl extension, not class constants in IntlDateFormatter
30-
* TODO: remove if the Form component drop the call to the intl_is_failure() function
29+
* The error code from the last operation
3130
*
32-
* @see StubIntlDateFormatter::getErrorCode()
33-
* @see StubIntlDateFormatter::getErrorMessage()
31+
* @var integer
3432
*/
35-
const U_ZERO_ERROR = 0;
36-
const U_ZERO_ERROR_MESSAGE = 'U_ZERO_ERROR';
33+
protected $errorCode = StubIntl::U_ZERO_ERROR;
34+
35+
/**
36+
* The error message from the last operation
37+
*
38+
* @var string
39+
*/
40+
protected $errorMessage = 'U_ZERO_ERROR';
3741

3842
/* date/time format types */
3943
const NONE = -1;
@@ -225,7 +229,7 @@ public function getDateType()
225229
*/
226230
public function getErrorCode()
227231
{
228-
return self::U_ZERO_ERROR;
232+
return $this->errorCode;
229233
}
230234

231235
/**
@@ -237,7 +241,7 @@ public function getErrorCode()
237241
*/
238242
public function getErrorMessage()
239243
{
240-
return self::U_ZERO_ERROR_MESSAGE;
244+
return $this->errorMessage;
241245
}
242246

243247
/**
@@ -350,12 +354,17 @@ public function parse($value, &$position = null)
350354
throw new MethodArgumentNotImplementedException(__METHOD__, 'position');
351355
}
352356

353-
StubIntl::setErrorCode(StubIntl::U_ZERO_ERROR);
354-
355357
$dateTime = $this->createDateTime(0);
356358
$transformer = new FullTransformer($this->getPattern(), $this->getTimeZoneId());
357359

358-
return $transformer->parse($dateTime, $value);
360+
$timestamp = $transformer->parse($dateTime, $value);
361+
362+
if (false === $timestamp) {
363+
$this->errorCode = StubIntl::getErrorCode();
364+
$this->errorMessage = StubIntl::getErrorMessage();
365+
}
366+
367+
return $timestamp;
359368
}
360369

361370
/**

src/Symfony/Component/Locale/Stub/StubNumberFormatter.php

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,31 +25,18 @@
2525
class StubNumberFormatter
2626
{
2727
/**
28-
* Constants defined by the intl extension, not class constants in NumberFormatter
29-
* TODO: remove if the Form component drop the call to the intl_is_failure() function
30-
*
31-
* @see StubNumberFormatter::getErrorCode()
32-
* @see StubNumberFormatter::getErrorMessage()
33-
*/
34-
const U_ZERO_ERROR = 0;
35-
const U_PARSE_ERROR = 9;
36-
37-
/**
38-
* The error messages for each error code
28+
* The error code from the last operation
3929
*
40-
* @var array
30+
* @var integer
4131
*/
42-
protected $errorMessages = array(
43-
self::U_ZERO_ERROR => 'U_ZERO_ERROR',
44-
self::U_PARSE_ERROR => 'Number parsing failed: U_PARSE_ERROR',
45-
);
32+
protected $errorCode = StubIntl::U_ZERO_ERROR;
4633

4734
/**
48-
* The error code from the last operation
35+
* The error message from the last operation
4936
*
50-
* @var integer
37+
* @var string
5138
*/
52-
protected $errorCode = self::U_ZERO_ERROR;
39+
protected $errorMessage = 'U_ZERO_ERROR';
5340

5441
/** Format style constants */
5542
const PATTERN_DECIMAL = 0;
@@ -403,7 +390,7 @@ public function getErrorCode()
403390
*/
404391
public function getErrorMessage()
405392
{
406-
return $this->errorMessages[$this->errorCode];
393+
return $this->errorMessage;
407394
}
408395

409396
/**
@@ -514,7 +501,9 @@ public function parse($value, $type = self::TYPE_DOUBLE, &$position = null)
514501

515502
// Any string before the numeric value causes error in the parsing
516503
if (isset($matches[1]) && !empty($matches[1])) {
517-
$this->errorCode = self::U_PARSE_ERROR;
504+
StubIntl::setErrorCode(StubIntl::U_PARSE_ERROR);
505+
$this->errorCode = StubIntl::getErrorCode();
506+
$this->errorMessage = StubIntl::getErrorMessage();
518507

519508
return false;
520509
}

tests/Symfony/Tests/Component/Locale/Stub/StubIntlDateFormatterTest.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public function testFormatStub($pattern, $timestamp, $expected, $errorCode = 0,
6363
$this->assertSame($errorMessage, StubIntl::getErrorMessage());
6464
$this->assertSame($errorCode, StubIntl::getErrorCode());
6565
$this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode()));
66+
$this->assertSame($errorMessage, $formatter->getErrorMessage());
67+
$this->assertSame($errorCode, $formatter->getErrorCode());
68+
$this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode()));
6669
}
6770

6871
/**
@@ -482,13 +485,13 @@ public function testGetDateType()
482485
public function testGetErrorCode()
483486
{
484487
$formatter = $this->createStubFormatter();
485-
$this->assertEquals(StubIntlDateFormatter::U_ZERO_ERROR, $formatter->getErrorCode());
488+
$this->assertEquals(StubIntl::getErrorCode(), $formatter->getErrorCode());
486489
}
487490

488491
public function testGetErrorMessage()
489492
{
490493
$formatter = $this->createStubFormatter();
491-
$this->assertEquals(StubIntlDateFormatter::U_ZERO_ERROR_MESSAGE, $formatter->getErrorMessage());
494+
$this->assertEquals(StubIntl::getErrorMessage(), $formatter->getErrorMessage());
492495
}
493496

494497
public function testGetLocale()
@@ -556,6 +559,9 @@ public function testParseStub($pattern, $value, $expected)
556559
$this->assertSame($errorMessage, StubIntl::getErrorMessage());
557560
$this->assertSame($errorCode, StubIntl::getErrorCode());
558561
$this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode()));
562+
$this->assertSame($errorMessage, $formatter->getErrorMessage());
563+
$this->assertSame($errorCode, $formatter->getErrorCode());
564+
$this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode()));
559565
}
560566

561567
public function parseProvider()
@@ -711,7 +717,7 @@ public function testParseErrorIntl($pattern, $value)
711717

712718
$this->skipIfIntlExtensionIsNotLoaded();
713719
$formatter = $this->createIntlFormatter($pattern);
714-
$this->assertSame(false, $formatter->parse($value));
720+
$this->assertFalse($formatter->parse($value));
715721
$this->assertSame($errorMessage, intl_get_error_message());
716722
$this->assertSame($errorCode, intl_get_error_code());
717723
$this->assertSame($errorCode != 0, intl_is_failure(intl_get_error_code()));
@@ -726,10 +732,13 @@ public function testParseErrorStub($pattern, $value)
726732
$errorMessage = 'Date parsing failed: U_PARSE_ERROR';
727733

728734
$formatter = $this->createStubFormatter($pattern);
729-
$this->assertSame(false, $formatter->parse($value));
735+
$this->assertFalse($formatter->parse($value));
730736
$this->assertSame($errorMessage, StubIntl::getErrorMessage());
731737
$this->assertSame($errorCode, StubIntl::getErrorCode());
732738
$this->assertSame($errorCode != 0, StubIntl::isFailure(StubIntl::getErrorCode()));
739+
$this->assertSame($errorMessage, $formatter->getErrorMessage());
740+
$this->assertSame($errorCode, $formatter->getErrorCode());
741+
$this->assertSame($errorCode != 0, StubIntl::isFailure($formatter->getErrorCode()));
733742
}
734743

735744
public function parseErrorProvider()

tests/Symfony/Tests/Component/Locale/Stub/StubNumberFormatterTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
require_once __DIR__.'/../TestCase.php';
1515

1616
use Symfony\Component\Locale\Locale;
17+
use Symfony\Component\Locale\Stub\StubIntl;
1718
use Symfony\Component\Locale\Stub\StubNumberFormatter;
1819
use Symfony\Tests\Component\Locale\TestCase as LocaleTestCase;
1920

@@ -613,7 +614,7 @@ public function formatRoundingModeRoundHalfEvenProvider()
613614
public function testGetErrorCode()
614615
{
615616
$formatter = $this->getStubFormatterWithDecimalStyle();
616-
$this->assertEquals(StubNumberFormatter::U_ZERO_ERROR, $formatter->getErrorCode());
617+
$this->assertEquals(StubIntl::U_ZERO_ERROR, $formatter->getErrorCode());
617618
}
618619

619620
public function testGetLocale()
@@ -668,9 +669,9 @@ public function testParseStub($value, $expected, $message = '')
668669
$this->assertSame($expected, $parsedValue, $message);
669670

670671
if ($expected === false) {
671-
$this->assertSame($formatter::U_PARSE_ERROR, $formatter->getErrorCode());
672+
$this->assertSame(StubIntl::U_PARSE_ERROR, $formatter->getErrorCode());
672673
} else {
673-
$this->assertEquals($formatter::U_ZERO_ERROR, $formatter->getErrorCode());
674+
$this->assertEquals(StubIntl::U_ZERO_ERROR, $formatter->getErrorCode());
674675
}
675676
}
676677

0 commit comments

Comments
 (0)