4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Form\Extension\Core\DataTransformer
;
14 use Symfony\Component\Form\DataTransformerInterface
;
15 use Symfony\Component\Form\Exception\TransformationFailedException
;
16 use Symfony\Component\Form\Exception\UnexpectedTypeException
;
19 * Transforms between a normalized format (integer or float) and a percentage value.
21 * @author Bernhard Schussek <bschussek@gmail.com>
22 * @author Florian Eckerstorfer <florian@eckerstorfer.org>
24 class PercentToLocalizedStringTransformer
implements DataTransformerInterface
26 const FRACTIONAL
= 'fractional';
27 const INTEGER = 'integer';
29 protected static $types = array(
41 * @see self::$types for a list of supported types
43 * @param integer $precision The precision
44 * @param string $type One of the supported types
46 * @throws UnexpectedTypeException if the given value of type is unknown
48 public function __construct($precision = null, $type = null)
50 if (null === $precision) {
55 $type = self
::FRACTIONAL
;
58 if (!in_array($type, self
::$types, true)) {
59 throw new UnexpectedTypeException($type, implode('", "', self
::$types));
63 $this->precision
= $precision;
67 * Transforms between a normalized format (integer or float) into a percentage value.
69 * @param number $value Normalized value
71 * @return number Percentage value
73 * @throws TransformationFailedException If the given value is not numeric or
74 * if the value could not be transformed.
76 public function transform($value)
78 if (null === $value) {
82 if (!is_numeric($value)) {
83 throw new TransformationFailedException('Expected a numeric.');
86 if (self
::FRACTIONAL
== $this->type
) {
90 $formatter = $this->getNumberFormatter();
91 $value = $formatter->format($value);
93 if (intl_is_failure($formatter->getErrorCode())) {
94 throw new TransformationFailedException($formatter->getErrorMessage());
97 // replace the UTF-8 non break spaces
102 * Transforms between a percentage value into a normalized format (integer or float).
104 * @param number $value Percentage value.
106 * @return number Normalized value.
108 * @throws TransformationFailedException If the given value is not a string or
109 * if the value could not be transformed.
111 public function reverseTransform($value)
113 if (!is_string($value)) {
114 throw new TransformationFailedException('Expected a string.');
121 $formatter = $this->getNumberFormatter();
122 // replace normal spaces so that the formatter can read them
123 $value = $formatter->parse(str_replace(' ', ' ', $value));
125 if (intl_is_failure($formatter->getErrorCode())) {
126 throw new TransformationFailedException($formatter->getErrorMessage());
129 if (self
::FRACTIONAL
== $this->type
) {
137 * Returns a preconfigured \NumberFormatter instance
139 * @return \NumberFormatter
141 protected function getNumberFormatter()
143 $formatter = new \
NumberFormatter(\Locale
::getDefault(), \NumberFormatter
::DECIMAL
);
145 $formatter->setAttribute(\NumberFormatter
::FRACTION_DIGITS
, $this->precision
);