You are here

public function JsonApiDocumentTopLevelNormalizerValue::__construct in JSON:API 8

Instantiates a JsonApiDocumentTopLevelNormalizerValue object.

Parameters

\Drupal\Core\Entity\EntityInterface[] $values: The data to normalize. It can be either a straight up entity or a collection of entities.

array $context: The context.

array $link_context: All the objects and variables needed to generate the links for this relationship.

bool $is_collection: TRUE if this is a serialization for a list.

File

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

Class

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

Namespace

Drupal\jsonapi\Normalizer\Value

Code

public function __construct(array $values, array $context, array $link_context, $is_collection = FALSE) {
  $this->values = $values;
  array_walk($values, [
    $this,
    'addCacheableDependency',
  ]);

  // Make sure that different sparse fieldsets are cached differently.
  $this
    ->addCacheContexts(array_map(function ($query_parameter_name) {
    return sprintf('url.query_args:%s', $query_parameter_name);
  }, JsonApiSpec::getReservedQueryParameters()));

  // Every JSON API document contains absolute URLs.
  $this
    ->addCacheContexts([
    'url.site',
  ]);
  $this->context = $context;
  $this->isCollection = $is_collection;
  $this->linkManager = $link_context['link_manager'];

  // Remove the manager and store the link context.
  unset($link_context['link_manager']);
  $this->linkContext = $link_context;

  // Get an array of arrays of includes.
  $this->includes = array_map(function ($value) {
    return $value
      ->getIncludes();
  }, $values);

  // Flatten the includes.
  $this->includes = array_reduce($this->includes, function ($carry, $includes) {
    array_walk($includes, [
      $this,
      'addCacheableDependency',
    ]);
    return array_merge($carry, $includes);
  }, []);

  // Filter the empty values.
  $this->includes = array_filter($this->includes);
}