You are here

public function ListDataDefinitionNormalizer::normalize in Schemata 8

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

Overrides DataDefinitionNormalizer::normalize

1 call to ListDataDefinitionNormalizer::normalize()
FieldDefinitionNormalizer::normalize in schemata_json_schema/src/Normalizer/jsonapi/FieldDefinitionNormalizer.php
Normalizes an object into a set of arrays/scalars.
2 methods override ListDataDefinitionNormalizer::normalize()
FieldDefinitionNormalizer::normalize in schemata_json_schema/src/Normalizer/jsonapi/FieldDefinitionNormalizer.php
Normalizes an object into a set of arrays/scalars.
RelationshipFieldDefinitionNormalizer::normalize in schemata_json_schema/src/Normalizer/jsonapi/RelationshipFieldDefinitionNormalizer.php
Normalizes an object into a set of arrays/scalars.

File

schemata_json_schema/src/Normalizer/jsonapi/ListDataDefinitionNormalizer.php, line 28

Class

ListDataDefinitionNormalizer
Normalizer for ListDataDefinitionInterface objects.

Namespace

Drupal\schemata_json_schema\Normalizer\jsonapi

Code

public function normalize($list_data_definition, $format = NULL, array $context = []) {
  assert($list_data_definition instanceof ListDataDefinitionInterface);
  $context['parent'] = $list_data_definition;
  $property = $this
    ->extractPropertyData($list_data_definition, $context);
  $property['type'] = 'array';

  // This retrieves the definition common to ever item in the list, and
  // serializes it so we can define how members of the array should look.
  // There are no lists that might contain items of different types.
  $property['items'] = $this->serializer
    ->normalize($list_data_definition
    ->getItemDefinition(), $format, $context);

  // FieldDefinitionInterface::isRequired() explicitly indicates there must be
  // at least one item in the list. Extending this reasoning, the same must be
  // true of all ListDataDefinitions.
  if ($this
    ->requiredProperty($list_data_definition)) {
    $property['minItems'] = 1;
  }
  if ($context['cardinality'] == 1) {
    $single_property = $property['items'];
    unset($property['items']);
    unset($property['type']);
    unset($property['minItems']);
    $single_property = array_merge($single_property, $property);
    $property = $single_property;
  }
  $normalized = [
    'description' => t('Entity attributes'),
    'type' => 'object',
    'properties' => [],
  ];
  $public_name = $context['name'];
  $normalized['properties'][$public_name] = $property;
  if ($this
    ->requiredProperty($list_data_definition)) {
    $normalized['required'][] = $public_name;
  }
  return [
    'type' => 'object',
    'properties' => [
      'attributes' => $normalized,
    ],
  ];
}