-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DoctrineBridge] Add decimal form type #24793
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Form\DataTransformer; | ||
|
||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
|
||
class NumberToStringTransformer implements DataTransformerInterface | ||
{ | ||
/** | ||
* @var bool | ||
*/ | ||
private $forceFullScale; | ||
|
||
/** | ||
* @var int|null | ||
*/ | ||
private $scale; | ||
|
||
/** | ||
* @param bool $forceFullScale | ||
* @param int|null $scale | ||
*/ | ||
public function __construct($forceFullScale = false, $scale = null) | ||
{ | ||
$this->forceFullScale = $forceFullScale; | ||
$this->scale = $scale; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* | ||
* @return string|null | ||
*/ | ||
public function transform($value) | ||
{ | ||
if (null === $value) { | ||
return null; | ||
} | ||
|
||
if (!is_string($value)) { | ||
throw new TransformationFailedException('Expected a string.'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably be reflected in the |
||
} | ||
|
||
return $value; | ||
} | ||
|
||
/** | ||
* @param mixed $value | ||
* | ||
* @return string|null | ||
*/ | ||
public function reverseTransform($value) | ||
{ | ||
if (null === $value) { | ||
return null; | ||
} | ||
|
||
if (is_string($value)) { | ||
return $value; | ||
} | ||
|
||
$valueIsInt = is_int($value); | ||
if (!$valueIsInt && !is_float($value)) { | ||
throw new TransformationFailedException('Expected an int or a float.'); | ||
} | ||
|
||
if ($this->forceFullScale && is_int($this->scale)) { | ||
if ($valueIsInt) { | ||
$value = floatval($value); | ||
} | ||
|
||
return number_format($value, $this->scale, '.', ''); | ||
} | ||
|
||
try { | ||
return (string) $value; | ||
} catch (\Exception $e) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should be |
||
throw new TransformationFailedException(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previous exception should be passed on here |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Form\Type; | ||
|
||
use Symfony\Bridge\Doctrine\Form\DataTransformer\NumberToStringTransformer; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\NumberType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class DecimalType extends AbstractType | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
$builder->addModelTransformer(new NumberToStringTransformer($options['force_full_scale'], $options['scale'])); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults(array( | ||
'force_full_scale' => false | ||
)); | ||
$resolver->setAllowedTypes('force_full_scale', array( | ||
'boolean' | ||
)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParent() | ||
{ | ||
return NumberType::class; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Tests\Fixtures; | ||
|
||
use Doctrine\ORM\Mapping\Entity; | ||
use Doctrine\ORM\Mapping\Id; | ||
use Doctrine\ORM\Mapping\Column; | ||
|
||
/** @Entity */ | ||
class Price | ||
{ | ||
/** @Id @Column(type="integer") */ | ||
public $id; | ||
|
||
/** @Column(type="decimal", scale=2) */ | ||
public $doesNotPreserveFullScaleValue; | ||
|
||
/** @Column(type="string") */ | ||
public $preserveFullScaleValueSimulation; | ||
|
||
/** | ||
* @param int $id | ||
* @param float $value | ||
*/ | ||
public function __construct(int $id, float $value) | ||
{ | ||
$this->id = $id; | ||
$this->doesNotPreserveFullScaleValue = $value; | ||
$this->preserveFullScaleValueSimulation = number_format($value, 2, '.', ''); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bridge\Doctrine\Tests\Form\Type; | ||
|
||
use Doctrine\ORM\EntityManager; | ||
use Doctrine\ORM\Tools\SchemaTool; | ||
use Symfony\Bridge\Doctrine\Form\Type\DecimalType; | ||
use Symfony\Bridge\Doctrine\Test\DoctrineTestHelper; | ||
use Symfony\Bridge\Doctrine\Tests\Fixtures\Price; | ||
use Symfony\Component\Form\Extension\Core\Type\FormType; | ||
use Symfony\Component\Form\Tests\Extension\Core\Type\BaseTypeTest; | ||
|
||
class DecimalTypeTest extends BaseTypeTest | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
const TESTED_TYPE = DecimalType::class; | ||
|
||
/** | ||
* @var EntityManager | ||
*/ | ||
private $em; | ||
|
||
protected function setUp() | ||
{ | ||
$this->em = DoctrineTestHelper::createTestEntityManager(); | ||
|
||
parent::setUp(); | ||
|
||
$schemaTool = new SchemaTool($this->em); | ||
$classes = array( | ||
$this->em->getClassMetadata(Price::class) | ||
); | ||
|
||
try { | ||
$schemaTool->dropSchema($classes); | ||
} catch (\Exception $e) { | ||
} | ||
|
||
try { | ||
$schemaTool->createSchema($classes); | ||
} catch (\Exception $e) { | ||
} | ||
} | ||
|
||
protected function tearDown() | ||
{ | ||
parent::tearDown(); | ||
|
||
$this->em = null; | ||
} | ||
|
||
// On some platforms, fetched decimal values are rounded (the full scale is not preserved) | ||
// eg : on SQLite, inserted float value 4.50 will be fetched as string value "4.5" | ||
public function testSubmitWithSameStringValueOnAPlatformThatDoesNotPreserveFullScaleValueWithoutForceFullScale() | ||
{ | ||
$fullScalePrice = new Price(1, 1.23); | ||
$nonFullScalePrice = new Price(2, 4.50); | ||
$this->em->persist($fullScalePrice); | ||
$this->em->persist($nonFullScalePrice); | ||
$this->em->flush(); | ||
|
||
$this->em->refresh($fullScalePrice); | ||
$this->em->refresh($nonFullScalePrice); | ||
|
||
$this->assertInternalType('string', $fullScalePrice->doesNotPreserveFullScaleValue); | ||
$fullScalePriceStringValue = $fullScalePrice->doesNotPreserveFullScaleValue; | ||
|
||
$formBuilder = $this->factory->createBuilder(FormType::class, $fullScalePrice, array( | ||
'data_class' => Price::class | ||
)); | ||
$formBuilder->add('doesNotPreserveFullScaleValue', static::TESTED_TYPE, array( | ||
'force_full_scale' => false | ||
)); | ||
|
||
$form = $formBuilder->getForm(); | ||
$form->submit(array( | ||
'doesNotPreserveFullScaleValue' => $fullScalePriceStringValue | ||
)); | ||
|
||
$this->assertSame($fullScalePriceStringValue, $fullScalePrice->doesNotPreserveFullScaleValue); | ||
|
||
$this->assertInternalType('string', $nonFullScalePrice->doesNotPreserveFullScaleValue); | ||
$nonFullScalePriceStringValue = $nonFullScalePrice->doesNotPreserveFullScaleValue; | ||
|
||
$formBuilder = $this->factory->createBuilder(FormType::class, $nonFullScalePrice, array( | ||
'data_class' => Price::class | ||
)); | ||
$formBuilder->add('doesNotPreserveFullScaleValue', static::TESTED_TYPE, array( | ||
'force_full_scale' => false | ||
)); | ||
|
||
$form = $formBuilder->getForm(); | ||
$form->submit(array( | ||
'doesNotPreserveFullScaleValue' => $nonFullScalePriceStringValue | ||
)); | ||
|
||
$this->assertSame($nonFullScalePriceStringValue, $nonFullScalePrice->doesNotPreserveFullScaleValue); | ||
|
||
$unitOfWork = $this->em->getUnitOfWork(); | ||
$unitOfWork->computeChangeSets(); | ||
|
||
$this->assertSame(array(), $unitOfWork->getEntityChangeSet($fullScalePrice)); | ||
$this->assertSame(array(), $unitOfWork->getEntityChangeSet($nonFullScalePrice)); | ||
} | ||
|
||
// On some platforms, fetched decimal values are not rounded at all (the full scale is preserved) | ||
// eg : on PostgreSQL, inserted float value 4.50 will be fetched as string value "4.50" | ||
public function testSubmitWithSameStringValueOnAPlatformThatPreserveFullScaleValueWithForceFullScale() | ||
{ | ||
$fullScalePrice = new Price(1, 1.23); | ||
$nonFullScalePrice = new Price(2, 4.50); | ||
$this->em->persist($fullScalePrice); | ||
$this->em->persist($nonFullScalePrice); | ||
$this->em->flush(); | ||
|
||
$this->em->refresh($fullScalePrice); | ||
$this->em->refresh($nonFullScalePrice); | ||
|
||
$this->assertInternalType('string', $fullScalePrice->preserveFullScaleValueSimulation); | ||
$fullScalePriceStringValue = $fullScalePrice->preserveFullScaleValueSimulation; | ||
|
||
$formBuilder = $this->factory->createBuilder(FormType::class, $fullScalePrice, array( | ||
'data_class' => Price::class | ||
)); | ||
$formBuilder->add('preserveFullScaleValueSimulation', static::TESTED_TYPE, array( | ||
'force_full_scale' => true, | ||
'scale' => 2 | ||
)); | ||
|
||
$form = $formBuilder->getForm(); | ||
$form->submit(array( | ||
'preserveFullScaleValueSimulation' => $fullScalePriceStringValue | ||
)); | ||
|
||
$this->assertSame($fullScalePriceStringValue, $fullScalePrice->preserveFullScaleValueSimulation); | ||
|
||
$this->assertInternalType('string', $nonFullScalePrice->preserveFullScaleValueSimulation); | ||
$nonFullScalePriceStringValue = $nonFullScalePrice->preserveFullScaleValueSimulation; | ||
|
||
$formBuilder = $this->factory->createBuilder(FormType::class, $nonFullScalePrice, array( | ||
'data_class' => Price::class | ||
)); | ||
$formBuilder->add('preserveFullScaleValueSimulation', static::TESTED_TYPE, array( | ||
'force_full_scale' => true, | ||
'scale' => 2 | ||
)); | ||
|
||
$form = $formBuilder->getForm(); | ||
$form->submit(array( | ||
'preserveFullScaleValueSimulation' => $nonFullScalePriceStringValue | ||
)); | ||
|
||
$this->assertSame($nonFullScalePriceStringValue, $nonFullScalePrice->preserveFullScaleValueSimulation); | ||
|
||
$unitOfWork = $this->em->getUnitOfWork(); | ||
$unitOfWork->computeChangeSets(); | ||
|
||
$this->assertSame(array(), $unitOfWork->getEntityChangeSet($fullScalePrice)); | ||
$this->assertSame(array(), $unitOfWork->getEntityChangeSet($nonFullScalePrice)); | ||
} | ||
|
||
public function testSubmitNull($expected = null, $norm = null, $view = null) | ||
{ | ||
parent::testSubmitNull($expected, $norm, ''); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs documentation