|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Symfony package. |
|
|
5 |
* |
|
|
6 |
* (c) Fabien Potencier <fabien@symfony.com> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Symfony\Component\Form; |
|
|
13 |
|
|
|
14 |
class CallbackTransformer implements DataTransformerInterface |
|
|
15 |
{ |
|
|
16 |
/** |
|
|
17 |
* The callback used for forward transform |
|
|
18 |
* @var \Closure |
|
|
19 |
*/ |
|
|
20 |
private $transform; |
|
|
21 |
|
|
|
22 |
/** |
|
|
23 |
* The callback used for reverse transform |
|
|
24 |
* @var \Closure |
|
|
25 |
*/ |
|
|
26 |
private $reverseTransform; |
|
|
27 |
|
|
|
28 |
/** |
|
|
29 |
* Constructor. |
|
|
30 |
* |
|
|
31 |
* @param \Closure $transform The forward transform callback |
|
|
32 |
* @param \Closure $reverseTransform The reverse transform callback |
|
|
33 |
*/ |
|
|
34 |
public function __construct(\Closure $transform, \Closure $reverseTransform) |
|
|
35 |
{ |
|
|
36 |
$this->transform = $transform; |
|
|
37 |
$this->reverseTransform = $reverseTransform; |
|
|
38 |
} |
|
|
39 |
|
|
|
40 |
/** |
|
|
41 |
* Transforms a value from the original representation to a transformed representation. |
|
|
42 |
* |
|
|
43 |
* @param mixed $data The value in the original representation |
|
|
44 |
* |
|
|
45 |
* @return mixed The value in the transformed representation |
|
|
46 |
* |
|
|
47 |
* @throws UnexpectedTypeException when the argument is not a string |
|
|
48 |
* @throws TransformationFailedException when the transformation fails |
|
|
49 |
*/ |
|
|
50 |
public function transform($data) |
|
|
51 |
{ |
|
|
52 |
return call_user_func($this->transform, $data); |
|
|
53 |
} |
|
|
54 |
|
|
|
55 |
/** |
|
|
56 |
* Transforms a value from the transformed representation to its original |
|
|
57 |
* representation. |
|
|
58 |
* |
|
|
59 |
* @param mixed $data The value in the transformed representation |
|
|
60 |
* |
|
|
61 |
* @return mixed The value in the original representation |
|
|
62 |
* |
|
|
63 |
* @throws UnexpectedTypeException when the argument is not of the expected type |
|
|
64 |
* @throws TransformationFailedException when the transformation fails |
|
|
65 |
*/ |
|
|
66 |
public function reverseTransform($data) |
|
|
67 |
{ |
|
|
68 |
return call_user_func($this->reverseTransform, $data); |
|
|
69 |
} |
|
|
70 |
} |