You are here

class FieldNormalizerValue in JSON:API 8

Helps normalize fields in compliance with the JSON API spec.

@internal

Hierarchy

Expanded class hierarchy of FieldNormalizerValue

4 files declare their use of FieldNormalizerValue
ConfigEntityNormalizer.php in src/Normalizer/ConfigEntityNormalizer.php
FieldNormalizer.php in src/Normalizer/FieldNormalizer.php
FieldNormalizerValueTest.php in tests/src/Unit/Normalizer/Value/FieldNormalizerValueTest.php
SerializerTest.php in tests/src/Kernel/Serializer/SerializerTest.php

File

src/Normalizer/Value/FieldNormalizerValue.php, line 13

Namespace

Drupal\jsonapi\Normalizer\Value
View source
class FieldNormalizerValue implements FieldNormalizerValueInterface {
  use CacheableDependencyTrait;
  use CacheableDependenciesMergerTrait;

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

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

  /**
   * The field cardinality.
   *
   * @var int
   */
  protected $cardinality;

  /**
   * The property type. Either: 'attributes' or `relationships'.
   *
   * @var string
   */
  protected $propertyType;

  /**
   * Instantiate a FieldNormalizerValue object.
   *
   * @param \Drupal\Core\Access\AccessResultInterface $field_access_result
   *   The field access result.
   * @param \Drupal\jsonapi\Normalizer\Value\FieldItemNormalizerValue[] $values
   *   The normalized result.
   * @param int $cardinality
   *   The cardinality of the field list.
   * @param string $property_type
   *   The property type of the field: 'attributes' or 'relationships'.
   */
  public function __construct(AccessResultInterface $field_access_result, array $values, $cardinality, $property_type) {
    assert($property_type === 'attributes' || $property_type === 'relationships');
    $this
      ->setCacheability(static::mergeCacheableDependencies(array_merge([
      $field_access_result,
    ], $values)));
    $this->values = $values;
    $this->includes = array_map(function ($value) {
      if (!$value instanceof RelationshipItemNormalizerValue) {
        return NULL;
      }
      return $value
        ->getInclude();
    }, $values);
    $this->includes = array_filter($this->includes);
    $this->cardinality = $cardinality;
    $this->propertyType = $property_type;
  }

  /**
   * {@inheritdoc}
   */
  public function rasterizeValue() {
    if (empty($this->values)) {
      return NULL;
    }
    if ($this->cardinality == 1) {
      assert(count($this->values) === 1);
      return $this->values[0] instanceof FieldItemNormalizerValue ? $this->values[0]
        ->rasterizeValue() : NULL;
    }
    return array_map(function ($value) {
      return $value instanceof FieldItemNormalizerValue ? $value
        ->rasterizeValue() : NULL;
    }, $this->values);
  }

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

  /**
   * {@inheritdoc}
   */
  public function getIncludes() {
    return $this->includes;
  }

  /**
   * {@inheritdoc}
   */
  public function getPropertyType() {
    return $this->propertyType;
  }

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

    // Make sure we don't output duplicate includes.
    return array_values(array_reduce($includes, function ($unique_includes, $include) {
      $rasterized_include = $include
        ->rasterizeValue();
      $unique_includes[$rasterized_include['data']['type'] . ':' . $rasterized_include['data']['id']] = $include;
      return $unique_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.
FieldNormalizerValue::$cardinality protected property The field cardinality.
FieldNormalizerValue::$includes protected property The includes.
FieldNormalizerValue::$propertyType protected property The property type. Either: 'attributes' or `relationships'.
FieldNormalizerValue::$values protected property The values.
FieldNormalizerValue::getAllIncludes public function Computes all the nested includes recursively. Overrides FieldNormalizerValueInterface::getAllIncludes
FieldNormalizerValue::getIncludes public function Gets the includes. Overrides FieldNormalizerValueInterface::getIncludes
FieldNormalizerValue::getPropertyType public function Gets the propertyType. Overrides FieldNormalizerValueInterface::getPropertyType
FieldNormalizerValue::rasterizeIncludes public function Get the includes. Overrides ValueExtractorInterface::rasterizeIncludes
FieldNormalizerValue::rasterizeValue public function Get the rasterized value. Overrides ValueExtractorInterface::rasterizeValue 1
FieldNormalizerValue::__construct public function Instantiate a FieldNormalizerValue object. 2