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
;
18 * Transforms between a Boolean and a string.
20 * @author Bernhard Schussek <bschussek@gmail.com>
21 * @author Florian Eckerstorfer <florian@eckerstorfer.org>
23 class BooleanToStringTransformer
implements DataTransformerInterface
26 * The value emitted upon transform if the input is true
32 * Sets the value emitted upon transform if the input is true.
34 * @param string $trueValue
36 public function __construct($trueValue)
38 $this->trueValue
= $trueValue;
42 * Transforms a Boolean into a string.
44 * @param Boolean $value Boolean value.
46 * @return string String value.
48 * @throws TransformationFailedException If the given value is not a Boolean.
50 public function transform($value)
52 if (null === $value) {
56 if (!is_bool($value)) {
57 throw new TransformationFailedException('Expected a Boolean.');
60 return true === $value ? $this->trueValue
: null;
64 * Transforms a string into a Boolean.
66 * @param string $value String value.
68 * @return Boolean Boolean value.
70 * @throws TransformationFailedException If the given value is not a string.
72 public function reverseTransform($value)
74 if (null === $value) {
78 if (!is_string($value)) {
79 throw new TransformationFailedException('Expected a string.');