|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tpg\ExtjsBundle\Listener; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
6
|
|
|
use Doctrine\ORM\Mapping\JoinColumn; |
|
7
|
|
|
use JMS\Serializer\EventDispatcher\PreDeserializeEvent; |
|
8
|
|
|
use JMS\Serializer\Metadata\PropertyMetadata; |
|
9
|
|
|
|
|
10
|
|
|
class DeserializationAssociateRelationship { |
|
11
|
|
|
|
|
12
|
|
|
/** @var AnnotationReader */ |
|
13
|
|
|
protected $reader; |
|
14
|
|
|
|
|
15
|
30 |
|
public function __construct($reader) { |
|
16
|
30 |
|
$this->reader = $reader; |
|
17
|
30 |
|
} |
|
18
|
|
|
|
|
19
|
30 |
|
public function onSerializerPreDeserialize(PreDeserializeEvent $e) { |
|
20
|
30 |
|
$className = $e->getType(); |
|
21
|
30 |
|
$className = $className['name']; |
|
22
|
|
|
/** Handle ArrayCollection or array JMS type. */ |
|
23
|
30 |
|
if ($className == "ArrayCollection" || $className == "array") { |
|
24
|
5 |
|
$className = $e->getType(); |
|
25
|
5 |
|
$className = $className['params'][0]['name']; |
|
26
|
5 |
|
} |
|
27
|
30 |
|
$classMetadata = $e->getContext()->getMetadataFactory()->getMetadataForClass($className); |
|
28
|
|
|
/** @var PropertyMetadata $propertyMetadata */ |
|
29
|
30 |
|
foreach ($classMetadata->propertyMetadata as $propertyMetadata) { |
|
30
|
30 |
|
if ($propertyMetadata->reflection === null) continue; |
|
31
|
|
|
/** @var JoinColumn $joinColumnAnnotation */ |
|
32
|
30 |
|
$joinColumnAnnotation = $this->reader->getPropertyAnnotation( |
|
33
|
30 |
|
$propertyMetadata->reflection, |
|
34
|
|
|
'Doctrine\ORM\Mapping\JoinColumn' |
|
35
|
30 |
|
); |
|
36
|
30 |
|
if ($joinColumnAnnotation !== null) { |
|
37
|
15 |
|
if (array_key_exists($joinColumnAnnotation->name, $e->getData())) { |
|
38
|
|
|
$idValue = $e->getData(); |
|
39
|
|
|
$idValue = $idValue[$joinColumnAnnotation->name]; |
|
40
|
|
|
$referencedColumnName = $joinColumnAnnotation->referencedColumnName ?: 'id'; |
|
41
|
|
|
if ($idValue !== null && $idValue !== '') { |
|
42
|
|
|
$e->setData( |
|
43
|
|
|
$e->getData() + array($propertyMetadata->name => array($referencedColumnName => $idValue)) |
|
44
|
|
|
); |
|
45
|
|
|
} else { |
|
46
|
|
|
$e->setData( |
|
47
|
|
|
$e->getData() + array($propertyMetadata->name => null) |
|
48
|
|
|
); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
15 |
|
} |
|
52
|
30 |
|
} |
|
53
|
|
|
} |
|
54
|
|
|
} |