8000 [Form] deprecate `NumberToLocalizedStringTransformer::ROUND_*` constants by nicolas-grekas · Pull Request #36711 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] deprecate NumberToLocalizedStringTransformer::ROUND_* constants 8000 #36711

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 1 commit into from
May 5, 2020
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
5 changes: 3 additions & 2 deletions UPGRADE-5.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,15 @@ EventDispatcher
Form
----

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

FrameworkBundle
---------------
Expand Down
5 changes: 3 additions & 2 deletions UPGRADE-6.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,13 @@ EventDispatcher
Form
----

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

FrameworkBundle
---------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Bridge\Twig\Tests\Extension;

use Symfony\Component\Form\Extension\Core\DataTransformer\PercentToLocalizedStringTransformer;
use Symfony\Component\Form\Extension\Core\Type\PercentType;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\Tests\AbstractLayoutTest;
Expand Down Expand Up @@ -2212,7 +2211,7 @@ public function testPasswordWithMaxLength()

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

$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
'/div
Expand All @@ -2234,7 +2233,7 @@ public function testPercent()

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

public function testPercentCustomSymbol()
{
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => '‱', 'rounding_mode' => PercentToLocalizedStringTransformer::ROUND_CEILING]);
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => '‱', 'rounding_mode' => \NumberFormatter::ROUND_CEILING]);
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
'/div
[@class="input-group"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Bridge\Twig\Tests\Extension;

use Symfony\Component\Form\Extension\Core\DataTransformer\PercentToLocalizedStringTransformer;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
Expand Down Expand Up @@ -1169,7 +1168,7 @@ public function testMoney()

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

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

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

public function testPercentCustomSymbol()
{
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => '‱', 'rounding_mode' => PercentToLocalizedStringTransformer::ROUND_CEILING]);
$form = $this->factory->createNamed('name', PercentType::class, 0.1, ['symbol' => '‱', 'rounding_mode' => \NumberFormatter::ROUND_CEILING]);
$this->assertWidgetMatchesXpath($form->createView(), ['id' => 'my&id', 'attr' => ['class' => 'my&class']],
'/div
[@class="input-group"]
Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ CHANGELOG
5.1.0
-----

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

5.0.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class IntegerToLocalizedStringTransformer extends NumberToLocalizedStringTransfo
* @param bool $grouping Whether thousands should be grouped
* @param int $roundingMode One of the ROUND_ constants in this class
*/
public function __construct(?bool $grouping = false, ?int $roundingMode = self::ROUND_DOWN)
public function __construct(?bool $grouping = false, ?int $roundingMode = \NumberFormatter::ROUND_DOWN)
{
parent::__construct(0, $grouping, $roundingMode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class MoneyToLocalizedStringTransformer extends NumberToLocalizedStringTransform
{
private $divisor;

public function __construct(?int $scale = 2, ?bool $grouping = true, ?int $roundingMode = self::ROUND_HALF_UP, ?int $divisor = 1)
public function __construct(?int $scale = 2, ?bool $grouping = true, ?int $roundingMode = \NumberFormatter::ROUND_HALFUP, ?int $divisor = 1)
{
if (null === $grouping) {
$grouping = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,51 +24,37 @@
class NumberToLocalizedStringTransformer implements DataTransformerInterface
{
/**
* Rounds a number towards positive infinity.
*
* Rounds 1.4 to 2 and -1.4 to -1.
* @deprecated since Symfony 5.1, use \NumberFormatter::ROUND_CEILING instead.
*/
const ROUND_CEILING = \NumberFormatter::ROUND_CEILING;

/**
* Rounds a number towards negative infinity.
*
* Rounds 1.4 to 1 and -1.4 to -2.
* @deprecated since Symfony 5.1, use \NumberFormatter::ROUND_FLOOR instead.
*/
const ROUND_FLOOR = \NumberFormatter::ROUND_FLOOR;

/**
* Rounds a number away from zero.
*
* Rounds 1.4 to 2 and -1.4 to -2.
* @deprecated since Symfony 5.1, use \NumberFormatter::ROUND_UP instead.
*/
const ROUND_UP = \NumberFormatter::ROUND_UP;

/**
* Rounds a number towards zero.
*
* Rounds 1.4 to 1 and -1.4 to -1.
* @deprecated since Symfony 5.1, use \NumberFormatter::ROUND_DOWN instead.
*/
const ROUND_DOWN = \NumberFormatter::ROUND_DOWN;

/**
* Rounds to the nearest number and halves to the next even number.
*
* Rounds 2.5, 1.6 and 1.5 to 2 and 1.4 to 1.
* @deprecated since Symfony 5.1, use \NumberFormatter::ROUND_HALFEVEN instead.
*/
const ROUND_HALF_EVEN = \NumberFormatter::ROUND_HALFEVEN;

/**
* Rounds to the nearest number and halves away from zero.
*
* Rounds 2.5 to 3, 1.6 and 1.5 to 2 and 1.4 to 1.
* @deprecated since Symfony 5.1, use \NumberFormatter::ROUND_HALFUP instead.
*/
const ROUND_HALF_UP = \NumberFormatter::ROUND_HALFUP;

/**
* Rounds to the nearest number and halves towards zero.
*
* Rounds 2.5 and 1.6 to 2, 1.5 and 1.4 to 1.
* @deprecated since Symfony 5.1, use \NumberFormatter::ROUND_HALFDOWN instead.
*/
const ROUND_HALF_DOWN = \NumberFormatter::ROUND_HALFDOWN;

Expand All @@ -79,14 +65,14 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface
private $scale;
private $locale;

public function __construct(int $scale = null, ?bool $grouping = false, ?int $roundingMode = self::ROUND_HALF_UP, string $locale = null)
public function __construct(int $scale = null, ?bool $grouping = false, ?int $roundingMode = \NumberFormatter::ROUND_HALFUP, string $locale = null)
{
if (null === $grouping) {
$grouping = false;
}

if (null === $roundingMode) {
$roundingMode = self::ROUND_HALF_UP;
$roundingMode = \NumberFormatter::ROUND_HALFUP;
}

$this->scale = $scale;
Expand Down Expand Up @@ -256,25 +242,25 @@ private function round($number)
$number = (string) ($number * $roundingCoef);

switch ($this->roundingMode) {
case self::ROUND_CEILING:
case \NumberFormatter::ROUND_CEILING:
$number = ceil($number);
break;
case self::ROUND_FLOOR:
case \NumberFormatter::ROUND_FLOOR:
$number = floor($number);
break;
case self::ROUND_UP:
case \NumberFormatter::ROUND_UP:
$number = $number > 0 ? ceil($number) : floor($number);
break;
case self::ROUND_DOWN:
case \NumberFormatter::ROUND_DOWN:
$number = $number > 0 ? floor($number) : ceil($number);
break;
case self::ROUND_HALF_EVEN:
case \NumberFormatter::ROUND_HALFEVEN:
$number = round($number, 0, PHP_ROUND_HALF_EVEN);
break;
case self::ROUND_HALF_UP:
case \NumberFormatter::ROUND_HALFUP:
$number = round($number, 0, PHP_ROUND_HALF_UP);
break;
case self::ROUND_HALF_DOWN:
case \NumberFormatter::ROUND_HALFDOWN:
$number = round($number, 0, PHP_ROUND_HALF_DOWN);
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,55 +23,6 @@
*/
class PercentToLocalizedStringTransformer implements DataTransformerInterface
{
/**
* Rounds a number towards positive infinity.
*
* Rounds 1.4 to 2 and -1.4 to -1.
*/
const ROUND_CEILING = \NumberFormatter::ROUND_CEILING;

/**
* Rounds a number towards negative infinity.
*
* Rounds 1.4 to 1 and -1.4 to -2.
*/
const ROUND_FLOOR = \NumberFormatter::ROUND_FLOOR;

/**
* Rounds a number away from zero.
*
* Rounds 1.4 to 2 and -1.4 to -2.
*/
const ROUND_UP = \NumberFormatter::ROUND_UP;

/**
* Rounds a number towards zero.
*
* Rounds 1.4 to 1 and -1.4 to -1.
*/
const ROUND_DOWN = \NumberFormatter::ROUND_DOWN;

/**
* Rounds to the nearest number and halves to the next even number.
*
* Rounds 2.5, 1.6 and 1.5 to 2 and 1.4 to 1.
*/
const ROUND_HALF_EVEN = \NumberFormatter::ROUND_HALFEVEN;

/**
* Rounds to the nearest number and halves away from zero.
*
* Rounds 2.5 to 3, 1.6 and 1.5 to 2 and 1.4 to 1.
*/
const ROUND_HALF_UP = \NumberFormatter::ROUND_HALFUP;

/**
* Rounds to the nearest number and halves towards zero.
*
* Rounds 2.5 and 1.6 to 2, 1.5 and 1.4 to 1.
*/
const ROUND_HALF_DOWN = \NumberFormatter::ROUND_HALFDOWN;

const FRACTIONAL = 'fractional';
const INTEGER = 'integer';

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

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

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

switch ($this->roundingMode) {
case self::ROUND_CEILING:
case \NumberFormatter::ROUND_CEILING:
$number = ceil($number);
break;
case self::ROUND_FLOOR:
case \NumberFormatter::ROUND_FLOOR:
$number = floor($number);
break;
case self::ROUND_UP:
case \NumberFormatter::ROUND_UP:
$number = $number > 0 ? ceil($number) : floor($number);
break;
case self::ROUND_DOWN:
case \NumberFormatter::ROUND_DOWN:
$number = $number > 0 ? floor($number) : ceil($number);
break;
case self::ROUND_HALF_EVEN:
case \NumberFormatter::ROUND_HALFEVEN:
$number = round($number, 0, PHP_ROUND_HALF_EVEN);
break;
case self::ROUND_HALF_UP:
case \NumberFormatter::ROUND_HALFUP:
$number = round($number, 0, PHP_ROUND_HALF_UP);
break;
case self::ROUND_HALF_DOWN:
case \NumberFormatter::ROUND_HALFDOWN:
$number = round($number, 0, PHP_ROUND_HALF_DOWN);
break;
}
Expand Down
16 changes: 8 additions & 8 deletions src/Symfony/Component/Form/Extension/Core/Type/IntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setDefaults([
'grouping' => false,
// Integer cast rounds towards 0, so do the same when displaying fractions
'rounding_mode' => IntegerToLocalizedStringTransformer::ROUND_DOWN,
'rounding_mode' => \NumberFormatter::ROUND_DOWN,
'compound' => false,
]);

$resolver->setAllowedValues('rounding_mode', [
IntegerToLocalizedStringTransformer::ROUND_FLOOR,
IntegerToLocalizedStringTransformer::ROUND_DOWN,
IntegerToLocalizedStringTransformer::ROUND_HALF_DOWN,
IntegerToLocalizedStringTransformer::ROUND_HALF_EVEN,
IntegerToLocalizedStringTransformer::ROUND_HALF_UP,
IntegerToLocalizedStringTransformer::ROUND_UP,
IntegerToLocalizedStringTransformer::ROUND_CEILING,
\NumberFormatter::ROUND_FLOOR,
\NumberFormatter::ROUND_DOWN,
\NumberFormatter::ROUND_HALFDOWN,
\NumberFormatter::ROUND_HALFEVEN,
\NumberFormatter::ROUND_HALFUP,
\NumberFormatter::ROUND_UP,
\NumberFormatter::ROUND_CEILING,
]);
}

Expand Down
Loading
0