You are here

public function JsonApiDocumentTopLevelNormalizerValue::getIncludes in JSON:API 8

Gets a flattened list of includes in all the chain.

Return value

\Drupal\jsonapi\Normalizer\Value\EntityNormalizerValue[] The array of included relationships.

1 call to JsonApiDocumentTopLevelNormalizerValue::getIncludes()
JsonApiDocumentTopLevelNormalizerValue::rasterizeIncludes in src/Normalizer/Value/JsonApiDocumentTopLevelNormalizerValue.php
Get the includes.

File

src/Normalizer/Value/JsonApiDocumentTopLevelNormalizerValue.php, line 193

Class

JsonApiDocumentTopLevelNormalizerValue
Helps normalize the top level document in compliance with the JSON API spec.

Namespace

Drupal\jsonapi\Normalizer\Value

Code

public function getIncludes() {
  $nested_includes = array_map(function ($include) {
    return $include
      ->getIncludes();
  }, $this->includes);
  $includes = array_reduce(array_filter($nested_includes), function ($carry, $item) {
    return array_merge($carry, $item);
  }, $this->includes);

  // Make sure we don't output duplicate includes.
  return array_values(array_reduce($includes, function ($unique_includes, $include) {
    $rasterized_include = $include
      ->rasterizeValue();
    if ($rasterized_include['data'] === FALSE) {
      $unique_includes[] = $include;
    }
    else {
      $unique_key = $rasterized_include['data']['type'] . ':' . $rasterized_include['data']['id'];
      $unique_includes[$unique_key] = $include;
    }
    return $unique_includes;
  }, []));
}