[Form] Fixed NumberToLocalizedStringTransformer to accept both comma and dot as decimal separator, if possible

This commit is contained in:
Bernhard Schussek 2012-11-08 14:58:55 +01:00
parent 5c57bbb435
commit dc80385070
2 changed files with 104 additions and 3 deletions

View File

@ -104,6 +104,17 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface
}
$formatter = $this->getNumberFormatter();
$groupSep = $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
$decSep = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
if ('.' !== $decSep && (!$this->grouping || '.' !== $groupSep)) {
$value = str_replace('.', $decSep, $value);
}
if (',' !== $decSep && (!$this->grouping || ',' !== $groupSep)) {
$value = str_replace(',', $decSep, $value);
}
$value = $formatter->parse($value);
if (intl_is_failure($formatter->getErrorCode())) {

View File

@ -88,14 +88,104 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
{
$transformer = new NumberToLocalizedStringTransformer(null, true);
// completely valid format
$this->assertEquals(1234.5, $transformer->reverseTransform('1.234,5'));
$this->assertEquals(12345.912, $transformer->reverseTransform('12.345,912'));
// omit group separator
$this->assertEquals(1234.5, $transformer->reverseTransform('1234,5'));
$this->assertEquals(12345.912, $transformer->reverseTransform('12345,912'));
}
public function testDecimalSeparatorMayBeDotIfGroupingSeparatorIsNotDot()
{
\Locale::setDefault('fr');
$transformer = new NumberToLocalizedStringTransformer(null, true);
// completely valid format
$this->assertEquals(1234.5, $transformer->reverseTransform('1 234,5'));
// accept dots
$this->assertEquals(1234.5, $transformer->reverseTransform('1 234.5'));
// omit group separator
$this->assertEquals(1234.5, $transformer->reverseTransform('1234,5'));
$this->assertEquals(1234.5, $transformer->reverseTransform('1234.5'));
}
/**
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testDecimalSeparatorMayNotBeDotIfGroupingSeparatorIsDot()
{
$transformer = new NumberToLocalizedStringTransformer(null, true);
$transformer->reverseTransform('1.234.5');
}
/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testDecimalSeparatorMayNotBeDotIfGroupingSeparatorIsDot_noGroupSep()
{
$transformer = new NumberToLocalizedStringTransformer(null, true);
$transformer->reverseTransform('1234.5');
}
public function testDecimalSeparatorMayBeDotIfGroupingSeparatorIsDotButNoGroupingUsed()
{
\Locale::setDefault('fr');
$transformer = new NumberToLocalizedStringTransformer();
$this->assertEquals(1234.5, $transformer->reverseTransform('1234,5'));
$this->assertEquals(1234.5, $transformer->reverseTransform('1234.5'));
}
public function testDecimalSeparatorMayBeCommaIfGroupingSeparatorIsNotComma()
{
\Locale::setDefault('ak');
$transformer = new NumberToLocalizedStringTransformer(null, true);
// completely valid format
$this->assertEquals(1234.5, $transformer->reverseTransform('1 234.5'));
// accept commas
$this->assertEquals(1234.5, $transformer->reverseTransform('1 234,5'));
// omit group separator
$this->assertEquals(1234.5, $transformer->reverseTransform('1234.5'));
$this->assertEquals(1234.5, $transformer->reverseTransform('1234,5'));
}
/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testDecimalSeparatorMayNotBeCommaIfGroupingSeparatorIsComma()
{
\Locale::setDefault('en');
$transformer = new NumberToLocalizedStringTransformer(null, true);
$transformer->reverseTransform('1,234,5');
}
/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testDecimalSeparatorMayNotBeCommaIfGroupingSeparatorIsComma_noGroupSep()
{
\Locale::setDefault('en');
$transformer = new NumberToLocalizedStringTransformer(null, true);
$transformer->reverseTransform('1234,5');
}
public function testDecimalSeparatorMayBeCommaIfGroupingSeparatorIsCommaButNoGroupingUsed()
{
\Locale::setDefault('en');
$transformer = new NumberToLocalizedStringTransformer();
$this->assertEquals(1234.5, $transformer->reverseTransform('1234,5'));
$this->assertEquals(1234.5, $transformer->reverseTransform('1234.5'));
}
/**
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
*/
public function testTransformExpectsNumeric()
{
@ -105,7 +195,7 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
}
/**
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
* @expectedException \Symfony\Component\Form\Exception\UnexpectedTypeException
*/
public function testReverseTransformExpectsString()
{
@ -115,7 +205,7 @@ class NumberToLocalizedStringTransformerTest extends LocalizedTestCase
}
/**
* @expectedException Symfony\Component\Form\Exception\TransformationFailedException
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testReverseTransformExpectsValidNumber()
{