|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\MediaBundle\DataTransformer; |
|
4
|
|
|
|
|
5
|
|
|
use Alpixel\Bundle\MediaBundle\Entity\Media; |
|
6
|
|
|
use Doctrine\Common\Collections\Collection; |
|
7
|
|
|
use Doctrine\ORM\EntityManager; |
|
8
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
9
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
|
10
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
|
11
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Data transformation class. |
|
15
|
|
|
* |
|
16
|
|
|
* @author Gregwar <[email protected]> |
|
17
|
|
|
*/ |
|
18
|
|
|
class EntityToIdTransformer implements DataTransformerInterface |
|
19
|
|
|
{ |
|
20
|
|
|
protected $em; |
|
21
|
|
|
private $class; |
|
22
|
|
|
private $multiple; |
|
23
|
|
|
|
|
24
|
|
|
public function __construct(EntityManager $em, $class, $property, $queryBuilder, $multiple) |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
if (!(null === $queryBuilder || $queryBuilder instanceof QueryBuilder || $queryBuilder instanceof \Closure)) { |
|
|
|
|
|
|
27
|
|
|
throw new UnexpectedTypeException($queryBuilder, 'Doctrine\ORM\QueryBuilder or \Closure'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
if (null === $class) { |
|
31
|
|
|
throw new UnexpectedTypeException($class, 'string'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
$this->em = $em; |
|
35
|
|
|
$this->class = $class; |
|
36
|
|
|
$this->multiple = $multiple; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function transform($data) |
|
40
|
|
|
{ |
|
41
|
|
|
if ($data instanceof Collection) { |
|
42
|
|
|
return $this->reverseTransform($data); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
if (null === $data) { |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
if (!$this->multiple) { |
|
50
|
|
|
return $this->transformSingleEntity($data); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if (is_array($data)) |
|
54
|
|
|
return $data; |
|
55
|
|
|
|
|
56
|
|
|
$return = []; |
|
57
|
|
|
$data = explode('#&#', $data); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($data as $element) { |
|
60
|
|
|
$return[] = $this->transformSingleEntity($element); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $return; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function transformSingleEntity($data) |
|
67
|
|
|
{ |
|
68
|
|
|
if ($data instanceof Media) { |
|
69
|
|
|
return $data->getSecretKey(); |
|
70
|
|
|
} else { |
|
71
|
|
|
return $this->em->getRepository($this->class)->findOneBySecretKey($data); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function reverseTransform($data) |
|
76
|
|
|
{ |
|
77
|
|
|
if (!$data) { |
|
78
|
|
|
return; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (!($data instanceof Collection)) { |
|
82
|
|
|
return $this->transform($data); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$return = []; |
|
86
|
|
|
|
|
87
|
|
|
foreach ($data as $element) { |
|
88
|
|
|
$return[] = $this->reverseTransformSingleEntity($element); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return implode('#&#', $return); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
protected function reverseTransformSingleEntity($data) |
|
95
|
|
|
{ |
|
96
|
|
|
if (!($data instanceof Media)) { |
|
97
|
|
|
throw new TransformationFailedException('Can not find entity'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $data->getSecretKey(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.