function DataNormalizer::normalize

Same name and namespace in other branches
  1. 10 core/modules/jsonapi/src/Normalizer/DataNormalizer.php \Drupal\jsonapi\Normalizer\DataNormalizer::normalize()
  2. 11.x core/modules/jsonapi/src/Normalizer/DataNormalizer.php \Drupal\jsonapi\Normalizer\DataNormalizer::normalize()
  3. 9 core/modules/jsonapi/src/Normalizer/DataNormalizer.php \Drupal\jsonapi\Normalizer\DataNormalizer::normalize()
  4. 8.9.x core/modules/jsonapi/src/Normalizer/DataNormalizer.php \Drupal\jsonapi\Normalizer\DataNormalizer::normalize()

Normalizes data into a set of arrays/scalars.

Parameters

\Drupal\jsonapi\JsonApiResource\Data $object: Data to normalize.

string|null $format: Format the normalization result will be encoded as.

array<string, mixed> $context: Context options for the normalizer.

Return value

array|string|int|float|bool|\ArrayObject<mixed, mixed>|null \ArrayObject is used to make sure an empty object is encoded as an object not an array.

File

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

Class

DataNormalizer
Normalizes JSON:API Data objects.

Namespace

Drupal\jsonapi\Normalizer

Code

public function normalize($object, $format = NULL, array $context = []) : array|string|int|float|bool|\ArrayObject|null {
  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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.