|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace JD\FormBundle\Form\DataTransformer; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ObjectRepository; |
|
6
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
|
7
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
|
8
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
9
|
|
|
|
|
10
|
|
|
final class EntityToIdTransformer implements DataTransformerInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
private $class; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $property; |
|
21
|
|
|
/** |
|
22
|
|
|
* @var ObjectRepository |
|
23
|
|
|
*/ |
|
24
|
|
|
private $repository; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param ObjectRepository $repository |
|
28
|
|
|
* @param string $class |
|
29
|
|
|
* @param string $property |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(ObjectRepository $repository, $class, $property = 'id') |
|
32
|
|
|
{ |
|
33
|
|
|
$this->class = $class; |
|
34
|
|
|
$this->property = $property; |
|
35
|
|
|
$this->repository = $repository; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Transforms a value from the original representation to a transformed representation. |
|
40
|
|
|
* |
|
41
|
|
|
* This method is called on two occasions inside a form field: |
|
42
|
|
|
* |
|
43
|
|
|
* 1. When the form field is initialized with the data attached from the datasource (object or array). |
|
44
|
|
|
* 2. When data from a request is submitted using {@link Form::submit()} to transform the new input data |
|
45
|
|
|
* back into the renderable format. For example if you have a date field and submit '2009-10-10' |
|
46
|
|
|
* you might accept this value because its easily parsed, but the transformer still writes back |
|
47
|
|
|
* "2009/10/10" onto the form field (for further displaying or other purposes). |
|
48
|
|
|
* |
|
49
|
|
|
* This method must be able to deal with empty values. Usually this will |
|
50
|
|
|
* be NULL, but depending on your implementation other empty values are |
|
51
|
|
|
* possible as well (such as empty strings). The reasoning behind this is |
|
52
|
|
|
* that value transformers must be chainable. If the transform() method |
|
53
|
|
|
* of the first value transformer outputs NULL, the second value transformer |
|
54
|
|
|
* must be able to process that value. |
|
55
|
|
|
* |
|
56
|
|
|
* By convention, transform() should return an empty string if NULL is |
|
57
|
|
|
* passed. |
|
58
|
|
|
* |
|
59
|
|
|
* @param mixed $entity The value in the original representation |
|
60
|
|
|
* |
|
61
|
|
|
* @return mixed The value in the transformed representation |
|
62
|
|
|
* |
|
63
|
|
|
* @throws TransformationFailedException When the transformation fails. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function transform($entity) |
|
66
|
|
|
{ |
|
67
|
|
|
if (null === $entity) { |
|
68
|
|
|
return; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
|
72
|
|
|
|
|
73
|
|
|
return $accessor->getValue($entity, $this->property); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Transforms a value from the transformed representation to its original |
|
78
|
|
|
* representation. |
|
79
|
|
|
* |
|
80
|
|
|
* This method is called when {@link Form::submit()} is called to transform the requests tainted data |
|
81
|
|
|
* into an acceptable format for your data processing/model layer. |
|
82
|
|
|
* |
|
83
|
|
|
* This method must be able to deal with empty values. Usually this will |
|
84
|
|
|
* be an empty string, but depending on your implementation other empty |
|
85
|
|
|
* values are possible as well (such as empty strings). The reasoning behind |
|
86
|
|
|
* this is that value transformers must be chainable. If the |
|
87
|
|
|
* reverseTransform() method of the first value transformer outputs an |
|
88
|
|
|
* empty string, the second value transformer must be able to process that |
|
89
|
|
|
* value. |
|
90
|
|
|
* |
|
91
|
|
|
* By convention, reverseTransform() should return NULL if an empty string |
|
92
|
|
|
* is passed. |
|
93
|
|
|
* |
|
94
|
|
|
* @param mixed $identifier The value in the transformed representation |
|
95
|
|
|
* |
|
96
|
|
|
* @return object|null The value in the original representation |
|
97
|
|
|
* |
|
98
|
|
|
* @throws TransformationFailedException When the transformation fails. |
|
99
|
|
|
*/ |
|
100
|
|
|
public function reverseTransform($identifier) |
|
101
|
|
|
{ |
|
102
|
|
|
if (!$identifier) { |
|
103
|
|
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
$entity = $this->repository->find($identifier); |
|
107
|
|
|
|
|
108
|
|
|
if (null === $entity) { |
|
109
|
|
|
throw new TransformationFailedException(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return $entity; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|