You are here

public function DataNormalizer::normalize in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/Normalizer/DataNormalizer.php \Drupal\jsonapi\Normalizer\DataNormalizer::normalize()

File

core/modules/jsonapi/src/Normalizer/DataNormalizer.php, line 23

Class

DataNormalizer
Normalizes JSON:API Data objects.

Namespace

Drupal\jsonapi\Normalizer

Code

public function normalize($object, $format = NULL, array $context = []) {
  assert($object instanceof Data);
  $cacheable_normalizations = array_map(function ($resource) use ($format, $context) {
    return $this->serializer
      ->normalize($resource, $format, $context);
  }, $object
    ->toArray());
  return $object
    ->getCardinality() === 1 ? array_shift($cacheable_normalizations) ?: CacheableNormalization::permanent(NULL) : CacheableNormalization::aggregate($cacheable_normalizations);
}