Skip to content

Commit

Permalink
feature #13717 Deprecated precision option in favor of scale (WouterJ)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.7 branch.

Discussion
----------

Deprecated precision option in favor of scale

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | yes
| Tests pass?   | yes
| Fixed tickets | #7383
| License       | MIT
| Doc PR        | symfony/symfony-docs#5005

Scale is the number of digits to the right of the decimal point in a number, precision isn't. See the referenced ticket for more context.

Commits
-------

2a2f7e2 Deprecated precision option in favor of scale
  • Loading branch information
fabpot committed Apr 3, 2015
2 parents 71417e6 + 49a274f commit 230a32e
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 38 deletions.
1 change: 1 addition & 0 deletions 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 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']) {
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 @@ -54,6 +65,8 @@ public function configureOptions(OptionsResolver $resolver)
IntegerToLocalizedStringTransformer::ROUND_UP,
IntegerToLocalizedStringTransformer::ROUND_CEILING,
));

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

/**
Expand Down
19 changes: 17 additions & 2 deletions 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']) {
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 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 @@ -52,6 +63,8 @@ public function configureOptions(OptionsResolver $resolver)
NumberToLocalizedStringTransformer::ROUND_UP,
NumberToLocalizedStringTransformer::ROUND_CEILING,
));

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

/**
Expand Down
19 changes: 17 additions & 2 deletions 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 @@ -41,6 +54,8 @@ public function configureOptions(OptionsResolver $resolver)
'fractional',
'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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testTransformWithInteger()
$this->assertEquals('16', $transformer->transform(15.9));
}

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

Expand Down Expand Up @@ -87,7 +87,7 @@ public function testReverseTransformWithInteger()
$this->assertEquals(200, $transformer->reverseTransform('200'));
}

public function testReverseTransformWithPrecision()
public function testReverseTransformWithScale()
{
$transformer = new PercentToLocalizedStringTransformer(2);

Expand Down
Loading

0 comments on commit 230a32e

Please sign in to comment.