-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[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 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
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 |
---|---|---|
|
@@ -11,11 +11,11 @@ | |
|
||
namespace Symfony\Bridge\Doctrine\Form\Type; | ||
|
||
use Symfony\Bridge\Doctrine\Form\DataTransformer\NumberToStringTransformer; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\CallbackTransformer; | ||
use Symfony\Component\Form\Exception\TransformationFailedException; | ||
use Symfony\Component\Form\Extension\Core\Type\NumberType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
class DecimalType extends AbstractType | ||
{ | ||
|
@@ -24,27 +24,20 @@ class DecimalType extends AbstractType | |
*/ | ||
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 CallbackTransformer(function ($value) { | ||
if (null === $value) { | ||
return null; | ||
} | ||
|
||
if (!is_string($value)) { | ||
throw new TransformationFailedException('Expected a string.'); | ||
} | ||
|
||
return $value; | ||
}, function ($value) { | ||
if (null === $value) { | ||
return null; | ||
} | ||
|
||
if (!is_int($value) && !is_float($value)) { | ||
throw new TransformationFailedException('Expected an int or a float.'); | ||
} | ||
$builder->addModelTransformer(new NumberToStringTransformer($options['force_full_scale'], $options['scale'])); | ||
} | ||
|
||
return (string) $value; | ||
})); | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configureOptions(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults(array( | ||
'force_full_scale' => false | ||
)); | ||
$resolver->setAllowedTypes('force_full_scale', array( | ||
'boolean' | ||
)); | ||
} | ||
|
||
/** | ||
|
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