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
|
|
|
|
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
|
|
|
|
* @var \Closure
|
|
|
|
*/
|
2011-02-16 17:23:41 +00:00
|
|
|
private $transform;
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
|
|
|
* The callback used for reverse transform
|
|
|
|
* @var \Closure
|
|
|
|
*/
|
2011-02-16 17:23:41 +00:00
|
|
|
private $reverseTransform;
|
|
|
|
|
2011-05-10 17:23:58 +01:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*
|
|
|
|
* @param \Closure $transform The forward transform callback
|
|
|
|
* @param \Closure $reverseTransform The reverse transform callback
|
|
|
|
*/
|
2011-02-16 17:23:41 +00:00
|
|
|
public function __construct(\Closure $transform, \Closure $reverseTransform)
|
|
|
|
{
|
|
|
|
$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.
|
|
|
|
*
|
|
|
|
* @param mixed $value The value in the original representation
|
|
|
|
*
|
|
|
|
* @return mixed The value in the transformed representation
|
|
|
|
*
|
|
|
|
* @throws UnexpectedTypeException when the argument is not a string
|
|
|
|
* @throws DataTransformerException when the transformation fails
|
|
|
|
*/
|
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.
|
|
|
|
*
|
|
|
|
* @param mixed $value The value in the transformed representation
|
|
|
|
*
|
|
|
|
* @return mixed The value in the original representation
|
|
|
|
*
|
|
|
|
* @throws UnexpectedTypeException when the argument is not of the expected type
|
|
|
|
* @throws DataTransformerException when the transformation fails
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
}
|