merged branch bschussek/issue5707 (PR #5943)
This PR was merged into the master branch.
Commits
-------
c16d6bf
[Form] Fixed NumberToLocalizedStringTransformer to throw an exception if numbers contain illegal characters
Discussion
----------
[Form] Fixed NumberType to fail if numbers contain letters
Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: #5707
Todo: -
License of the code: MIT
Documentation PR: -
This commit is contained in:
commit
53f5596b77
@ -107,18 +107,35 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface
|
||||
throw new TransformationFailedException('"NaN" is not a valid number');
|
||||
}
|
||||
|
||||
$position = 0;
|
||||
$formatter = $this->getNumberFormatter();
|
||||
$value = $formatter->parse($value);
|
||||
$result = $formatter->parse($value, \NumberFormatter::TYPE_DOUBLE, $position);
|
||||
|
||||
if (intl_is_failure($formatter->getErrorCode())) {
|
||||
throw new TransformationFailedException($formatter->getErrorMessage());
|
||||
}
|
||||
|
||||
if ($value >= INF || $value <= -INF) {
|
||||
if ($result >= INF || $result <= -INF) {
|
||||
throw new TransformationFailedException('I don\'t have a clear idea what infinity looks like');
|
||||
}
|
||||
|
||||
return $value;
|
||||
// After parsing, position holds the index of the character where the
|
||||
// parsing stopped
|
||||
if ($position < strlen($value)) {
|
||||
// Check if there are unrecognized characters at the end of the
|
||||
// number
|
||||
$remainder = substr($value, $position);
|
||||
|
||||
// Remove all whitespace characters
|
||||
if ('' !== preg_replace('/[\s\xc2\xa0]*/', '', $remainder)) {
|
||||
throw new TransformationFailedException(
|
||||
sprintf('The number contains unrecognized characters: "%s"',
|
||||
$remainder
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -93,7 +93,7 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
|
||||
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
|
||||
*/
|
||||
public function testTransformExpectsNumeric()
|
||||
{
|
||||
@ -103,7 +103,7 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
|
||||
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
|
||||
*/
|
||||
public function testReverseTransformExpectsString()
|
||||
{
|
||||
@ -113,7 +113,7 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Form\Exception\TransformationFailedException
|
||||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||
*/
|
||||
public function testReverseTransformExpectsValidNumber()
|
||||
{
|
||||
@ -123,7 +123,7 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Form\Exception\TransformationFailedException
|
||||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||
* @link https://github.com/symfony/symfony/issues/3161
|
||||
*/
|
||||
public function testReverseTransformDisallowsNaN()
|
||||
@ -134,7 +134,7 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Form\Exception\TransformationFailedException
|
||||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||
*/
|
||||
public function testReverseTransformDisallowsNaN2()
|
||||
{
|
||||
@ -144,7 +144,7 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Form\Exception\TransformationFailedException
|
||||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||
*/
|
||||
public function testReverseTransformDisallowsInfinity()
|
||||
{
|
||||
@ -154,7 +154,7 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Form\Exception\TransformationFailedException
|
||||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||
*/
|
||||
public function testReverseTransformDisallowsInfinity2()
|
||||
{
|
||||
@ -164,7 +164,7 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Form\Exception\TransformationFailedException
|
||||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||
*/
|
||||
public function testReverseTransformDisallowsNegativeInfinity()
|
||||
{
|
||||
@ -172,4 +172,34 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
|
||||
|
||||
$transformer->reverseTransform('-∞');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||
*/
|
||||
public function testReverseTransformDisallowsLeadingExtraCharacters()
|
||||
{
|
||||
$transformer = new NumberToLocalizedStringTransformer();
|
||||
|
||||
$transformer->reverseTransform('foo123');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||
*/
|
||||
public function testReverseTransformDisallowsCenteredExtraCharacters()
|
||||
{
|
||||
$transformer = new NumberToLocalizedStringTransformer();
|
||||
|
||||
$transformer->reverseTransform('12foo3');
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
|
||||
*/
|
||||
public function testReverseTransformDisallowsTrailingExtraCharacters()
|
||||
{
|
||||
$transformer = new NumberToLocalizedStringTransformer();
|
||||
|
||||
$transformer->reverseTransform('123foo');
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user