-
Notifications
You must be signed in to change notification settings - Fork 352
/
PercentToLocalizedStringTransformer.php
54 lines (45 loc) · 1.38 KB
/
PercentToLocalizedStringTransformer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace Oro\Bundle\FormBundle\Form\DataTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\PercentToLocalizedStringTransformer as BaseTransformer;
/**
* Fix for standard transformer that supports precision = null
* and if the conversion type is "fractional", convert strings represent an integer value to float instead of integer.
*/
class PercentToLocalizedStringTransformer extends BaseTransformer
{
/** @var int|null */
private $precision;
/** @var string|null */
private $type;
/**
* {@inheritDoc}
*/
public function __construct($precision = null, $type = null)
{
parent::__construct($precision, $type);
$this->precision = $precision;
$this->type = $type;
}
/**
* {@inheritDoc}
*/
public function reverseTransform($value)
{
$result = parent::reverseTransform($value);
if (is_int($result) && self::FRACTIONAL === $this->type) {
$result = (float)$result;
}
return $result;
}
/**
* {@inheritDoc}
*/
protected function getNumberFormatter()
{
$formatter = new \NumberFormatter(\Locale::getDefault(), \NumberFormatter::DECIMAL);
if (null !== $this->precision) {
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->precision);
}
return $formatter;
}
}