Skip to content

Commit f615650

Browse files
ISSUE-15: Implemented BooleanToStringTransformer
1 parent 5848a17 commit f615650

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

src/BooleanToStringTransformer.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Copyright (c) 2017 DarkWeb Design
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
17+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
18+
* SOFTWARE.
19+
*/
20+
21+
namespace DarkWebDesign\SymfonyAddon\Transformer;
22+
23+
use Symfony\Component\Form\DataTransformerInterface;
24+
use Symfony\Component\Form\Exception\TransformationFailedException;
25+
26+
/**
27+
* Transforms between a boolean and a string.
28+
*
29+
* @author Raymond Schouten
30+
*
31+
* @since 2.3
32+
*/
33+
class BooleanToStringTransformer implements DataTransformerInterface
34+
{
35+
/** @var string */
36+
private $trueValue;
37+
38+
/** @var string */
39+
private $falseValue;
40+
41+
/**
42+
* Constructor.
43+
*
44+
* @param string $trueValue
45+
* @param string $falseValue
46+
*/
47+
public function __construct($trueValue = 'true', $falseValue = 'false')
48+
{
49+
$this->trueValue = $trueValue;
50+
$this->falseValue = $falseValue;
51+
}
52+
53+
/**
54+
* Transforms a value from the original representation to a transformed representation.
55+
*
56+
* @param bool $value
57+
*
58+
* @return string
59+
*
60+
* @throws \Symfony\Component\Form\Exception\TransformationFailedException
61+
*/
62+
public function transform($value)
63+
{
64+
if (null === $value) {
65+
return null;
66+
}
67+
68+
if (!is_bool($value)) {
69+
throw new TransformationFailedException('Expected a boolean.');
70+
}
71+
72+
return $value ? $this->trueValue : $this->falseValue;
73+
}
74+
75+
/**
76+
* Transforms a value from the transformed representation to its original representation.
77+
*
78+
* @param string $value
79+
*
80+
* @return bool
81+
*
82+
* @throws \Symfony\Component\Form\Exception\TransformationFailedException
83+
*/
84+
public function reverseTransform($value)
85+
{
86+
if (null === $value || '' === $value) {
87+
return null;
88+
}
89+
90+
if (!is_scalar($value)) {
91+
throw new TransformationFailedException('Expected a scalar.');
92+
}
93+
94+
$value = (string) $value;
95+
96+
if (!($value === $this->trueValue || $value === $this->falseValue)) {
97+
throw new TransformationFailedException(
98+
sprintf('Expected a string "%s" or "%s".', $this->trueValue, $this->falseValue)
99+
);
100+
}
101+
102+
return $value === $this->trueValue;
103+
}
104+
}

0 commit comments

Comments
 (0)