You are here

protected static function IncludeResolver::buildTree in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/src/IncludeResolver.php \Drupal\jsonapi\IncludeResolver::buildTree()

Takes an array of exploded paths and builds a tree of field names.

Input example: [ ['one', 'two', 'three'], ['one', 'two', 'four'], ['one', 'two', 'internal'], ]

Output example: [ 'one' => [ 'two' [ 'three' => [], 'four' => [], 'internal' => [], ], ], ]

Parameters

array $paths: An array of exploded include paths.

Return value

array An multi-dimensional array representing a tree of field names to be included. Array keys are the field names. Leaves are empty arrays.

1 call to IncludeResolver::buildTree()
IncludeResolver::toIncludeTree in core/modules/jsonapi/src/IncludeResolver.php
Returns a tree of field names to include from an include parameter.

File

core/modules/jsonapi/src/IncludeResolver.php, line 244

Class

IncludeResolver
Resolves included resources for an entity or collection of entities.

Namespace

Drupal\jsonapi

Code

protected static function buildTree(array $paths) {
  $merged = [];
  foreach ($paths as $parts) {
    if (!($field_name = array_shift($parts))) {
      continue;
    }
    $previous = isset($merged[$field_name]) ? $merged[$field_name] : [];
    $merged[$field_name] = array_merge($previous, [
      $parts,
    ]);
  }
  return !empty($merged) ? array_map([
    static::class,
    __FUNCTION__,
  ], $merged) : $merged;
}