You are here

protected function JsonApiDocumentTopLevelNormalizer::normalizeErrorDocument in JSON:API 8.2

Normalizes an error collection.

@todo: refactor this to use CacheableNormalization::aggregate in https://www.drupal.org/project/jsonapi/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 src/Normalizer/JsonApiDocumentTopLevelNormalizer.php
Normalizes an object into a set of arrays/scalars.

File

src/Normalizer/JsonApiDocumentTopLevelNormalizer.php, line 230

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);
}