8000 [Form] deprecate `NumberToLocalizedStringTransformer::ROUND_*` constants · symfony/symfony@40f5060 · GitHub
[go: up one dir, main page]

Skip to content

Commit 40f5060

Browse files
[Form] deprecate NumberToLocalizedStringTransformer::ROUND_* constants
1 parent e024db7 commit 40f5060

19 files changed

+417
-482
lines changed

UPGRADE-5.1.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ EventDispatcher
3939
Form
4040
----
4141

42-
* Not configuring the `rounding_mode` option of the `PercentType` is deprecated. It will default to `PercentToLocalizedStringTransformer::ROUND_HALF_UP` in Symfony 6.
43-
* Not passing a rounding mode to the constructor of `PercentToLocalizedStringTransformer` is deprecated. It will default to `ROUND_HALF_UP` in Symfony 6.
42+
* Not configuring the `rounding_mode` option of the `PercentType` is deprecated. It will default to `\NumberFormatter::ROUND_HALFUP` in Symfony 6.
43+
* Not passing a rounding mode to the constructor of `PercentToLocalizedStringTransformer` is deprecated. It will default to `\NumberFormatter::ROUND_HALFUP` in Symfony 6.
4444
* Implementing the `FormConfigInterface` without implementing the `getIsEmptyCallback()` method
4545
is deprecated. The method will be added to the interface in 6.0.
4646
* Implementing the `FormConfigBuilderInterface` without implementing the `setIsEmptyCallback()` method
4747
is deprecated. The method will be added to the interface in 6.0.
4848
* Added argument `callable|null $filter` to `ChoiceListFactoryInterface::createListFromChoices()` and `createListFromLoader()` - not defining them is deprecated.
4949
* Using `Symfony\Component\Form\Extension\Validator\Util\ServerParams` class is deprecated, use its parent `Symfony\Component\Form\Util\ServerParams` instead.
50+
* The `NumberToLocalizedStringTransformer::ROUND_*` constants have been deprecated, use `\NumberFormatter::ROUND_*` instead.
5051

5152
FrameworkBundle
5253
---------------

UPGRADE-6.0.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ EventDispatcher
3939
Form
4040
----
4141

42-
* The default value of the `rounding_mode` option of the `PercentType` has been changed to `PercentToLocalizedStringTransformer::ROUND_HALF_UP`.
43-
* The default rounding mode of the `PercentToLocalizedStringTransformer` has been changed to `ROUND_HALF_UP`.
42+
* The default value of the `rounding_mode` option of the `PercentType` has been changed to `\NumberFormatter::ROUND_HALFUP`.
43+
* The default rounding mode of the `PercentToLocalizedStringTransformer` has been changed to `\NumberFormatter::ROUND_HALFUP`.
4444
* Added the `getIsEmptyCallback()` method to the `FormConfigInterface`.
4545
* Added the `setIsEmptyCallback()` method to the `FormConfigBuilderInterface`.
4646
* Added argument `callable|null $filter` to `ChoiceListFactoryInterface::createListFromChoices()` and `createListFromLoader()`.
4747
* The `Symfony\Component\Form\Extension\Validator\Util\ServerParams` class has been removed, use its parent `Symfony\Component\Form\Util\ServerParams` instead.
48+
* The `NumberToLocalizedStringTransformer::ROUND_*` constants have been removed, use `\NumberFormatter::ROUND_*` instead.
4849

4950
FrameworkBundle
5051
---------------

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap3LayoutTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Bridge\Twig\Tests\Extension;
1313

14-
use Symfony\Component\Form\Extension\Core\DataTransformer\PercentToLocalizedStringTransformer;
1514
use Symfony\Component\Form\Extension\Core\Type\PercentType;
1615
use Symfony\Component\Form\FormError;
1716
use Symfony\Component\Form\Tests\AbstractLayoutTest;
@@ -2212,7 +2211,7 @@ public function testPasswordWithMaxLength()
22122211

22132212
public function testPercent()
22142213
{
2215-
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\PercentType', 0.1, ['rounding_mode' => PercentToLocalizedStringTransformer::ROUND_CEILING]);
2214+
$form = $this->factory->createNamed('name', 'Symfony\Component\Form\Extension\Core\Type\PercentType', 0.1, ['rounding_mode' => \NumberFormatter::ROUND_CEILING]);
22162215

22172216
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class'< 10000 /span> => 'my&class']],
22182217
'/div
@@ -2234,7 +2233,7 @@ public function testPercent()
22342233

22352234
public function testPercentNoSymbol()
22362235
{
2237-
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => false, 'rounding_mode' => PercentToLocalizedStringTransformer::ROUND_CEILING]);
2236+
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => false, 'rounding_mode' => \NumberFormatter::ROUND_CEILING]);
22382237
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
22392238
'/input
22402239
[@id="my&id"]
@@ -2248,7 +2247,7 @@ public function testPercentNoSymbol()
22482247

22492248
public function testPercentCustomSymbol()
22502249
{
2251-
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => '', 'rounding_mode' => PercentToLocalizedStringTransformer::ROUND_CEILING]);
2250+
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => '', 'rounding_mode' => \NumberFormatter::ROUND_CEILING]);
22522251
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
22532252
'/div
22542253
[@class="input-group"]

src/Symfony/Bridge/Twig/Tests/Extension/AbstractBootstrap4LayoutTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Symfony\Bridge\Twig\Tests\Extension;
1313

14-
use Symfony\Component\Form\Extension\Core\DataTransformer\PercentToLocalizedStringTransformer;
1514
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
1615
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
1716
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
@@ -1169,7 +1168,7 @@ public function testMoney()
11691168

11701169
public function testPercent()
11711170
{
1172-
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['rounding_mode' => PercentToLocalizedStringTransformer::ROUND_CEILING]);
1171+
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['rounding_mode' => \NumberFormatter::ROUND_CEILING]);
11731172

11741173
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
11751174
'/div
@@ -1195,7 +1194,7 @@ public function testPercent()
11951194

11961195
public function testPercentNoSymbol()
11971196
{
1198-
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => false, 'rounding_mode' => PercentToLocalizedStringTransformer::ROUND_CEILING]);
1197+
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => false, 'rounding_mode' => \NumberFormatter::ROUND_CEILING]);
11991198
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
12001199
'/input
12011200
[@id="my&id"]
@@ -1209,7 +1208,7 @@ public function testPercentNoSymbol()
12091208

12101209
public function testPercentCustomSymbol()
12111210
{
1212-
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => '', 'rounding_mode' => PercentToLocalizedStringTransformer::ROUND_CEILING]);
1211+
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => '', 'rounding_mode' => \NumberFormatter::ROUND_CEILING]);
12131212
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
12141213
'/div
12151214
[@class="input-group"]

src/Symfony/Component/Form/CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ CHANGELOG
44
5.1.0
55
-----
66

7-
* Deprecated not configuring the `rounding_mode` option of the `PercentType`. It will default to `PercentToLocalizedStringTransformer::ROUND_HALF_UP` in Symfony 6.
8-
* Deprecated not passing a rounding mode to the constructor of `PercentToLocalizedStringTransformer`. It will default to `ROUND_HALF_UP` in Symfony 6.
7+
* Deprecated not configuring the `rounding_mode` option of the `PercentType`. It will default to `\NumberFormatter::ROUND_HALFUP` in Symfony 6.
8+
* Deprecated not passing a rounding mode to the constructor of `PercentToLocalizedStringTransformer`. It will default to `\NumberFormatter::ROUND_HALFUP` in Symfony 6.
99
* Added `collection_entry` block prefix to `CollectionType` entries
1010
* Added a `choice_filter` option to `ChoiceType`
1111
* Added argument `callable|null $filter` to `ChoiceListFactoryInterface::createListFromChoices()` and `createListFromLoader()` - not defining them is deprecated.
@@ -20,6 +20,7 @@ CHANGELOG
2020
* Added a `rounding_mode` option for the PercentType and correctly round the value when submitted
2121
* Deprecated `Symfony\Component\Form\Extension\Validator\Util\ServerParams` in favor of its parent class `Symfony\Component\Form\Util\ServerParams`
2222
* Added the `html5` option to the `ColorType` to validate the input
23+
* Deprecated `NumberToLocalizedStringTransformer::ROUND_*` constants, use `\NumberFormatter::ROUND_*` instead
2324

2425
5.0.0
2526
-----

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class IntegerToLocalizedStringTransformer extends NumberToLocalizedStringTransfo
2727
* @param bool $grouping Whether thousands should be grouped
2828
* @param int $roundingMode One of the ROUND_ constants in this class
2929
*/
30-
public function __construct(?bool $grouping = false, ?int $roundingMode = self::ROUND_DOWN)
30+
public function __construct(?bool $grouping = false, ?int $roundingMode = \NumberFormatter::ROUND_DOWN)
3131
{
3232
parent::__construct(0, $grouping, $roundingMode);
3333
}

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

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

26-
public function __construct(?int $scale = 2, ?bool $grouping = true, ?int $roundingMode = self::ROUND_HALF_UP, ?int $divisor = 1)
26+
public function __construct(?int $scale = 2, ?bool $grouping = true, ?int $roundingMode = \NumberFormatter::ROUND_HALFUP, ?int $divisor = 1)
2727
{
2828
if (null === $grouping) {
2929
$grouping = true;

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

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -24,51 +24,37 @@
2424
class NumberToLocalizedStringTransformer implements DataTransformerInterface
2525
{
2626
/**
27-
* Rounds a number towards positive infinity.
28-
*
29-
* Rounds 1.4 to 2 and -1.4 to -1.
27+
* @deprecated since Symfony 5.1, use \NumberFormatter::ROUND_CEILING instead.
3028
*/
3129
const ROUND_CEILING = \NumberFormatter::ROUND_CEILING;
3230

3331
/**
34-
* Rounds a number towards negative infinity.
35-
*
36-
* Rounds 1.4 to 1 and -1.4 to -2.
32+
* @deprecated since Symfony 5.1, use \NumberFormatter::ROUND_FLOOR instead.
3733
*/
3834
const ROUND_FLOOR = \NumberFormatter::ROUND_FLOOR;
3935

4036
/**
41-
* Rounds a number away from zero.
42-
*
43-
* Rounds 1.4 to 2 and -1.4 to -2.
37+
* @deprecated since Symfony 5.1, use \NumberFormatter::ROUND_UP instead.
4438
*/
4539
const ROUND_UP = \NumberFormatter::ROUND_UP;
4640

4741
/**
48-
* Rounds a number towards zero.
49-
*
50-
* Rounds 1.4 to 1 and -1.4 to -1.
42+
* @deprecated since Symfony 5.1, use \NumberFormatter::ROUND_DOWN instead.
5143
*/
5244
const ROUND_DOWN = \NumberFormatter::ROUND_DOWN;
5345

5446
/**
55-
* Rounds to the nearest number and halves to the next even number.
56-
*
57-
* Rounds 2.5, 1.6 and 1.5 to 2 and 1.4 to 1.
47+
* @deprecated since Symfony 5.1, use \NumberFormatter::ROUND_HALFEVEN instead.
5848
*/
5949
const ROUND_HALF_EVEN = \NumberFormatter::ROUND_HALFEVEN;
6050

6151
/**
62-
* Rounds to the nearest number and halves away from zero.
63-
*
64-
* Rounds 2.5 to 3, 1.6 and 1.5 to 2 and 1.4 to 1.
52+
* @deprecated since Symfony 5.1, use \NumberFormatter::ROUND_HALFUP instead.
6553
*/
6654
const ROUND_HALF_UP = \NumberFormatter::ROUND_HALFUP;
6755

6856
/**
69-
* Rounds to the nearest number and halves towards zero.
70-
*
71-
* Rounds 2.5 and 1.6 to 2, 1.5 and 1.4 to 1.
57+
* @deprecated since Symfony 5.1, use \NumberFormatter::ROUND_HALFDOWN instead.
7258
*/
7359
const ROUND_HALF_DOWN = \NumberFormatter::ROUND_HALFDOWN;
7460

@@ -79,14 +65,14 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface
7965
private $scale;
8066
private $locale;
8167

82-
public function __construct(int $scale = null, ?bool $grouping = false, ?int $roundingMode = self::ROUND_HALF_UP, string $locale = null)
68+
public function __construct(int $scale = null, ?bool $grouping = false, ?int $roundingMode = \NumberFormatter::ROUND_HALFUP, string $locale = null)
8369
{
8470
if (null === $grouping) {
8571
$grouping = false;
8672
}
8773

8874
if (null === $roundingMode) {
89-
$roundingMode = self::ROUND_HALF_UP;
75+
$roundingMode = \NumberFormatter::ROUND_HALFUP;
9076
}
9177

9278
$this->scale = $scale;
@@ -256,25 +242,25 @@ private function round($number)
256242
$number = (string) ($number * $roundingCoef);
257243

258244
switch ($this->roundingMode) {
259-
case self::ROUND_CEILING:
245+
case \NumberFormatter::ROUND_CEILING:
260246
$number = ceil($number);
261247
break;
262-
case self::ROUND_FLOOR:
248+
case \NumberFormatter::ROUND_FLOOR:
263249
$number = floor($number);
264250
break;
265-
case self::ROUND_UP:
251+
case \NumberFormatter::ROUND_UP:
266252
$number = $number > 0 ? ceil($number) : floor($number);
267253
break;
268-
case self::ROUND_DOWN:
254+
case \NumberFormatter::ROUND_DOWN:
269255
$number = $number > 0 ? floor($number) : ceil($number);
270256
break;
271-
case self::ROUND_HALF_EVEN:
257+
case \NumberFormatter::ROUND_HALFEVEN:
272258
$number = round($number, 0, PHP_ROUND_HALF_EVEN);
273259
break;
274-
case self::ROUND_HALF_UP:
260+
case \NumberFormatter::ROUND_HALFUP:
275261
$number = round($number, 0, PHP_ROUND_HALF_UP);
276262
break;
277-
case self::ROUND_HALF_DOWN:
263+
case \NumberFormatter::ROUND_HALFDOWN:
278264
$number = round($number, 0, PHP_ROUND_HALF_DOWN);
279265
break;
280266
}

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

Lines changed: 8 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -23,55 +23,6 @@
2323
*/
2424
class PercentToLocalizedStringTransformer implements DataTransformerInterface
2525
{
26-
/**
27-
* Rounds a number towards positive infinity.
28-
*
29-
* Rounds 1.4 to 2 and -1.4 to -1.
30-
*/
31-
const ROUND_CEILING = \NumberFormatter::ROUND_CEILING;
32-
33-
/**
34-
* Rounds a number towards negative infinity.
35-
*
36-
* Rounds 1.4 to 1 and -1.4 to -2.
37-
*/
38-
const ROUND_FLOOR = \NumberFormatter::ROUND_FLOOR;
39-
40-
/**
41-
* Rounds a number away from zero.
42-
*
43-
* Rounds 1.4 to 2 and -1.4 to -2.
44-
*/
45-
const ROUND_UP = \NumberFormatter::ROUND_UP;
46-
47-
/**
48-
* Rounds a number towards zero.
49-
*
50-
* Rounds 1.4 to 1 and -1.4 to -1.
51-
*/
52-
const ROUND_DOWN = \NumberFormatter::ROUND_DOWN;
53-
54-
/**
55-
* Rounds to the nearest number and halves to the next even number.
56-
*
57-
* Rounds 2.5, 1.6 and 1.5 to 2 and 1.4 to 1.
58-
*/
59-
const ROUND_HALF_EVEN = \NumberFormatter::ROUND_HALFEVEN;
60-
61-
/**
62-
* Rounds to the nearest number and halves away from zero.
63-
*
64-
* Rounds 2.5 to 3, 1.6 and 1.5 to 2 and 1.4 to 1.
65-
*/
66-
const ROUND_HALF_UP = \NumberFormatter::ROUND_HALFUP;
67-
68-
/**
69-
* Rounds to the nearest number and halves towards zero.
70-
*
71-
* Rounds 2.5 and 1.6 to 2, 1.5 and 1.4 to 1.
72-
*/
73-
const ROUND_HALF_DOWN = \NumberFormatter::ROUND_HALFDOWN;
74-
7526
const FRACTIONAL = 'fractional';
7627
const INTEGER = 'integer';
7728

@@ -103,7 +54,7 @@ public function __construct(int $scale = null, string $type = null, ?int $roundi
10354
}
10455

10556
if (null === $roundingMode && (\func_num_args() < 4 || func_get_arg(3))) {
106-
trigger_deprecation('symfony/form', '5.1', sprintf('Not passing a rounding mode to %s() is deprecated. Starting with Symfony 6.0 it will default to "%s::ROUND_HALF_UP".', __METHOD__, __CLASS__));
57+
trigger_deprecation('symfony/form', '5.1', sprintf('Not passing a rounding mode to %s() is deprecated. Starting with Symfony 6.0 it will default to "NumberFormatter::ROUND_HALFUP".', __METHOD__, __CLASS__));
10758
}
10859

10960
if (!\in_array($type, self::$types, true)) {
@@ -263,25 +214,25 @@ private function round($number)
263214
$number = (string) ($number * $roundingCoef);
264215

265216
switch ($this->roundingMode) {
266-
case self::ROUND_CEILING:
217+
case \NumberFormatter::ROUND_CEILING:
267218
$number = ceil($number);
268219
break;
269-
case self::ROUND_FLOOR:
220+
case \NumberFormatter::ROUND_FLOOR:
270221
$number = floor($number);
271222
break;
272-
case self::ROUND_UP:
223+
case \NumberFormatter::ROUND_UP:
273224
$number = $number > 0 ? ceil($number) : floor($number);
274225
break;
275-
case self::ROUND_DOWN:
226+
case \NumberFormatter::ROUND_DOWN:
276227
$number = $number > 0 ? floor($number) : ceil($number);
277228
break;
278-
case self::ROUND_HALF_EVEN:
229+
case \NumberFormatter::ROUND_HALFEVEN:
279230
$number = round($number, 0, PHP_ROUND_HALF_EVEN);
280231
break;
281-
case self::ROUND_HALF_UP:
232+
case \NumberFormatter::ROUND_HALFUP:
282233
$number = round($number, 0, PHP_ROUND_HALF_UP);
283234
break;
284-
case self::ROUND_HALF_DOWN:
235+
case \NumberFormatter::ROUND_HALFDOWN:
285236
$number = round($number, 0, PHP_ROUND_HALF_DOWN);
286237
break;
287238
}

src/Symfony/Component/Form/Extension/Core/Type/IntegerType.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@ public function configureOptions(OptionsResolver $resolver)
4646
$resolver->setDefaults([
4747
'grouping' => false,
4848
// Integer cast rounds towards 0, so do the same when displaying fractions
49-
'rounding_mode' => IntegerToLocalizedStringTransformer::ROUND_DOWN,
49+
'rounding_mode' => \NumberFormatter::ROUND_DOWN,
5050
'compound' => false,
5151
]);
5252

5353
$resolver->setAllowedValues('rounding_mode', [
54-
IntegerToLocalizedStringTransformer::ROUND_FLOOR,
55-
IntegerToLocalizedStringTransformer::ROUND_DOWN,
56-
IntegerToLocalizedStringTransformer::ROUND_HALF_DOWN,
57-
IntegerToLocalizedStringTransformer::ROUND_HALF_EVEN,
58-
IntegerToLocalizedStringTransformer::ROUND_HALF_UP,
59-
IntegerToLocalizedStringTransformer::ROUND_UP,
60-
IntegerToLocalizedStringTransformer::ROUND_CEILING,
54+
\NumberFormatter::ROUND_FLOOR,
55+
\NumberFormatter::ROUND_DOWN,
56+
\NumberFormatter::ROUND_HALFDOWN,
57+
\NumberFormatter::ROUND_HALFEVEN,
58+
\NumberFormatter::ROUND_HALFUP,
59+
\NumberFormatter::ROUND_UP,
60+
\NumberFormatter::ROUND_CEILING,
6161
]);
6262
}
6363

0 commit comments

Comments
 (0)
0