Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1c419a2

Browse filesBrowse files
committed
forward valid numeric values to transform()
1 parent bb54e40 commit 1c419a2
Copy full SHA for 1c419a2

File tree

3 files changed

+37
-2
lines changed
Filter options

3 files changed

+37
-2
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformer.php
+7-1Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/DataTransformer/NumberToLocalizedStringTransformer.php
+5Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/DataTransformer/MoneyToLocalizedStringTransformerTest.php
+25-1Lines 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+
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.