Skip to content

Commit

Permalink
rename the model_type option to input and rework it
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Apr 29, 2024
1 parent 706fe13 commit a492b5c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ CHANGELOG
* Add option `separator` to `ChoiceType` to use a custom separator after preferred choices (use the new `separator_html` option to display the separator text as HTML)
* Deprecate not configuring the `default_protocol` option of the `UrlType`, it will default to `null` in 8.0 (the current default is `'http'`)
* Add a `keep_as_list` option to `CollectionType`
* Add a new `model_type` option to `MoneyType`, to be able to cast the transformed value to `integer`
* Add an `input` option to `MoneyType`, to be able to cast the transformed value to `integer`

7.0
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(
?int $roundingMode = \NumberFormatter::ROUND_HALFUP,
?int $divisor = 1,
?string $locale = null,
private string $modelType = 'float',
private readonly string $input = 'float',
) {
parent::__construct($scale ?? 2, $grouping ?? true, $roundingMode, $locale);

Expand Down Expand Up @@ -67,15 +67,15 @@ public function transform(mixed $value): string
public function reverseTransform(mixed $value): int|float|null
{
$value = parent::reverseTransform($value);
if (null !== $value && 1 !== $this->divisor) {
if (null !== $value) {
$value = (string) ($value * $this->divisor);

if ('float' === $this->modelType) {
if ('float' === $this->input) {
return (float) $value;
}

if ($value > \PHP_INT_MAX || $value < \PHP_INT_MIN) {
throw new TransformationFailedException(sprintf("The value '%d' is too large you should pass the 'model_type' to 'float'.", $value));
throw new TransformationFailedException(sprintf('Cannot cast "%s" to an integer. Try setting the input to "float" instead.', $value));
}

$value = (int) $value;
Expand Down
14 changes: 3 additions & 11 deletions Extension/Core/Type/MoneyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$options['rounding_mode'],
$options['divisor'],
$options['html5'] ? 'en' : null,
$options['model_type'],
$options['input'],
))
;
}
Expand All @@ -60,7 +60,7 @@ public function configureOptions(OptionsResolver $resolver): void
'compound' => false,
'html5' => false,
'invalid_message' => 'Please enter a valid money amount.',
'model_type' => 'float',
'input' => 'float',
]);

$resolver->setAllowedValues('rounding_mode', [
Expand All @@ -77,7 +77,7 @@ public function configureOptions(OptionsResolver $resolver): void

$resolver->setAllowedTypes('html5', 'bool');

$resolver->setAllowedValues('model_type', ['float', 'integer']);
$resolver->setAllowedValues('input', ['float', 'integer']);

$resolver->setNormalizer('grouping', static function (Options $options, $value) {
if ($value && $options['html5']) {
Expand All @@ -86,14 +86,6 @@ public function configureOptions(OptionsResolver $resolver): void

return $value;
});

$resolver->setNormalizer('model_type', static function (Options $options, $value) {
if ('integer' === $value && 1 === $options['divisor']) {
throw new LogicException('When the "model_type" option is set to "integer", the "divisor" option should not be set to "1".');
}

return $value;
});
}

public function getBlockPrefix(): string
Expand Down
14 changes: 7 additions & 7 deletions Tests/Extension/Core/Type/MoneyTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Intl\Util\IntlTestHelper;

class MoneyTypeTest extends BaseTypeTestCase
Expand Down Expand Up @@ -125,26 +124,27 @@ public function testHtml5EnablesSpecificFormatting()
$this->assertSame('number', $form->createView()->vars['type']);
}

public function testDefaultModelType()
public function testDefaultInput()
{
$form = $this->factory->create(static::TESTED_TYPE, null, ['divisor' => 100]);
$form->submit('12345.67');

$this->assertSame(1234567.0, $form->getData());
}

public function testIntegerModelType()
public function testIntegerInput()
{
$form = $this->factory->create(static::TESTED_TYPE, null, ['divisor' => 100, 'model_type' => 'integer']);
$form = $this->factory->create(static::TESTED_TYPE, null, ['divisor' => 100, 'input' => 'integer']);
$form->submit('12345.67');

$this->assertSame(1234567, $form->getData());
}

public function testIntegerModelTypeExpectsDivisorNotEqualToOne()
public function testIntegerInputWithoutDivisor()
{
$this->expectException(LogicException::class);
$form = $this->factory->create(static::TESTED_TYPE, null, ['input' => 'integer']);
$form->submit('1234567');

$form = $this->factory->create(static::TESTED_TYPE, null, ['divisor' => 1, 'model_type' => 'integer']);
$this->assertSame(1234567, $form->getData());
}
}

0 comments on commit a492b5c

Please sign in to comment.