8000 merged branch stloyd/ScalarTransformer (PR #2341) · sammanjac/symfony@f22566c · GitHub
[go: up one dir, main page]

Skip to content

Commit f22566c

Browse files
committed
merged branch stloyd/ScalarTransformer (PR symfony#2341)
Commits ------- 95049ef [Form] Added type check to `ScalarToChoiceTransformer` Discussion ---------- [2.0][Form] Added type check to ScalarToChoiceTransformer Bug fix: yes Feature addition: no Backwards compatibility break: no Symfony2 tests pass: yes Fixes the following tickets: -
2 parents 4cdf6ac + 95049ef commit f22566c

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-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