]> git.immae.eu Git - github/wallabag/wallabag.git/blob - vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php
twig implementation
[github/wallabag/wallabag.git] / vendor / symfony / form / Symfony / Component / Form / Extension / Core / DataTransformer / BooleanToStringTransformer.php
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\Form\Extension\Core\DataTransformer;
13
14 use Symfony\Component\Form\DataTransformerInterface;
15 use Symfony\Component\Form\Exception\TransformationFailedException;
16
17 /**
18 * Transforms between a Boolean and a string.
19 *
20 * @author Bernhard Schussek <bschussek@gmail.com>
21 * @author Florian Eckerstorfer <florian@eckerstorfer.org>
22 */
23 class BooleanToStringTransformer implements DataTransformerInterface
24 {
25 /**
26 * The value emitted upon transform if the input is true
27 * @var string
28 */
29 private $trueValue;
30
31 /**
32 * Sets the value emitted upon transform if the input is true.
33 *
34 * @param string $trueValue
35 */
36 public function __construct($trueValue)
37 {
38 $this->trueValue = $trueValue;
39 }
40
41 /**
42 * Transforms a Boolean into a string.
43 *
44 * @param Boolean $value Boolean value.
45 *
46 * @return string String value.
47 *
48 * @throws TransformationFailedException If the given value is not a Boolean.
49 */
50 public function transform($value)
51 {
52 if (null === $value) {
53 return null;
54 }
55
56 if (!is_bool($value)) {
57 throw new TransformationFailedException('Expected a Boolean.');
58 }
59
60 return true === $value ? $this->trueValue : null;
61 }
62
63 /**
64 * Transforms a string into a Boolean.
65 *
66 * @param string $value String value.
67 *
68 * @return Boolean Boolean value.
69 *
70 * @throws TransformationFailedException If the given value is not a string.
71 */
72 public function reverseTransform($value)
73 {
74 if (null === $value) {
75 return false;
76 }
77
78 if (!is_string($value)) {
79 throw new TransformationFailedException('Expected a string.');
80 }
81
82 return true;
83 }
84
85 }