deprecate precision in IntegerToLocalizedStringTransformer

This commit is contained in:
Christian Flothmann 2018-09-23 19:53:26 +02:00
parent a26bd3673f
commit 65362e0ace
6 changed files with 60 additions and 8 deletions

View File

@ -55,6 +55,8 @@ Finder
Form Form
---- ----
* The `$scale` argument of the `IntegerToLocalizedStringTransformer` is deprecated.
* Deprecated calling `FormRenderer::searchAndRenderBlock` for fields which were already rendered. * Deprecated calling `FormRenderer::searchAndRenderBlock` for fields which were already rendered.
Instead of expecting such calls to return empty strings, check if the field has already been rendered. Instead of expecting such calls to return empty strings, check if the field has already been rendered.

View File

@ -64,6 +64,11 @@ EventDispatcher
* The `TraceableEventDispatcherInterface` has been removed. * The `TraceableEventDispatcherInterface` has been removed.
Form
----
* The `$scale` argument of the `IntegerToLocalizedStringTransformer` was removed.
Finder Finder
------ ------

View File

@ -4,6 +4,7 @@ CHANGELOG
4.2.0 4.2.0
----- -----
* deprecated the `$scale` argument of the `IntegerToLocalizedStringTransformer`
* added `Symfony\Component\Form\ClearableErrorsInterface` * added `Symfony\Component\Form\ClearableErrorsInterface`
* deprecated calling `FormRenderer::searchAndRenderBlock` for fields which were already rendered * deprecated calling `FormRenderer::searchAndRenderBlock` for fields which were already rendered

View File

@ -22,12 +22,18 @@ class IntegerToLocalizedStringTransformer extends NumberToLocalizedStringTransfo
/** /**
* Constructs a transformer. * Constructs a transformer.
* *
* @param int $scale Unused
* @param bool $grouping Whether thousands should be grouped * @param bool $grouping Whether thousands should be grouped
* @param int $roundingMode One of the ROUND_ constants in this class * @param int $roundingMode One of the ROUND_ constants in this class
*/ */
public function __construct(?int $scale = 0, ?bool $grouping = false, int $roundingMode = self::ROUND_DOWN) public function __construct($grouping = false, $roundingMode = self::ROUND_DOWN)
{ {
if (\is_int($grouping) || \is_bool($roundingMode) || 2 < \func_num_args()) {
@trigger_error(sprintf('Passing a precision as the first value to %s::__construct() is deprecated since Symfony 4.2 and support for it will be dropped in 5.0.', __CLASS__), E_USER_DEPRECATED);
$grouping = $roundingMode;
$roundingMode = 2 < \func_num_args() ? func_get_arg(2) : self::ROUND_DOWN;
}
parent::__construct(0, $grouping, $roundingMode); parent::__construct(0, $grouping, $roundingMode);
} }

View File

@ -23,12 +23,7 @@ class IntegerType extends AbstractType
*/ */
public function buildForm(FormBuilderInterface $builder, array $options) public function buildForm(FormBuilderInterface $builder, array $options)
{ {
$builder->addViewTransformer( $builder->addViewTransformer(new IntegerToLocalizedStringTransformer($options['grouping'], $options['rounding_mode']));
new IntegerToLocalizedStringTransformer(
$options['scale'],
$options['grouping'],
$options['rounding_mode']
));
} }
/** /**

View File

@ -79,6 +79,18 @@ class IntegerToLocalizedStringTransformerTest extends TestCase
* @dataProvider transformWithRoundingProvider * @dataProvider transformWithRoundingProvider
*/ */
public function testTransformWithRounding($input, $output, $roundingMode) public function testTransformWithRounding($input, $output, $roundingMode)
{
$transformer = new IntegerToLocalizedStringTransformer(null, $roundingMode);
$this->assertEquals($output, $transformer->transform($input));
}
/**
* @group legacy
* @expectedDeprecation Passing a precision as the first value to %s::__construct() is deprecated since Symfony 4.2 and support for it will be dropped in 5.0.
* @dataProvider transformWithRoundingProvider
*/
public function testTransformWithRoundingUsingLegacyConstructorSignature($input, $output, $roundingMode)
{ {
$transformer = new IntegerToLocalizedStringTransformer(null, null, $roundingMode); $transformer = new IntegerToLocalizedStringTransformer(null, null, $roundingMode);
@ -114,6 +126,25 @@ class IntegerToLocalizedStringTransformerTest extends TestCase
\Locale::setDefault('de_DE'); \Locale::setDefault('de_DE');
$transformer = new IntegerToLocalizedStringTransformer(true);
$this->assertEquals(1234, $transformer->reverseTransform('1.234,5'));
$this->assertEquals(12345, $transformer->reverseTransform('12.345,912'));
$this->assertEquals(1234, $transformer->reverseTransform('1234,5'));
$this->assertEquals(12345, $transformer->reverseTransform('12345,912'));
}
/**
* @group legacy
* @expectedDeprecation Passing a precision as the first value to %s::__construct() is deprecated since Symfony 4.2 and support for it will be dropped in 5.0.
*/
public function testReverseTransformWithGroupingUsingLegacyConstructorSignature()
{
// Since we test against "de_DE", we need the full implementation
IntlTestHelper::requireFullIntl($this, false);
\Locale::setDefault('de_DE');
$transformer = new IntegerToLocalizedStringTransformer(null, true); $transformer = new IntegerToLocalizedStringTransformer(null, true);
$this->assertEquals(1234, $transformer->reverseTransform('1.234,5')); $this->assertEquals(1234, $transformer->reverseTransform('1.234,5'));
@ -177,6 +208,18 @@ class IntegerToLocalizedStringTransformerTest extends TestCase
* @dataProvider reverseTransformWithRoundingProvider * @dataProvider reverseTransformWithRoundingProvider
*/ */
public function testReverseTransformWithRounding($input, $output, $roundingMode) public function testReverseTransformWithRounding($input, $output, $roundingMode)
{
$transformer = new IntegerToLocalizedStringTransformer(null, $roundingMode);
$this->assertEquals($output, $transformer->reverseTransform($input));
}
/**
* @group legacy
* @expectedDeprecation Passing a precision as the first value to %s::__construct() is deprecated since Symfony 4.2 and support for it will be dropped in 5.0.
* @dataProvider reverseTransformWithRoundingProvider
*/
public function testReverseTransformWithRoundingUsingLegacyConstructorSignature($input, $output, $roundingMode)
{ {
$transformer = new IntegerToLocalizedStringTransformer(null, null, $roundingMode); $transformer = new IntegerToLocalizedStringTransformer(null, null, $roundingMode);