Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3be0d35

Browse files
committed
forward valid numeric values to transform()
1 parent bb54e40 commit 3be0d35

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformer.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ class MoneyToLocalizedStringTransformer extends NumberToLocalizedStringTransform
2323
{
2424
private $divisor;
2525

26+
/**
27+
* @param int|null $scale
28+
* @param bool|null $grouping
29+
* @param int|null $roundingMode
30+
* @param int|null $divisor
31+
*/
2632
public function __construct($scale = 2, $grouping = true, $roundingMode = self::ROUND_HALF_UP, $divisor = 1)
2733
{
2834
if (null === $grouping) {
@@ -58,7 +64,7 @@ public function transform($value)
5864
if (!is_numeric($value)) {
5965
throw new TransformationFailedException('Expected a numeric.');
6066
}
61-
$value = (string) ($value / $this->divisor);
67+
$value /= $this->divisor;
6268
}
6369

6470
return parent::transform($value);

src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface
7878

7979
private $scale;
8080

81+
/**
82+
* @param int|null $scale
83+
* @param bool|null $grouping
84+
* @param int|null $roundingMode
85+
*/
8186
public function __construct($scale = null, $grouping = false, $roundingMode = self::ROUND_HALF_UP)
8287
{
8388
if (null === $grouping) {

src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformerTest.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717

1818
class MoneyToLocalizedStringTransformerTest extends TestCase
1919
{
20+
private $previousLocale;
21+
22+
protected function setUp()
23+
{
24+
$this->previousLocale = setlocale(LC_ALL, '0');
25+
}
26+
27+
protected function tearDown()
28+
{
29+
setlocale(LC_ALL, $this->previousLocale);
30+
}
31+
2032
public function testTransform()
2133
{
2234
// Since we test against "de_AT", we need the full implementation
@@ -73,7 +85,7 @@ public function testReverseTransformEmpty()
7385
$this->assertNull($transformer->reverseTransform(''));
7486
}
7587

76-
public function testFloatToIntConversionMismatchOnReversTransform()
88+
public function testFloatToIntConversionMismatchOnReverseTransform()
7789
{
7890
$transformer = new MoneyToLocalizedStringTransformer(null, null, null, 100);
7991
IntlTestHelper::requireFullIntl($this, false);
@@ -90,4 +102,16 @@ public function testFloatToIntConversionMismatchOnTransform()
90102

91103
$this->assertSame('10,20', $transformer->transform(1020));
92104
}
105+
106+
public function testValidNumericValuesWithNonDotDecimalPointCharacter()
107+
{
108+
// calling setlocale() here is important as it changes the representation of floats when being cast to strings
109+
setlocale(LC_ALL, 'de_AT.UTF-8');
110+
111+
$transformer = new MoneyToLocalizedStringTransformer(4, null, null, 100);
112+
IntlTestHelper::requireFullIntl($this, false);
113+
\Locale::setDefault('de_AT');
114+
115+
$this->assertSame('0,0035', $transformer->transform(12 / 34));
116+
}
93117
}

0 commit comments

Comments
 (0)