You are here

protected static function NormalizerBase::rasterizeValueRecursive in JSON:API 8.2

Rasterizes a value recursively.

This is mainly for configuration entities where a field can be a tree of values to rasterize.

Parameters

mixed $value: Either a scalar, an array or a rasterizable object.

Return value

mixed The rasterized value.

2 calls to NormalizerBase::rasterizeValueRecursive()
FieldItemNormalizer::normalize in src/Normalizer/FieldItemNormalizer.php
This normalizer leaves JSON:API normalizer land and enters the land of Drupal core's serialization system. That system was never designed with cacheability in mind, and hence bubbles cacheability out of band. This must catch it, and pass it to…
HttpExceptionNormalizer::normalize in src/Normalizer/HttpExceptionNormalizer.php
Normalizes an object into a set of arrays/scalars.

File

src/Normalizer/NormalizerBase.php, line 36

Class

NormalizerBase
Base normalizer used in all JSON:API normalizers.

Namespace

Drupal\jsonapi\Normalizer

Code

protected static function rasterizeValueRecursive($value) {
  if (!$value || is_scalar($value)) {
    return $value;
  }
  if (is_array($value)) {
    $output = [];
    foreach ($value as $key => $item) {
      $output[$key] = static::rasterizeValueRecursive($item);
    }
    return $output;
  }
  if ($value instanceof CacheableNormalization) {
    return $value
      ->getNormalization();
  }

  // If the object can be turned into a string it's better than nothing.
  if (method_exists($value, '__toString')) {
    return $value
      ->__toString();
  }

  // We give up, since we do not know how to rasterize this.
  return NULL;
}