Skip to content

Commit f623d3a

Browse files
committed
Allow NumberToLocalizedStringTransformer empty values
1 parent 17fa1fd commit f623d3a

File tree

4 files changed

+12
-4
lines changed

4 files changed

+12
-4
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ public function __construct(?int $scale = 2, ?bool $grouping = true, ?int $round
3333
/**
3434
* Transforms a normalized format into a localized money string.
3535
*
36-
* @param int|float|null $value Normalized number
36+
* @param int|float|string|null $value Normalized number
3737
*
3838
* @throws TransformationFailedException if the given value is not numeric or
3939
* if the value cannot be transformed
4040
*/
4141
public function transform(mixed $value): string
4242
{
43-
if (null !== $value && 1 !== $this->divisor) {
43+
if (null !== $value && '' !== $value && 1 !== $this->divisor) {
4444
if (!is_numeric($value)) {
4545
throw new TransformationFailedException('Expected a numeric.');
4646
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ public function __construct(?int $scale = null, ?bool $grouping = false, ?int $r
4343
/**
4444
* Transforms a number type into localized number.
4545
*
46-
* @param int|float|null $value Number value
46+
* @param int|float|string|null $value Number value
4747
*
4848
* @throws TransformationFailedException if the given value is not numeric
4949
* or if the value cannot be transformed
5050
*/
5151
public function transform(mixed $value): string
5252
{
53-
if (null === $value) {
53+
if (null === $value || '' === $value) {
5454
return '';
5555
}
5656

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,13 @@ public function testTransformExpectsNumeric()
5454
$transformer->transform('abcd');
5555
}
5656

57+
public function testTransformEmptyString()
58+
{
59+
$transformer = new MoneyToLocalizedStringTransformer(null, null, null, 100);
60+
61+
$this->assertSame('', $transformer->transform(''));
62+
}
63+
5764
public function testTransformEmpty()
5865
{
5966
$transformer = new MoneyToLocalizedStringTransformer();

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public static function provideTransformations()
4949
{
5050
return [
5151
[null, '', 'de_AT'],
52+
['', '', 'de_AT'],
5253
[1, '1', 'de_AT'],
5354
[1.5, '1,5', 'de_AT'],
5455
[1234.5, '1234,5', 'de_AT'],

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy