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:
Fabien Potencier 2011-10-07 14:06:24 +02:00
commit f22566ca19
2 changed files with 25 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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());
}
}