-
-
Notifications
You must be signed in to change notification settings - Fork 586
/
ArrayCollectionHandler.php
161 lines (132 loc) · 5.02 KB
/
ArrayCollectionHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
declare(strict_types=1);
namespace JMS\Serializer\Handler;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ODM\MongoDB\PersistentCollection as MongoPersistentCollection;
use Doctrine\ODM\PHPCR\PersistentCollection as PhpcrPersistentCollection;
use Doctrine\ORM\PersistentCollection as OrmPersistentCollection;
use Doctrine\Persistence\ManagerRegistry;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\GraphNavigatorInterface;
use JMS\Serializer\Metadata\PropertyMetadata;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
use JMS\Serializer\Visitor\SerializationVisitorInterface;
final class ArrayCollectionHandler implements SubscribingHandlerInterface
{
public const COLLECTION_TYPES = [
'ArrayCollection',
ArrayCollection::class,
OrmPersistentCollection::class,
MongoPersistentCollection::class,
PhpcrPersistentCollection::class,
];
/**
* @var bool
*/
private $initializeExcluded;
/**
* @var ManagerRegistry|null
*/
private $managerRegistry;
public function __construct(
bool $initializeExcluded = true,
?ManagerRegistry $managerRegistry = null
) {
$this->initializeExcluded = $initializeExcluded;
$this->managerRegistry = $managerRegistry;
}
/**
* {@inheritdoc}
*/
public static function getSubscribingMethods()
{
$methods = [];
$formats = ['json', 'xml'];
foreach (self::COLLECTION_TYPES as $type) {
foreach ($formats as $format) {
$methods[] = [
'direction' => GraphNavigatorInterface::DIRECTION_SERIALIZATION,
'type' => $type,
'format' => $format,
'method' => 'serializeCollection',
];
$methods[] = [
'direction' => GraphNavigatorInterface::DIRECTION_DESERIALIZATION,
'type' => $type,
'format' => $format,
'method' => 'deserializeCollection',
];
}
}
return $methods;
}
/**
* @return array|\ArrayObject
*/
public function serializeCollection(SerializationVisitorInterface $visitor, Collection $collection, array $type, SerializationContext $context)
{
// We change the base type, and pass through possible parameters.
$type['name'] = 'array';
$context->stopVisiting($collection);
if (false === $this->initializeExcluded) {
$exclusionStrategy = $context->getExclusionStrategy();
if (null !== $exclusionStrategy && $exclusionStrategy->shouldSkipClass($context->getMetadataFactory()->getMetadataForClass(\get_class($collection)), $context)) {
$context->startVisiting($collection);
return $visitor->visitArray([], $type);
}
}
$result = $visitor->visitArray($collection->toArray(), $type);
$context->startVisiting($collection);
return $result;
}
/**
* @param mixed $data
*/
public function deserializeCollection(
DeserializationVisitorInterface $visitor,
$data,
array $type,
DeserializationContext $context
): Collection {
// See above.
$type['name'] = 'array';
$elements = new ArrayCollection($visitor->visitArray($data, $type));
if (null === $this->managerRegistry) {
return $elements;
}
$propertyMetadata = $context->getMetadataStack()->top();
if (!$propertyMetadata instanceof PropertyMetadata) {
return $elements;
}
$objectManager = $this->managerRegistry->getManagerForClass($propertyMetadata->class);
if (null === $objectManager) {
return $elements;
}
$classMetadata = $objectManager->getClassMetadata($propertyMetadata->class);
$currentObject = $visitor->getCurrentObject();
if (
array_key_exists('name', $propertyMetadata->type)
&& in_array($propertyMetadata->type['name'], self::COLLECTION_TYPES)
&& $classMetadata->isCollectionValuedAssociation($propertyMetadata->name)
) {
$existingCollection = $classMetadata->getFieldValue($currentObject, $propertyMetadata->name);
if (!$existingCollection instanceof OrmPersistentCollection) {
return $elements;
}
foreach ($elements as $element) {
if (!$existingCollection->contains($element)) {
$existingCollection->add($element);
}
}
foreach ($existingCollection as $collectionElement) {
if (!$elements->contains($collectionElement)) {
$existingCollection->removeElement($collectionElement);
}
}
return $existingCollection;
}
return $elements;
}
}