Skip to content

Deprecated precision option in favor of scale #13717

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
Apr 3, 2015
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
18 changes: 18 additions & 0 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ UPGRADE FROM 2.x to 3.0

### Form

* The option "precision" was renamed to "scale".

Before:

```php
$builder->add('length', 'number', array(
'precision' => 3,
));
```

After:

```php
$builder->add('length', 'number', array(
'scale' => 3,
));
```

* The method `AbstractType::setDefaultOptions(OptionsResolverInterface $resolver)` and
`AbstractTypeExtension::setDefaultOptions(OptionsResolverInterface $resolver)` have been
renamed. You should use `AbstractType::configureOptions(OptionsResolver $resolver)` and
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
2.7.0
-----

* deprecated option "precision" in favor of "scale"
* deprecated the overwriting of AbstractType::setDefaultOptions() in favor of overwriting AbstractType::configureOptions().
* deprecated the overwriting of AbstractTypeExtension::setDefaultOptions() in favor of overwriting AbstractTypeExtension::configureOptions().

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ class IntegerToLocalizedStringTransformer extends NumberToLocalizedStringTransfo
/**
* Constructs a transformer.
*
* @param int $precision Unused.
* @param int $scale Unused.
* @param bool $grouping Whether thousands should be grouped.
* @param int $roundingMode One of the ROUND_ constants in this class.
*/
public function __construct($precision = 0, $grouping = false, $roundingMode = self::ROUND_DOWN)
public function __construct($scale = 0, $grouping = false, $roundingMode = self::ROUND_DOWN)
{
if (null === $roundingMode) {
$roundingMode = self::ROUND_DOWN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ class MoneyToLocalizedStringTransformer extends NumberToLocalizedStringTransform
{
private $divisor;

public function __construct($precision = 2, $grouping = true, $roundingMode = self::ROUND_HALF_UP, $divisor = 1)
public function __construct($scale = 2, $grouping = true, $roundingMode = self::ROUND_HALF_UP, $divisor = 1)
{
if (null === $grouping) {
$grouping = true;
}

if (null === $precision) {
$precision = 2;
if (null === $scale) {
$scale = 2;
}

parent::__construct($precision, $grouping, $roundingMode);
parent::__construct($scale, $grouping, $roundingMode);

if (null === $divisor) {
$divisor = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,16 @@ class NumberToLocalizedStringTransformer implements DataTransformerInterface
*/
const ROUND_HALFDOWN = \NumberFormatter::ROUND_HALFDOWN;

/**
* @deprecated since version 2.7, will be replaced by a $scale private property in 3.0.
*/
protected $precision;

protected $grouping;

protected $roundingMode;

public function __construct($precision = null, $grouping = false, $roundingMode = self::ROUND_HALF_UP)
public function __construct($scale = null, $grouping = false, $roundingMode = self::ROUND_HALF_UP)
{
if (null === $grouping) {
$grouping = false;
Expand All @@ -109,7 +112,7 @@ public function __construct($precision = null, $grouping = false, $roundingMode
$roundingMode = self::ROUND_HALF_UP;
}

$this->precision = $precision;
$this->precision = $scale;
$this->grouping = $grouping;
$this->roundingMode = $roundingMode;
}
Expand Down Expand Up @@ -240,7 +243,7 @@ protected function getNumberFormatter()
}

/**
* Rounds a number according to the configured precision and rounding mode.
* Rounds a number according to the configured scale and rounding mode.
*
* @param int|float $number A number.
*
Expand All @@ -249,7 +252,7 @@ protected function getNumberFormatter()
private function round($number)
{
if (null !== $this->precision && null !== $this->roundingMode) {
// shift number to maintain the correct precision during rounding
// shift number to maintain the correct scale during rounding
$roundingCoef = pow(10, $this->precision);
$number *= $roundingCoef;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ class PercentToLocalizedStringTransformer implements DataTransformerInterface

private $type;

private $precision;
private $scale;

/**
* Constructor.
*
* @see self::$types for a list of supported types
*
* @param int $precision The precision
* @param string $type One of the supported types
* @param int $scale The scale
* @param string $type One of the supported types
*
* @throws UnexpectedTypeException if the given value of type is unknown
*/
public function __construct($precision = null, $type = null)
public function __construct($scale = null, $type = null)
{
if (null === $precision) {
$precision = 0;
if (null === $scale) {
$scale = 0;
}

if (null === $type) {
Expand All @@ -60,7 +60,7 @@ public function __construct($precision = null, $type = null)
}

$this->type = $type;
$this->precision = $precision;
$this->scale = $scale;
}

/**
Expand Down Expand Up @@ -142,7 +142,7 @@ protected function getNumberFormatter()
{
$formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);

$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->precision);
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->scale);

return $formatter;
}
Expand Down
17 changes: 15 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/IntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\DataTransformer\IntegerToLocalizedStringTransformer;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class IntegerType extends AbstractType
Expand All @@ -25,7 +26,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addViewTransformer(
new IntegerToLocalizedStringTransformer(
$options['precision'],
$options['scale'],
$options['grouping'],
$options['rounding_mode']
));
Expand All @@ -36,9 +37,19 @@ public function buildForm(FormBuilderInterface $builder, array $options)
*/
public function configureOptions(OptionsResolver $resolver)
{
$scale = function (Options $options) {
if (null !== $options['precision']) {
Copy link
Member

Choose a reason for hiding this comment

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

isset($options['precision']) would make the check work even when the key does not exist

Copy link
Member Author

Choose a reason for hiding this comment

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

This is also what other PRs used that renamed options. Doesn't the key always exists as it has a default?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, no need for isset

trigger_error('The form option "precision" is deprecated since version 2.7 and will be removed in 3.0. Use "scale" instead.', E_USER_DEPRECATED);
}

return $options['precision'];
};

$resolver->setDefaults(array(
// default precision is locale specific (usually around 3)
// deprecated as of Symfony 2.7, to be removed in Symfony 3.0.
'precision' => null,
// default scale is locale specific (usually around 3)
'scale' => $scale,
'grouping' => false,
// Integer cast rounds towards 0, so do the same when displaying fractions
'rounding_mode' => IntegerToLocalizedStringTransformer::ROUND_DOWN,
Expand All @@ -56,6 +67,8 @@ public function configureOptions(OptionsResolver $resolver)
IntegerToLocalizedStringTransformer::ROUND_CEILING,
),
));

$resolver->setAllowedTypes('scale', array('null', 'int'));
}

/**
Expand Down
19 changes: 17 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/MoneyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\DataTransformer\MoneyToLocalizedStringTransformer;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class MoneyType extends AbstractType
Expand All @@ -29,7 +30,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->addViewTransformer(new MoneyToLocalizedStringTransformer(
$options['precision'],
$options['scale'],
$options['grouping'],
null,
$options['divisor']
Expand All @@ -50,13 +51,27 @@ public function buildView(FormView $view, FormInterface $form, array $options)
*/
public function configureOptions(OptionsResolver $resolver)
{
$scale = function (Options $options) {
if (null !== $options['precision']) {
Copy link
Member

Choose a reason for hiding this comment

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

Same as above, if that a possible case: isset($options['precision'])

trigger_error('The form option "precision" is deprecated since version 2.7 and will be removed in 3.0. Use "scale" instead.', E_USER_DEPRECATED);

return $options['precision'];
}

return 2;
};

$resolver->setDefaults(array(
'precision' => 2,
// deprecated as of Symfony 2.7, to be removed in Symfony 3.0
'precision' => null,
'scale' => $scale,
'grouping' => false,
'divisor' => 1,
'currency' => 'EUR',
'compound' => false,
));

$resolver->setAllowedTypes('scale', 'int');
}

/**
Expand Down
17 changes: 15 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/NumberType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\DataTransformer\NumberToLocalizedStringTransformer;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class NumberType extends AbstractType
Expand All @@ -24,7 +25,7 @@ class NumberType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addViewTransformer(new NumberToLocalizedStringTransformer(
$options['precision'],
$options['scale'],
$options['grouping'],
$options['rounding_mode']
));
Expand All @@ -35,9 +36,19 @@ public function buildForm(FormBuilderInterface $builder, array $options)
*/
public function configureOptions(OptionsResolver $resolver)
{
$scale = function (Options $options) {
if (null !== $options['precision']) {
trigger_error('The form option "precision" is deprecated since version 2.7 and will be removed in 3.0. Use "scale" instead.', E_USER_DEPRECATED);
}

return $options['precision'];
};

$resolver->setDefaults(array(
// default precision is locale specific (usually around 3)
// deprecated as of Symfony 2.7, to be removed in Symfony 3.0
'precision' => null,
// default scale is locale specific (usually around 3)
'scale' => $scale,
'grouping' => false,
'rounding_mode' => NumberToLocalizedStringTransformer::ROUND_HALF_UP,
'compound' => false,
Expand All @@ -54,6 +65,8 @@ public function configureOptions(OptionsResolver $resolver)
NumberToLocalizedStringTransformer::ROUND_CEILING,
),
));

$resolver->setAllowedTypes('scale', array('null', 'int'));
}

/**
Expand Down
19 changes: 17 additions & 2 deletions src/Symfony/Component/Form/Extension/Core/Type/PercentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\DataTransformer\PercentToLocalizedStringTransformer;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;

class PercentType extends AbstractType
Expand All @@ -23,16 +24,28 @@ class PercentType extends AbstractType
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addViewTransformer(new PercentToLocalizedStringTransformer($options['precision'], $options['type']));
$builder->addViewTransformer(new PercentToLocalizedStringTransformer($options['scale'], $options['type']));
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$scale = function (Options $options) {
if (null !== $options['precision']) {
trigger_error('The form option "precision" is deprecated since version 2.7 and will be removed in 3.0. Use "scale" instead.', E_USER_DEPRECATED);

return $options['precision'];
}

return 0;
};

$resolver->setDefaults(array(
'precision' => 0,
// deprecated as of Symfony 2.7, to be removed in Symfony 3.0.
'precision' => null,
'scale' => $scale,
'type' => 'fractional',
'compound' => false,
));
Expand All @@ -43,6 +56,8 @@ public function configureOptions(OptionsResolver $resolver)
'integer',
),
));

$resolver->setAllowedTypes('scale', 'int');
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function testTransformWithGrouping($from, $to, $locale)
$this->assertSame($to, $transformer->transform($from));
}

public function testTransformWithPrecision()
public function testTransformWithScale()
{
$transformer = new NumberToLocalizedStringTransformer(2);

Expand Down Expand Up @@ -174,14 +174,14 @@ public function transformWithRoundingProvider()
/**
* @dataProvider transformWithRoundingProvider
*/
public function testTransformWithRounding($precision, $input, $output, $roundingMode)
public function testTransformWithRounding($scale, $input, $output, $roundingMode)
{
$transformer = new NumberToLocalizedStringTransformer($precision, null, $roundingMode);
$transformer = new NumberToLocalizedStringTransformer($scale, null, $roundingMode);

$this->assertEquals($output, $transformer->transform($input));
}

public function testTransformDoesNotRoundIfNoPrecision()
public function testTransformDoesNotRoundIfNoScale()
{
$transformer = new NumberToLocalizedStringTransformer(null, null, NumberToLocalizedStringTransformer::ROUND_DOWN);

Expand Down Expand Up @@ -327,14 +327,14 @@ public function reverseTransformWithRoundingProvider()
/**
* @dataProvider reverseTransformWithRoundingProvider
*/
public function testReverseTransformWithRounding($precision, $input, $output, $roundingMode)
public function testReverseTransformWithRounding($scale, $input, $output, $roundingMode)
{
$transformer = new NumberToLocalizedStringTransformer($precision, null, $roundingMode);
$transformer = new NumberToLocalizedStringTransformer($scale, null, $roundingMode);

$this->assertEquals($output, $transformer->reverseTransform($input));
}

public function testReverseTransformDoesNotRoundIfNoPrecision()
public function testReverseTransformDoesNotRoundIfNoScale()
{
$transformer = new NumberToLocalizedStringTransformer(null, null, NumberToLocalizedStringTransformer::ROUND_DOWN);

Expand Down
Loading