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\Exception\TransformationFailedException
;
17 * Transforms between a normalized format and a localized money string.
19 * @author Bernhard Schussek <bschussek@gmail.com>
20 * @author Florian Eckerstorfer <florian@eckerstorfer.org>
22 class MoneyToLocalizedStringTransformer
extends NumberToLocalizedStringTransformer
27 public function __construct($precision = null, $grouping = null, $roundingMode = null, $divisor = null)
29 if (null === $grouping) {
33 if (null === $precision) {
37 parent
::__construct($precision, $grouping, $roundingMode);
39 if (null === $divisor) {
43 $this->divisor
= $divisor;
47 * Transforms a normalized format into a localized money string.
49 * @param number $value Normalized number
51 * @return string Localized money string.
53 * @throws TransformationFailedException If the given value is not numeric or
54 * if the value can not be transformed.
56 public function transform($value)
58 if (null !== $value) {
59 if (!is_numeric($value)) {
60 throw new TransformationFailedException('Expected a numeric.');
63 $value /= $this->divisor
;
66 return parent
::transform($value);
70 * Transforms a localized money string into a normalized format.
72 * @param string $value Localized money string
74 * @return number Normalized number
76 * @throws TransformationFailedException If the given value is not a string
77 * or if the value can not be transformed.
79 public function reverseTransform($value)
81 $value = parent
::reverseTransform($value);
83 if (null !== $value) {
84 $value *= $this->divisor
;