You are here

protected function FieldItemNormalizerValue::rasterizeValueRecursive in JSON:API 8

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 FieldItemNormalizerValue::rasterizeValueRecursive()
ConfigFieldItemNormalizerValue::rasterizeValue in src/Normalizer/Value/ConfigFieldItemNormalizerValue.php
FieldItemNormalizerValue::rasterizeValue in src/Normalizer/Value/FieldItemNormalizerValue.php

File

src/Normalizer/Value/FieldItemNormalizerValue.php, line 65

Class

FieldItemNormalizerValue
Helps normalize field items in compliance with the JSON API spec.

Namespace

Drupal\jsonapi\Normalizer\Value

Code

protected function rasterizeValueRecursive($value) {
  if (!$value || is_scalar($value)) {
    return $value;
  }
  if (is_array($value)) {
    $output = [];
    foreach ($value as $key => $item) {
      $output[$key] = $this
        ->rasterizeValueRecursive($item);
    }
    return $output;
  }
  if ($value instanceof ValueExtractorInterface) {
    return $value
      ->rasterizeValue();
  }

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