Skip to content

Commit

Permalink
Set error code when number cannot be parsed. Fixes #2389
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtorrent committed Oct 30, 2011
1 parent 68b7662 commit 89cd64a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
24 changes: 21 additions & 3 deletions src/Symfony/Component/Locale/Stub/StubNumberFormatter.php
Expand Up @@ -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;
Expand Down Expand Up @@ -346,7 +363,7 @@ public function getAttribute($attr)
*/
public function getErrorCode()
{
return self::U_ZERO_ERROR;
return $this->errorCode;
}

/**
Expand All @@ -357,7 +374,7 @@ public function getErrorCode()
*/
public function getErrorMessage()
{
return self::U_ZERO_ERROR_MESSAGE;
return $this->errorMessages[$this->errorCode];
}

/**
Expand Down Expand Up @@ -458,6 +475,7 @@ 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;
return false;
}

Expand Down
Expand Up @@ -616,12 +616,6 @@ public function testGetErrorCode()
$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();
Expand Down Expand Up @@ -672,6 +666,12 @@ public function testParseStub($value, $expected, $message = '')
$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());
}
}

/**
Expand All @@ -684,6 +684,12 @@ public function testParseIntl($value, $expected, $message = '')
$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()
Expand Down

0 comments on commit 89cd64a

Please sign in to comment.