bug #18358 [Form] NumberToLocalizedStringTransformer should return floats when possible (nicolas-grekas)

This PR was merged into the 2.3 branch.

Discussion
----------

[Form] NumberToLocalizedStringTransformer should return floats when possible

| Q             | A
| ------------- | ---
| Branch?       | 2.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #18346
| License       | MIT
| Doc PR        | -

Commits
-------

f5ed09c [Form] NumberToLocalizedStringTransformer should return floats when possible
This commit is contained in:
Fabien Potencier 2016-03-30 15:48:39 +02:00
commit 56d3c264a9
2 changed files with 12 additions and 1 deletions

View File

@ -140,6 +140,10 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface
throw new TransformationFailedException('I don\'t have a clear idea what infinity looks like');
}
if (is_int($result) && $result === (int) $float = (float) $result) {
$result = $float;
}
if (function_exists('mb_detect_encoding') && false !== $encoding = mb_detect_encoding($value, null, true)) {
$length = mb_strlen($value, $encoding);
$remainder = mb_substr($value, $position, $length, $encoding);

View File

@ -434,10 +434,17 @@ class NumberToLocalizedStringTransformerTest extends \PHPUnit_Framework_TestCase
$transformer->reverseTransform("12\xc2\xa0345,678foo");
}
public function testReverseTransformBigint()
public function testReverseTransformBigInt()
{
$transformer = new NumberToLocalizedStringTransformer(null, true);
$this->assertEquals(PHP_INT_MAX - 1, (int) $transformer->reverseTransform((string) (PHP_INT_MAX - 1)));
}
public function testReverseTransformSmallInt()
{
$transformer = new NumberToLocalizedStringTransformer(null, true);
$this->assertSame(1.0, $transformer->reverseTransform('1'));
}
}