8000 [Form] Fix handling the empty string in NumberToLocalizedStringTransformer by gnat42 · Pull Request #60676 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Fix handling the empty string in NumberToLocalizedStringTransformer #60676

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

Merged
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.
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 @@ -33,14 +33,14 @@ public function __construct(?int $scale = 2, ?bool $grouping = true, ?int $round
/**
* Transforms a normalized format into a localized money string.
*
* @param int|float|null $value Normalized number
* @param int|float|string|null $value Normalized number
*
* @throws TransformationFailedException if the given value is not numeric or
* if the value cannot be transformed
*/
public function transform(mixed $value): string
{
if (null !== $value && 1 !== $this->divisor) {
if (null !== $value && '' !== $value && 1 !== $this->divisor) {
if (!is_numeric($value)) {
throw new TransformationFailedException('Expected a numeric.');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public function __construct(?int $scale = null, ?bool $grouping = false, ?int $r
/**
* Transforms a number type into localized number.
*
* @param int|float|null $value Number value
* @param int|float|string|null $value Number value
*
* @throws TransformationFailedException if the given value is not numeric
* or if the value cannot be transformed
*/
public function transform(mixed $value): string
{
if (null === $value) {
if (null === $value || '' === $value) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn’t $value PHPdoc be updated to include string?

return '';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public function testTransformExpectsNumeric()
$transformer->transform('abcd');
}

public function testTransformEmptyString()
{
$transformer = new MoneyToLocalizedStringTransformer(null, null, null, 100);

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

public function testTransformEmpty()
{
$transformer = new MoneyToLocalizedStringTransformer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static function provideTransformations()
{
return [
[null, '', 'de_AT'],
['', '', 'de_AT'],
[1, '1', 'de_AT'],
[1.5, '1,5', 'de_AT'],
[1234.5, '1234,5', 'de_AT'],
Expand Down
Loading
0