You are here

class ComplexDataDefinitionNormalizer in JSON:API Schema 8

Normalizer for ComplexDataDefinitionInterface.

ComplexDataDefinitions represent objects - compound values whose objects have string keys. Almost all fields are complex in this way, with their key data stored in an object property of "value". In turn, these objects are wrapped in an array which is normalized by ListDataDefinitionNormalizer.

Hierarchy

Expanded class hierarchy of ComplexDataDefinitionNormalizer

1 string reference to 'ComplexDataDefinitionNormalizer'
jsonapi_schema.services.yml in ./jsonapi_schema.services.yml
jsonapi_schema.services.yml
1 service uses ComplexDataDefinitionNormalizer
serializer.normalizer.complex_data_definition.schema_json in ./jsonapi_schema.services.yml
Drupal\jsonapi_schema\Normalizer\ComplexDataDefinitionNormalizer

File

src/Normalizer/ComplexDataDefinitionNormalizer.php, line 18

Namespace

Drupal\jsonapi_schema\Normalizer
View source
class ComplexDataDefinitionNormalizer extends DataDefinitionNormalizer {

  /**
   * The interface or class that this Normalizer supports.
   *
   * @var string
   */
  protected $supportedInterfaceOrClass = ComplexDataDefinitionInterface::class;

  /**
   * {@inheritdoc}
   */
  public function normalize($entity, $format = NULL, array $context = []) {
    assert($entity instanceof ComplexDataDefinitionInterface);
    $context['parent'] = $entity;
    $normalized = $this
      ->extractPropertyData($entity);
    $normalized['type'] = 'object';

    // Retrieve 'properties' and possibly 'required' nested arrays.
    $property_definitions = array_filter($entity
      ->getPropertyDefinitions(), function (DataDefinitionInterface $item) {
      return !$item
        ->isInternal();
    });
    $properties = $this
      ->normalizeProperties($property_definitions, $format, $context);
    $normalized = NestedArray::mergeDeep($normalized, $properties);
    if (count($property_definitions) == 1) {

      // If there is only one property, JSON API does not use the complex data.
      return $normalized['properties'][key($property_definitions)];
    }
    return $normalized;
  }

  /**
   * Determine if the current field is a reference field.
   *
   * @param \Drupal\Core\TypedData\ComplexDataDefinitionInterface $entity
   *   The complex data definition to be checked.
   * @param array $context
   *   The current serializer context.
   *
   * @return bool
   *   TRUE if it is a reference, FALSE otherwise.
   */
  protected function isReferenceField(ComplexDataDefinitionInterface $entity, array $context = NULL) {
    $main = $entity
      ->getPropertyDefinition($entity
      ->getMainPropertyName());

    // @todo use an interface or API call instead of an object check.
    return $main instanceof DataReferenceTargetDefinition;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
ComplexDataDefinitionNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides DataDefinitionNormalizer::$supportedInterfaceOrClass
ComplexDataDefinitionNormalizer::isReferenceField protected function Determine if the current field is a reference field.
ComplexDataDefinitionNormalizer::normalize public function Overrides DataDefinitionNormalizer::normalize
DataDefinitionNormalizer::$format protected property The formats that the Normalizer can handle. Overrides NormalizerBase::$format
DataDefinitionNormalizer::$supportedDataTypes protected property The supported data type. 6
DataDefinitionNormalizer::denormalize public function
DataDefinitionNormalizer::extractPropertyData protected function Extracts property details from a data definition. 6
DataDefinitionNormalizer::JSON_TYPES constant
DataDefinitionNormalizer::normalizeProperties protected function Normalize an array of data definitions.
DataDefinitionNormalizer::requiredProperty protected function Determine if the given property is a required element of the schema.
DataDefinitionNormalizer::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
DataDefinitionNormalizer::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. Overrides NormalizerBase::supportsNormalization 1
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2