You are here

public function ComplexDataDefinitionNormalizer::normalize in Schemata 8

Same name in this branch
  1. 8 schemata_json_schema/src/Normalizer/jsonapi/ComplexDataDefinitionNormalizer.php \Drupal\schemata_json_schema\Normalizer\jsonapi\ComplexDataDefinitionNormalizer::normalize()
  2. 8 schemata_json_schema/src/Normalizer/json/ComplexDataDefinitionNormalizer.php \Drupal\schemata_json_schema\Normalizer\json\ComplexDataDefinitionNormalizer::normalize()
  3. 8 schemata_json_schema/src/Normalizer/hal/ComplexDataDefinitionNormalizer.php \Drupal\schemata_json_schema\Normalizer\hal\ComplexDataDefinitionNormalizer::normalize()

Overrides DataDefinitionNormalizer::normalize

File

schemata_json_schema/src/Normalizer/jsonapi/ComplexDataDefinitionNormalizer.php, line 29

Class

ComplexDataDefinitionNormalizer
Normalizer for ComplexDataDefinitionInterface.

Namespace

Drupal\schemata_json_schema\Normalizer\jsonapi

Code

public function normalize($entity, $format = NULL, array $context = []) {

  /* @var $entity \Drupal\Core\TypedData\ComplexDataDefinitionInterface */
  $context['parent'] = $entity;
  $normalized = $this
    ->extractPropertyData($entity);
  $normalized['type'] = 'object';

  // Retrieve 'properties' and possibly 'required' nested arrays.
  $property_definitions = $entity
    ->getPropertyDefinitions();
  $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;
}