Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ public function reverseTransform($value)
}

$formatter = $this->getNumberFormatter();
$groupSep = $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
Copy link
Contributor Author

@aaa2000 aaa2000 Apr 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I duplicate code of NumberToLocalizedStringTransformer.php maybe it is better to use it directly ?

-        if (!is_string($value)) {
-            throw new TransformationFailedException('Expected a string.');
-        }
-
         if ('' === $value) {
             return;
         }
 
         $formatter = $this->getNumberFormatter();
-        $groupSep = $formatter->getSymbol(\NumberFormatter::GROUPING_SEPARATOR_SYMBOL);
-        $decSep = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
         $grouping = $formatter->getAttribute(\NumberFormatter::GROUPING_USED);
-
-        if ('.' !== $decSep && (!$grouping || '.' !== $groupSep)) {
-            $value = str_replace('.', $decSep, $value);
-        }
-
-        if (',' !== $decSep && (!$grouping || ',' !== $groupSep)) {
-            $value = str_replace(',', $decSep, $value);
-        }
-
-        // replace normal spaces so that the formatter can read them
-        $value = $formatter->parse(str_replace(' ', ' ', $value));
-
-        if (intl_is_failure($formatter->getErrorCode())) {
-            throw new TransformationFailedException($formatter->getErrorMessage());
-        }
+        $transformer = new NumberToLocalizedStringTransformer($this->scale, (bool) $grouping);
+        $value = $transformer->reverseTransform($value);

$decSep = $formatter->getSymbol(\NumberFormatter::DECIMAL_SEPARATOR_SYMBOL);
$grouping = $formatter->getAttribute(\NumberFormatter::GROUPING_USED);

if ('.' !== $decSep && (!$grouping || '.' !== $groupSep)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be easier to first remove the grouping character and then replace $decSep with a dot?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use the same logic as NumberToLocalizedStringTransformer but if you want, I can try

$value = str_replace('.', $decSep, $value);
}

if (',' !== $decSep && (!$grouping || ',' !== $groupSep)) {
$value = str_replace(',', $decSep, $value);
}

// replace normal spaces so that the formatter can read them
$value = $formatter->parse(str_replace(' ', "\xc2\xa0", $value));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,121 @@ public function testReverseTransformExpectsString()

$transformer->reverseTransform(1);
}

public function testDecimalSeparatorMayBeDotIfGroupingSeparatorIsNotDot()
{
IntlTestHelper::requireFullIntl($this, '4.8.1.1');

\Locale::setDefault('fr');
$transformer = new PercentToLocalizedStringTransformer(1, 'integer');

// 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\TransformationFailedException
*/
public function testDecimalSeparatorMayNotBeDotIfGroupingSeparatorIsDot()
{
// Since we test against "de_AT", we need the full implementation
IntlTestHelper::requireFullIntl($this, '4.8.1.1');

\Locale::setDefault('de_AT');

$transformer = new PercentToLocalizedStringTransformer(1, 'integer');

$transformer->reverseTransform('1.234.5');
}

/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testDecimalSeparatorMayNotBeDotIfGroupingSeparatorIsDotWithNoGroupSep()
{
// Since we test against "de_DE", we need the full implementation
IntlTestHelper::requireFullIntl($this, '4.8.1.1');

\Locale::setDefault('de_DE');

$transformer = new PercentToLocalizedStringTransformer(1, 'integer');

$transformer->reverseTransform('1234.5');
}

public function testDecimalSeparatorMayBeDotIfGroupingSeparatorIsDotButNoGroupingUsed()
{
// Since we test against other locales, we need the full implementation
IntlTestHelper::requireFullIntl($this, false);

\Locale::setDefault('fr');
$transformer = new PercentToLocalizedStringTransformer(1, 'integer');

$this->assertEquals(1234.5, $transformer->reverseTransform('1234,5'));
$this->assertEquals(1234.5, $transformer->reverseTransform('1234.5'));
}

public function testDecimalSeparatorMayBeCommaIfGroupingSeparatorIsNotComma()
{
// Since we test against other locales, we need the full implementation
IntlTestHelper::requireFullIntl($this, '4.8.1.1');

\Locale::setDefault('bg');
$transformer = new PercentToLocalizedStringTransformer(1, 'integer');

// 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()
{
IntlTestHelper::requireFullIntl($this, '4.8.1.1');

$transformer = new PercentToLocalizedStringTransformer(1, 'integer');

$transformer->reverseTransform('1,234,5');
}

/**
* @expectedException \Symfony\Component\Form\Exception\TransformationFailedException
*/
public function testDecimalSeparatorMayNotBeCommaIfGroupingSeparatorIsCommaWithNoGroupSep()
{
IntlTestHelper::requireFullIntl($this, '4.8.1.1');

$transformer = new PercentToLocalizedStringTransformer(1, 'integer');

$transformer->reverseTransform('1234,5');
}

public function testDecimalSeparatorMayBeCommaIfGroupingSeparatorIsCommaButNoGroupingUsed()
{
$formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 1);
$formatter->setAttribute(\NumberFormatter::GROUPING_USED, false);

$transformer = $this->getMockBuilder('Symfony\Component\Form\Extension\Core\DataTransformer\PercentToLocalizedStringTransformer')
->setMethods(array('getNumberFormatter'))
->setConstructorArgs(array(1, 'integer'))
->getMock();
$transformer->expects($this->any())
->method('getNumberFormatter')
->willReturn($formatter);

$this->assertEquals(1234.5, $transformer->reverseTransform('1234,5'));
$this->assertEquals(1234.5, $transformer->reverseTransform('1234.5'));
}
}