1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\MediaBundle\Form\Type; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Collection; |
6
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
7
|
|
|
use Kunstmaan\MediaBundle\Entity\Media; |
8
|
|
|
use Symfony\Component\Form\DataTransformerInterface; |
9
|
|
|
use Symfony\Component\Form\Exception\TransformationFailedException; |
10
|
|
|
use Symfony\Component\Form\Exception\UnexpectedTypeException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* IdToMediaTransformer |
14
|
|
|
*/ |
15
|
|
|
class IdToMediaTransformer implements DataTransformerInterface |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var ObjectManager |
19
|
|
|
*/ |
20
|
|
|
private $objectManager; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var CurrentValueContainer |
24
|
|
|
*/ |
25
|
|
|
private $currentValueContainer; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ObjectManager $objectManager The object manager |
29
|
|
|
* @param CurrentValueContainer $currentValueContainer The current value container |
30
|
|
|
*/ |
31
|
|
|
public function __construct(ObjectManager $objectManager, CurrentValueContainer $currentValueContainer) |
32
|
|
|
{ |
33
|
|
|
$this->objectManager = $objectManager; |
34
|
|
|
$this->currentValueContainer = $currentValueContainer; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param Media $entity The value in the original representation |
39
|
|
|
* |
40
|
|
|
* @return mixed The value in the transformed representation |
|
|
|
|
41
|
|
|
* |
42
|
|
|
* @throws UnexpectedTypeException when the argument is not an object |
43
|
|
|
* @throws \InvalidArgumentException when the parameter is a collection |
44
|
|
|
*/ |
45
|
|
|
public function transform($entity) |
46
|
|
|
{ |
47
|
|
|
if (empty($entity)) { |
48
|
|
|
return ''; |
49
|
|
|
} |
50
|
|
|
if (!\is_object($entity)) { |
51
|
|
|
throw new UnexpectedTypeException($entity, 'object'); |
52
|
|
|
} |
53
|
|
|
if ($entity instanceof Collection) { |
54
|
|
|
throw new \InvalidArgumentException('Expected an object, but got a collection. Did you forget to pass "multiple=true" to an entity field?'); |
55
|
|
|
} |
56
|
|
|
$this->currentValueContainer->setCurrentValue($entity); |
57
|
|
|
|
58
|
|
|
return array( |
59
|
|
|
'ent' => $entity, |
60
|
|
|
'id' => $entity->getId(), |
61
|
|
|
); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $key |
66
|
|
|
* |
67
|
|
|
* @return Media |
|
|
|
|
68
|
|
|
* |
69
|
|
|
* @throws UnexpectedTypeException when the parameter is not numeric |
70
|
|
|
* @throws TransformationFailedException when the media item cannot be loaded/found |
71
|
|
|
*/ |
72
|
|
|
public function reverseTransform($key) |
73
|
|
|
{ |
74
|
|
|
if (empty($key)) { |
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
if (!is_numeric($key)) { |
78
|
|
|
throw new UnexpectedTypeException($key, 'numeric'); |
79
|
|
|
} |
80
|
|
|
if (!($entity = $this->objectManager->getRepository(Media::class)->find($key))) { |
81
|
|
|
throw new TransformationFailedException(sprintf('The entity with key "%s" could not be found', $key)); |
82
|
|
|
} |
83
|
|
|
$this->currentValueContainer->setCurrentValue($entity); |
84
|
|
|
|
85
|
|
|
return $entity; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.