You are here

class EntityNormalizerValue in JSON:API 8

Helps normalize entities in compliance with the JSON API spec.

@internal

Hierarchy

Expanded class hierarchy of EntityNormalizerValue

2 files declare their use of EntityNormalizerValue
EntityNormalizer.php in src/Normalizer/EntityNormalizer.php
EntityNormalizerValueTest.php in tests/src/Unit/Normalizer/Value/EntityNormalizerValueTest.php

File

src/Normalizer/Value/EntityNormalizerValue.php, line 14

Namespace

Drupal\jsonapi\Normalizer\Value
View source
class EntityNormalizerValue implements ValueExtractorInterface, CacheableDependencyInterface {
  use CacheableDependencyTrait;
  use CacheableDependenciesMergerTrait;

  /**
   * The values.
   *
   * @var array
   */
  protected $values;

  /**
   * The includes.
   *
   * @var array
   */
  protected $includes;

  /**
   * The resource path.
   *
   * @var array
   */
  protected $context;

  /**
   * The resource entity.
   *
   * @var \Drupal\Core\Entity\EntityInterface
   */
  protected $entity;

  /**
   * The link manager.
   *
   * @var \Drupal\jsonapi\LinkManager\LinkManager
   */
  protected $linkManager;

  /**
   * Instantiate a EntityNormalizerValue object.
   *
   * @param FieldNormalizerValueInterface[] $values
   *   The normalized result.
   * @param array $context
   *   The context for the normalizer.
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity.
   * @param array $link_context
   *   All the objects and variables needed to generate the links for this
   *   relationship.
   */
  public function __construct(array $values, array $context, EntityInterface $entity, array $link_context) {
    $this
      ->setCacheability(static::mergeCacheableDependencies(array_merge([
      $entity,
    ], $values)));

    // Gather includes from all normalizer values, before filtering away null
    // and include-only normalizer values.
    $this->includes = array_map(function ($value) {
      return $value
        ->getIncludes();
    }, $values);
    $this->values = array_filter($values, function ($value) {
      return !($value instanceof NullFieldNormalizerValue || $value instanceof IncludeOnlyRelationshipNormalizerValue);
    });
    $this->context = $context;
    $this->entity = $entity;
    $this->linkManager = $link_context['link_manager'];

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

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

  /**
   * {@inheritdoc}
   */
  public function rasterizeValue() {

    // Create the array of normalized fields, starting with the URI.
    $rasterized = [
      'type' => $this->context['resource_type']
        ->getTypeName(),
      'id' => $this->entity
        ->uuid(),
      'attributes' => [],
      'relationships' => [],
    ];
    $rasterized['links'] = [
      'self' => $this->linkManager
        ->getEntityLink($rasterized['id'], $this->context['resource_type'], [], 'individual'),
    ];
    foreach ($this
      ->getValues() as $field_name => $normalizer_value) {
      $rasterized[$normalizer_value
        ->getPropertyType()][$field_name] = $normalizer_value
        ->rasterizeValue();
    }
    return array_filter($rasterized);
  }

  /**
   * {@inheritdoc}
   */
  public function rasterizeIncludes() {

    // First gather all the includes in the chain.
    return array_map(function ($include) {
      return $include
        ->rasterizeValue();
    }, $this
      ->getIncludes());
  }

  /**
   * Gets the values.
   *
   * @return mixed
   *   The values.
   */
  public function getValues() {
    return $this->values;
  }

  /**
   * Gets a flattened list of includes in all the chain.
   *
   * @return \Drupal\jsonapi\Normalizer\Value\EntityNormalizerValue[]
   *   The array of included relationships.
   */
  public function getIncludes() {
    $nested_includes = array_map(function ($include) {
      return $include
        ->getIncludes();
    }, $this->includes);
    return array_reduce(array_filter($nested_includes), function ($carry, $item) {
      return array_merge($carry, $item);
    }, $this->includes);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableDependenciesMergerTrait::mergeCacheableDependencies protected static function Determines the joint cacheability of all provided dependencies.
CacheableDependencyTrait::$cacheContexts protected property Cache contexts.
CacheableDependencyTrait::$cacheMaxAge protected property Cache max-age.
CacheableDependencyTrait::$cacheTags protected property Cache tags.
CacheableDependencyTrait::getCacheContexts public function
CacheableDependencyTrait::getCacheMaxAge public function
CacheableDependencyTrait::getCacheTags public function
CacheableDependencyTrait::setCacheability protected function Sets cacheability; useful for value object constructors.
EntityNormalizerValue::$context protected property The resource path.
EntityNormalizerValue::$entity protected property The resource entity.
EntityNormalizerValue::$includes protected property The includes.
EntityNormalizerValue::$linkManager protected property The link manager.
EntityNormalizerValue::$values protected property The values.
EntityNormalizerValue::getIncludes public function Gets a flattened list of includes in all the chain.
EntityNormalizerValue::getValues public function Gets the values.
EntityNormalizerValue::rasterizeIncludes public function Get the includes. Overrides ValueExtractorInterface::rasterizeIncludes
EntityNormalizerValue::rasterizeValue public function Get the rasterized value. Overrides ValueExtractorInterface::rasterizeValue
EntityNormalizerValue::__construct public function Instantiate a EntityNormalizerValue object.