merged branch stloyd/ScalarTransformer (PR #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: -
This commit is contained in:
commit
f22566ca19
@ -13,16 +13,25 @@ namespace Symfony\Component\Form\Extension\Core\DataTransformer;
|
||||
|
||||
use Symfony\Component\Form\Util\FormUtil;
|
||||
use Symfony\Component\Form\DataTransformerInterface;
|
||||
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
||||
|
||||
class ScalarToChoiceTransformer implements DataTransformerInterface
|
||||
{
|
||||
public function transform($value)
|
||||
{
|
||||
if (null !== $value && !is_scalar($value)) {
|
||||
throw new UnexpectedTypeException($value, 'scalar');
|
||||
}
|
||||
|
||||
return FormUtil::toArrayKey($value);
|
||||
}
|
||||
|
||||
public function reverseTransform($value)
|
||||
{
|
||||
if (null !== $value && !is_scalar($value)) {
|
||||
throw new UnexpectedTypeException($value, 'scalar');
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
@ -62,4 +62,20 @@ class ScalarToChoiceTransformerTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$this->assertSame($out, $this->transformer->transform($in));
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
|
||||
*/
|
||||
public function testTransformExpectsScalar()
|
||||
{
|
||||
$this->transformer->transform(array());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
|
||||
*/
|
||||
public function testReverseTransformExpectsScalar()
|
||||
{
|
||||
$this->transformer->reverseTransform(array());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user