function NormalizerBase::rasterizeValueRecursive

Same name and namespace in other branches
  1. 10 core/modules/jsonapi/src/Normalizer/NormalizerBase.php \Drupal\jsonapi\Normalizer\NormalizerBase::rasterizeValueRecursive()
  2. 11.x core/modules/jsonapi/src/Normalizer/NormalizerBase.php \Drupal\jsonapi\Normalizer\NormalizerBase::rasterizeValueRecursive()
  3. 9 core/modules/jsonapi/src/Normalizer/NormalizerBase.php \Drupal\jsonapi\Normalizer\NormalizerBase::rasterizeValueRecursive()
  4. 8.9.x 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.

2 calls to NormalizerBase::rasterizeValueRecursive()
FieldItemNormalizer::doNormalize in core/modules/jsonapi/src/Normalizer/FieldItemNormalizer.php
Normalizes data into a set of arrays/scalars.
HttpExceptionNormalizer::normalize in core/modules/jsonapi/src/Normalizer/HttpExceptionNormalizer.php
Normalizes data into a set of arrays/scalars.

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

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