public function JsonApiDocumentTopLevelNormalizer::normalize in JSON:API 8.2
Same name and namespace in other branches
- 8 src/Normalizer/JsonApiDocumentTopLevelNormalizer.php \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer::normalize()
File
- src/
Normalizer/ JsonApiDocumentTopLevelNormalizer.php, line 175
Class
- JsonApiDocumentTopLevelNormalizer
- Normalizes the top-level document according to the JSON:API specification.
Namespace
Drupal\jsonapi\NormalizerCode
public function normalize($object, $format = NULL, array $context = []) {
assert($object instanceof JsonApiDocumentTopLevel);
$data = $object
->getData();
$document['jsonapi'] = CacheableNormalization::permanent([
'version' => JsonApiSpec::SUPPORTED_SPECIFICATION_VERSION,
'meta' => [
'links' => [
'self' => [
'href' => JsonApiSpec::SUPPORTED_SPECIFICATION_PERMALINK,
],
],
],
]);
if ($data instanceof ErrorCollection) {
$document['errors'] = $this
->normalizeErrorDocument($object, $format, $context);
}
else {
// Add data.
// @todo: remove this if-else and just call $this->serializer->normalize($data...) in https://www.drupal.org/project/jsonapi/issues/3036285.
if ($data instanceof EntityReferenceFieldItemListInterface) {
$document['data'] = $this
->normalizeEntityReferenceFieldItemList($object, $format, $context);
}
else {
$document['data'] = $this->serializer
->normalize($data, $format, $context);
}
// Add includes.
$document['included'] = $this->serializer
->normalize($object
->getIncludes(), $format, $context)
->omitIfEmpty();
// Add omissions and metadata.
$normalized_omissions = $this
->normalizeOmissionsLinks($object
->getOmissions(), $format, $context);
$meta = !$normalized_omissions instanceof CacheableOmission ? array_merge($object
->getMeta(), [
'omitted' => $normalized_omissions
->getNormalization(),
]) : $object
->getMeta();
$document['meta'] = (new CacheableNormalization($normalized_omissions, $meta))
->omitIfEmpty();
}
// Add document links.
$document['links'] = $this->serializer
->normalize($object
->getLinks(), $format, $context)
->omitIfEmpty();
// Every JSON:API document contains absolute URLs.
return CacheableNormalization::aggregate($document)
->withCacheableDependency((new CacheableMetadata())
->addCacheContexts([
'url.site',
]));
}