diff options
author | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-03 19:26:54 +0200 |
---|---|---|
committer | Nicolas LÅ“uillet <nicolas.loeuillet@gmail.com> | 2013-08-03 19:26:54 +0200 |
commit | 4f5b44bd3bd490309eb2ba7b44df4769816ba729 (patch) | |
tree | 6cefe170dfe0a5a361cb1e2d1fc4d580a3316d02 /vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php | |
parent | 2b840e0cfb63a453bea67a98541f3df9c273c5f5 (diff) | |
download | wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.gz wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.tar.zst wallabag-4f5b44bd3bd490309eb2ba7b44df4769816ba729.zip |
twig implementation
Diffstat (limited to 'vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php')
-rw-r--r-- | vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php new file mode 100644 index 00000000..95e7332d --- /dev/null +++ b/vendor/symfony/form/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php | |||
@@ -0,0 +1,85 @@ | |||
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 | } | ||