8000 [Form] Added type check to `ScalarToChoiceTransformer` · symfony/symfony@95049ef · GitHub
[go: up one dir, main page]

Skip to content

Commit 95049ef

Browse files
committed
[Form] Added type check to ScalarToChoiceTransformer
1 parent a74ae9d commit 95049ef

File tree

2 files changed

+25
-0
lines changed

2 files changed

+0
lines changed

src/Symfony/Component/Form/Extension/Core/DataTransformer/ScalarToChoiceTransformer.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,25 @@
1313

1414
use Symfony\Component\Form\Util\FormUtil;
1515
use Symfony\Component\Form\DataTransformerInterface;
16+
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1617

1718
class ScalarToChoiceTransformer implements DataTransformerInterface
1819
{
1920
public function transform($value)
2021
{
22+
if (null !== $value && !is_scalar($value)) {
23+
throw new UnexpectedTypeException($value, 'scalar');
24+
}
25+
2126
return FormUtil::toArrayKey($value);
2227
}
2328

2429
public function reverseTransform($value)
2530
{
31+
if (null !== $value && !is_scalar($value)) {
32+
throw new UnexpectedTypeException($value, 'scalar');
33+
}
34+
2635
return $value;
2736
}
2837
}

tests/Symfony/Tests/Component/Form/Extension/Core/DataTransformer/ScalarToChoiceTransformerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,20 @@ public function testReverseTransform($in, $out)
6262
{
6363
$this->assertSame($out, $this->transformer->transform($in));
6464
}
65+
66+
/**
67+
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
68+
*/
69+
public function testTransformExpectsScalar()
70+
{
71+
$this->transformer->transform(array());
72+
}
73+
74+
/**
75+
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
76+
*/
77+
public function testReverseTransformExpectsScalar()
78+
{
79+
$this->transformer->reverseTransform(array());
80+
}
6581
}

0 commit comments

Comments
 (0)
0