for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/*
* This file is part of the Lug package.
*
* (c) Eric GELOEN <[email protected]>
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/
namespace Lug\Bundle\ResourceBundle\Form\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
/**
* @author GeLo <[email protected]>
class BooleanTransformer implements DataTransformerInterface
{
* {@inheritdoc}
public function transform($value)
if ($value === null) {
return;
}
if (!is_bool($value)) {
throw new TransformationFailedException(sprintf(
'The boolean type expects a boolean or null value, got "%s"',
is_object($value) ? get_class($value) : gettype($value)
));
return $value;
public function reverseTransform($value)
if ($value === null || $value === '') {
return false;
if (!is_scalar($value)
|| ($value = filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)) === null) {
throw new TransformationFailedException('The boolean value is not valid.');