24
24
use Symfony \Component \Form \Exception \TransformationFailedException ;
25
25
26
26
/**
27
- * Transforms between a boolean and a string .
27
+ * Transforms between a boolean and a scalar value .
28
28
*
29
29
* @author Raymond Schouten
30
30
*
31
31
* @since 2.3
32
32
*/
33
- class BooleanToStringTransformer implements DataTransformerInterface
33
+ class BooleanToValueTransformer implements DataTransformerInterface
34
34
{
35
35
/** @var string */
36
36
private $ trueValue ;
@@ -41,10 +41,10 @@ class BooleanToStringTransformer implements DataTransformerInterface
41
41
/**
42
42
* Constructor.
43
43
*
44
- * @param string $trueValue
45
- * @param string $falseValue
44
+ * @param string|int|float|bool $trueValue
45
+ * @param string|int|float|bool $falseValue
46
46
*/
47
- public function __construct ($ trueValue = ' true ' , $ falseValue = ' false ' )
47
+ public function __construct ($ trueValue = true , $ falseValue = false )
48
48
{
49
49
$ this ->trueValue = $ trueValue ;
50
50
$ this ->falseValue = $ falseValue ;
@@ -55,7 +55,7 @@ public function __construct($trueValue = 'true', $falseValue = 'false')
55
55
*
56
56
* @param bool $value
57
57
*
58
- * @return string
58
+ * @return string|int|float|bool
59
59
*
60
60
* @throws \Symfony\Component\Form\Exception\TransformationFailedException
61
61
*/
@@ -75,7 +75,7 @@ public function transform($value)
75
75
/**
76
76
* Transforms a value from the transformed representation to its original representation.
77
77
*
78
- * @param string $value
78
+ * @param string|int|float|bool $value
79
79
*
80
80
* @return bool
81
81
*
@@ -91,12 +91,8 @@ public function reverseTransform($value)
91
91
throw new TransformationFailedException ('Expected a scalar. ' );
92
92
}
93
93
94
- $ value = (string ) $ value ;
95
-
96
94
if (!($ value === $ this ->trueValue || $ value === $ this ->falseValue )) {
97
- throw new TransformationFailedException (
98
- sprintf ('Expected a string "%s" or "%s". ' , $ this ->trueValue , $ this ->falseValue )
99
- );
95
+ throw new TransformationFailedException ('Expected true/false boolean value. ' );
100
96
}
101
97
102
98
return $ value === $ this ->trueValue ;
0 commit comments