You are here

protected function JsonApiDocumentTopLevelNormalizer::normalizeErrorDocument in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer::normalizeErrorDocument()

Normalizes an error collection.

@todo: refactor this to use CacheableNormalization::aggregate in https://www.drupal.org/project/drupal/issues/3036284.

Parameters

\Drupal\jsonapi\JsonApiResource\JsonApiDocumentTopLevel $document: The document to normalize.

string $format: The normalization format.

array $context: The normalization context.

Return value

\Drupal\jsonapi\Normalizer\Value\CacheableNormalization The normalized document.

1 call to JsonApiDocumentTopLevelNormalizer::normalizeErrorDocument()
JsonApiDocumentTopLevelNormalizer::normalize in core/modules/jsonapi/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php

File

core/modules/jsonapi/src/Normalizer/JsonApiDocumentTopLevelNormalizer.php, line 224

Class

JsonApiDocumentTopLevelNormalizer
Normalizes the top-level document according to the JSON:API specification.

Namespace

Drupal\jsonapi\Normalizer

Code

protected function normalizeErrorDocument(JsonApiDocumentTopLevel $document, $format, array $context = []) {
  $normalized_values = array_map(function (HttpExceptionInterface $exception) use ($format, $context) {
    return $this->serializer
      ->normalize($exception, $format, $context);
  }, (array) $document
    ->getData()
    ->getIterator());
  $cacheability = new CacheableMetadata();
  $errors = [];
  foreach ($normalized_values as $normalized_error) {
    $cacheability
      ->addCacheableDependency($normalized_error);
    $errors = array_merge($errors, $normalized_error
      ->getNormalization());
  }
  return new CacheableNormalization($cacheability, $errors);
}