You are here

class RelationshipItemNormalizerValue in JSON:API 8

Helps normalize relationship items in compliance with the JSON API spec.

@internal

Hierarchy

Expanded class hierarchy of RelationshipItemNormalizerValue

4 files declare their use of RelationshipItemNormalizerValue
FieldNormalizerValueTest.php in tests/src/Unit/Normalizer/Value/FieldNormalizerValueTest.php
RelationshipItemNormalizer.php in src/Normalizer/RelationshipItemNormalizer.php
RelationshipItemNormalizerValueTest.php in tests/src/Unit/Normalizer/Value/RelationshipItemNormalizerValueTest.php
RelationshipNormalizerValueTest.php in tests/src/Unit/Normalizer/Value/RelationshipNormalizerValueTest.php

File

src/Normalizer/Value/RelationshipItemNormalizerValue.php, line 12

Namespace

Drupal\jsonapi\Normalizer\Value
View source
class RelationshipItemNormalizerValue extends FieldItemNormalizerValue implements ValueExtractorInterface, CacheableDependencyInterface {
  use CacheableDependenciesMergerTrait;

  /**
   * Resource path.
   *
   * @var string
   */
  protected $resource;

  /**
   * Included normalized entity, if any.
   *
   * @var \Drupal\jsonapi\Normalizer\Value\EntityNormalizerValue|\Drupal\jsonapi\Normalizer\Value\JsonApiDocumentTopLevelNormalizerValue|\Drupal\jsonapi\Normalizer\Value\HttpExceptionNormalizerValue|null
   */
  protected $include;

  /**
   * Instantiates a RelationshipItemNormalizerValue object.
   *
   * @param array $values
   *   The values.
   * @param \Drupal\Core\Cache\CacheableDependencyInterface $values_cacheability
   *   The cacheability of the normalized result. This cacheability is not part
   *   of $values because field items are normalized by Drupal core's
   *   serialization system, which was never designed with cacheability in mind.
   *   FieldItemNormalizer::normalize() must catch the out-of-band bubbled
   *   cacheability and then passes it to this value object.
   * @param string $resource
   *   The resource type of the target entity.
   * @param \Drupal\jsonapi\Normalizer\Value\EntityNormalizerValue|\Drupal\jsonapi\Normalizer\Value\JsonApiDocumentTopLevelNormalizerValue|\Drupal\jsonapi\Normalizer\Value\HttpExceptionNormalizerValue|null $include
   *   The included normalized entity, or NULL.
   */
  public function __construct(array $values, CacheableDependencyInterface $values_cacheability, $resource, $include) {
    assert($include === NULL || $include instanceof EntityNormalizerValue || $include instanceof JsonApiDocumentTopLevelNormalizerValue || $include instanceof HttpExceptionNormalizerValue);
    parent::__construct($values, $values_cacheability);
    if ($include !== NULL) {
      $this
        ->setCacheability(static::mergeCacheableDependencies([
        $include,
        $values_cacheability,
      ]));
    }
    $this->resource = $resource;
    $this->include = $include;
  }

  /**
   * {@inheritdoc}
   */
  public function rasterizeValue() {
    if (!($value = parent::rasterizeValue())) {
      return $value;
    }
    $rasterized_value = [
      'type' => $this->resource
        ->getTypeName(),
      'id' => empty($value['target_uuid']) ? $value : $value['target_uuid'],
    ];
    if (!empty($value['meta'])) {
      $rasterized_value['meta'] = $value['meta'];
    }
    return $rasterized_value;
  }

  /**
   * {@inheritdoc}
   */
  public function rasterizeIncludes() {
    return $this->include
      ->rasterizeValue();
  }

  /**
   * Gets the include.
   *
   * @return \Drupal\jsonapi\Normalizer\Value\EntityNormalizerValue
   *   The include.
   */
  public function getInclude() {
    return $this->include;
  }

}

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.
FieldItemNormalizerValue::$raw protected property Raw values. 1
FieldItemNormalizerValue::rasterizeValueRecursive protected function Rasterizes a value recursively.
RelationshipItemNormalizerValue::$include protected property Included normalized entity, if any.
RelationshipItemNormalizerValue::$resource protected property Resource path.
RelationshipItemNormalizerValue::getInclude public function Gets the include.
RelationshipItemNormalizerValue::rasterizeIncludes public function Get the includes. Overrides ValueExtractorInterface::rasterizeIncludes
RelationshipItemNormalizerValue::rasterizeValue public function Get the rasterized value. Overrides FieldItemNormalizerValue::rasterizeValue
RelationshipItemNormalizerValue::__construct public function Instantiates a RelationshipItemNormalizerValue object. Overrides FieldItemNormalizerValue::__construct