don't lose int precision with not needed type casts

This commit is contained in:
Christian Flothmann 2019-02-02 11:09:54 +01:00
parent 205b0ba2cc
commit 72136f1c04
3 changed files with 62 additions and 3 deletions

View File

@ -44,4 +44,12 @@ class IntegerToLocalizedStringTransformer extends NumberToLocalizedStringTransfo
return null !== $result ? (int) $result : null;
}
/**
* @internal
*/
protected function castParsedValue($value)
{
return $value;
}
}

View File

@ -181,9 +181,7 @@ 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;
}
$result = $this->castParsedValue($result);
if (false !== $encoding = mb_detect_encoding($value, null, true)) {
$length = mb_strlen($value, $encoding);
@ -228,6 +226,18 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface
return $formatter;
}
/**
* @internal
*/
protected function castParsedValue($value)
{
if (\is_int($value) && $value === (int) $float = (float) $value) {
return $float;
}
return $value;
}
/**
* Rounds a number according to the configured scale and rounding mode.
*

View File

@ -50,4 +50,45 @@ class IntegerTypeTest extends BaseTypeTest
$this->assertSame($expectedData, $form->getNormData());
$this->assertSame($expectedData, $form->getData());
}
public function testSubmittedLargeIntegersAreNotCastToFloat()
{
if (4 === \PHP_INT_SIZE) {
$this->markTestSkipped('This test requires a 64bit PHP.');
}
$form = $this->factory->create(static::TESTED_TYPE);
$form->submit('201803221011791');
$this->assertSame(201803221011791, $form->getData());
$this->assertSame('201803221011791', $form->getViewData());
}
public function testTooSmallIntegersAreNotValid()
{
if (4 === \PHP_INT_SIZE) {
$min = '-2147483649';
} else {
$min = '-9223372036854775808';
}
$form = $this->factory->create(static::TESTED_TYPE);
$form->submit($min);
$this->assertFalse($form->isSynchronized());
}
public function testTooGreatIntegersAreNotValid()
{
if (4 === \PHP_INT_SIZE) {
$max = '2147483648';
} else {
$max = '9223372036854775808';
}
$form = $this->factory->create(static::TESTED_TYPE);
$form->submit($max);
$this->assertFalse($form->isSynchronized());
}
}