Skip to content
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

[Form] forward valid numeric values to transform() #30126

Merged
merged 1 commit into from Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,6 +23,12 @@ class MoneyToLocalizedStringTransformer extends NumberToLocalizedStringTransform
{
private $divisor;

/**
* @param int|null $scale
* @param bool|null $grouping
* @param int|null $roundingMode
* @param int|null $divisor
*/
public function __construct($scale = 2, $grouping = true, $roundingMode = self::ROUND_HALF_UP, $divisor = 1)
{
if (null === $grouping) {
Expand Down Expand Up @@ -58,7 +64,7 @@ public function transform($value)
if (!is_numeric($value)) {
throw new TransformationFailedException('Expected a numeric.');
}
$value = (string) ($value / $this->divisor);
$value /= $this->divisor;
Copy link
Contributor

Choose a reason for hiding this comment

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

I have tried forcing a rounding issue with and without casting to string and also by reverse transforming either value and I can't find any differences in the behavior. I can't remember why we put the string cast there, but I assume it was just for safety. This change looks good to me.

Copy link
Contributor

Choose a reason for hiding this comment

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

@xabbuh checked the referenced issue and this fix, looks good to me, yes, doesn't seem casting to string was added on specific purpose here, so i think it should be fine.

}

return parent::transform($value);
Expand Down
Expand Up @@ -78,6 +78,11 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface

private $scale;

/**
* @param int|null $scale
* @param bool|null $grouping
* @param int|null $roundingMode
*/
public function __construct($scale = null, $grouping = false, $roundingMode = self::ROUND_HALF_UP)
{
if (null === $grouping) {
Expand Down
Expand Up @@ -17,6 +17,18 @@

class MoneyToLocalizedStringTransformerTest extends TestCase
{
private $previousLocale;

protected function setUp()
{
$this->previousLocale = setlocale(LC_ALL, '0');
}

protected function tearDown()
{
setlocale(LC_ALL, $this->previousLocale);
}

public function testTransform()
{
// Since we test against "de_AT", we need the full implementation
Expand Down Expand Up @@ -73,7 +85,7 @@ public function testReverseTransformEmpty()
$this->assertNull($transformer->reverseTransform(''));
}

public function testFloatToIntConversionMismatchOnReversTransform()
public function testFloatToIntConversionMismatchOnReverseTransform()
{
$transformer = new MoneyToLocalizedStringTransformer(null, null, null, 100);
IntlTestHelper::requireFullIntl($this, false);
Expand All @@ -90,4 +102,16 @@ public function testFloatToIntConversionMismatchOnTransform()

$this->assertSame('10,20', $transformer->transform(1020));
}

public function testValidNumericValuesWithNonDotDecimalPointCharacter()
{
// calling setlocale() here is important as it changes the representation of floats when being cast to strings
setlocale(LC_ALL, 'de_AT.UTF-8');

$transformer = new MoneyToLocalizedStringTransformer(4, null, null, 100);
IntlTestHelper::requireFullIntl($this, false);
\Locale::setDefault('de_AT');

$this->assertSame('0,0035', $transformer->transform(12 / 34));
}
}