You are here

protected static function NormalizerBase::rasterizeValueRecursive in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/src/Normalizer/NormalizerBase.php \Drupal\jsonapi\Normalizer\NormalizerBase::rasterizeValueRecursive()
  2. 9 core/modules/jsonapi/src/Normalizer/NormalizerBase.php \Drupal\jsonapi\Normalizer\NormalizerBase::rasterizeValueRecursive()

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.

File

core/modules/jsonapi/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;
}