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:
commit
56d3c264a9
@ -140,6 +140,10 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface
|
|||||||
throw new TransformationFailedException('I don\'t have a clear idea what infinity looks like');
|
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)) {
|
if (function_exists('mb_detect_encoding') && false !== $encoding = mb_detect_encoding($value, null, true)) {
|
||||||
$length = mb_strlen($value, $encoding);
|
$length = mb_strlen($value, $encoding);
|
||||||
$remainder = mb_substr($value, $position, $length, $encoding);
|
$remainder = mb_substr($value, $position, $length, $encoding);
|
||||||
|
@ -434,10 +434,17 @@ class NumberToLocalizedStringTransformerTest extends \PHPUnit_Framework_TestCase
|
|||||||
$transformer->reverseTransform("12\xc2\xa0345,678foo");
|
$transformer->reverseTransform("12\xc2\xa0345,678foo");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testReverseTransformBigint()
|
public function testReverseTransformBigInt()
|
||||||
{
|
{
|
||||||
$transformer = new NumberToLocalizedStringTransformer(null, true);
|
$transformer = new NumberToLocalizedStringTransformer(null, true);
|
||||||
|
|
||||||
$this->assertEquals(PHP_INT_MAX - 1, (int) $transformer->reverseTransform((string) (PHP_INT_MAX - 1)));
|
$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'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user