2011-02-16 17:23:41 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
2011-04-24 12:59:46 +01:00
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
2011-02-16 17:23:41 +00:00
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
2011-04-22 16:41:21 +01:00
|
|
|
namespace Symfony\Component\Form;
|
2011-02-16 17:23:41 +00:00
|
|
|
|
2013-09-18 13:41:53 +01:00
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException;
|
|
|
|
|
2011-03-18 15:01:06 +00:00
|
|
|
class CallbackTransformer implements DataTransformerInterface
|
2011-02-16 17:23:41 +00:00
|
|
|
{
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
|
|
|
* The callback used for forward transform
|
2014-08-06 04:51:22 +01:00
|
|
|
* @var callable
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2011-02-16 17:23:41 +00:00
|
|
|
private $transform;
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
|
|
|
* The callback used for reverse transform
|
2014-08-06 04:51:22 +01:00
|
|
|
* @var callable
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2011-02-16 17:23:41 +00:00
|
|
|
private $reverseTransform;
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
2014-08-06 04:51:22 +01:00
|
|
|
* @param callable $transform The forward transform callback
|
|
|
|
* @param callable $reverseTransform The reverse transform callback
|
|
|
|
*
|
|
|
|
* @throws \InvalidArgumentException when the given callbacks is invalid
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2014-08-06 04:51:22 +01:00
|
|
|
public function __construct($transform, $reverseTransform)
|
2011-02-16 17:23:41 +00:00
|
|
|
{
|
2014-08-06 04:51:22 +01:00
|
|
|
if (!is_callable($transform)) {
|
|
|
|
throw new \InvalidArgumentException('Argument 1 should be a callable');
|
|
|
|
}
|
|
|
|
if (!is_callable($reverseTransform)) {
|
|
|
|
throw new \InvalidArgumentException('Argument 2 should be a callable');
|
|
|
|
}
|
|
|
|
|
2011-02-16 17:23:41 +00:00
|
|
|
$this->transform = $transform;
|
|
|
|
$this->reverseTransform = $reverseTransform;
|
|
|
|
}
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
|
|
|
* Transforms a value from the original representation to a transformed representation.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param mixed $data The value in the original representation
|
2011-05-10 17:23:58 +01:00
|
|
|
*
|
2012-05-20 10:18:31 +01:00
|
|
|
* @return mixed The value in the transformed representation
|
2011-05-10 17:23:58 +01:00
|
|
|
*
|
2014-08-06 04:51:22 +01:00
|
|
|
* @throws UnexpectedTypeException when the argument is not of the expected type
|
2014-07-09 11:03:33 +01:00
|
|
|
* @throws TransformationFailedException when the transformation fails
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2011-02-16 17:23:41 +00:00
|
|
|
public function transform($data)
|
|
|
|
{
|
2011-05-12 17:22:36 +01:00
|
|
|
return call_user_func($this->transform, $data);
|
2011-02-16 17:23:41 +00:00
|
|
|
}
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
|
|
|
* Transforms a value from the transformed representation to its original
|
|
|
|
* representation.
|
|
|
|
*
|
2012-05-15 21:19:31 +01:00
|
|
|
* @param mixed $data The value in the transformed representation
|
2011-05-10 17:23:58 +01:00
|
|
|
*
|
2012-05-20 10:18:31 +01:00
|
|
|
* @return mixed The value in the original representation
|
2011-05-10 17:23:58 +01:00
|
|
|
*
|
2014-07-09 11:03:33 +01:00
|
|
|
* @throws UnexpectedTypeException when the argument is not of the expected type
|
|
|
|
* @throws TransformationFailedException when the transformation fails
|
2011-05-10 17:23:58 +01:00
|
|
|
*/
|
2011-02-16 17:23:41 +00:00
|
|
|
public function reverseTransform($data)
|
|
|
|
{
|
2011-05-12 17:22:36 +01:00
|
|
|
return call_user_func($this->reverseTransform, $data);
|
2011-02-16 17:23:41 +00:00
|
|
|
}
|
2011-06-08 11:16:48 +01:00
|
|
|
}
|