Skip to content

Commit 38f70d5

Browse files
committed
Better subcollections handling
1 parent 28b1d40 commit 38f70d5

File tree

2 files changed

+27
-21
lines changed

2 files changed

+27
-21
lines changed

Mapping/ClassMetadataFactory.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,6 @@ public function getMetadataFor($value)
100100

101101
$metadata = new ClassMetadata($class, $serializerMetadata, $validatorMetadata, $doctrineMetadata);
102102

103-
$reflClass = $metadata->getReflectionClass();
104-
105-
// Include constraints from the parent class
106-
if ($parent = $reflClass->getParentClass()) {
107-
}
108-
109-
// Include constraints from all implemented interfaces
110-
foreach ($reflClass->getInterfaces() as $interface) {
111-
}
112-
113103
if ($this->cache) {
114104
$this->cache->save($class, $metadata);
115105
}

Serializer/JsonLdNormalizer.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ public function normalize($object, $format = null, array $context = [])
8888
return $this->handleCircularReference($object);
8989
}
9090

91-
if (!$resource = $this->guessResource($object, $context)) {
92-
throw new InvalidArgumentException('A resource object must be passed in the context.');
93-
}
91+
$resource = $this->guessResource($object, $context);
9492

9593
$data = [];
9694
if (!isset($context['has_json_ld_context'])) {
@@ -160,11 +158,15 @@ public function normalize($object, $format = null, array $context = [])
160158
if ('id' !== $attribute) {
161159
$attributeValue = $this->propertyAccessor->getValue($object, $attribute);
162160

163-
if (null !== $attributeValue && !is_scalar($attributeValue)) {
164-
$attributeValue = $this->serializer->normalize($attributeValue, $format, $context);
161+
if ($details['type']) {
162+
$attributeContext = $context;
163+
$attributeContext['resource'] = $this->resources->getResourceForEntity($details['type']);
164+
$data[$attribute] = $this->serializer->normalize($attributeValue, $format, $attributeContext);
165+
166+
continue;
165167
}
166168

167-
$data[$attribute] = $attributeValue;
169+
$data[$attribute] = $this->serializer->normalize($attributeValue, 'json', $context);
168170
}
169171
}
170172

@@ -214,21 +216,35 @@ public function denormalize($data, $class, $format = null, array $context = [])
214216
/**
215217
* Guesses the associated resource.
216218
*
217-
* @param mixed $object
219+
* @param mixed $type
218220
* @param array $context
219221
*
220222
* @return \Dunglas\JsonLdApiBundle\Resource|null
223+
*
224+
* @throws InvalidArgumentException
221225
*/
222-
private function guessResource($object, array $context)
226+
private function guessResource($type, array $context)
223227
{
224228
if (isset($context['resource'])) {
225229
return $context['resource'];
226230
}
227231

228-
if (is_object($object)) {
229-
return $this->resources->getResourceForEntity(get_class($object));
232+
if (is_object($type)) {
233+
$type = get_class($type);
234+
}
235+
236+
if (is_string($type)) {
237+
if ($resource = $this->resources->getResourceForEntity($type)) {
238+
return $resource;
239+
}
240+
241+
throw new InvalidArgumentException(
242+
sprintf('Cannot find a resource object for type "%s". Add a "resource" key in the context.', $type)
243+
);
230244
}
231245

232-
return;
246+
throw new InvalidArgumentException(
247+
sprintf('Cannot find a resource object for type "%s". Add a "resource" key in the context.', gettype($type))
248+
);
233249
}
234250
}

0 commit comments

Comments
 (0)