You are here

class DataDefinitionNormalizer in Schemata 8

Same name in this branch
  1. 8 schemata_json_schema/src/Normalizer/jsonapi/DataDefinitionNormalizer.php \Drupal\schemata_json_schema\Normalizer\jsonapi\DataDefinitionNormalizer
  2. 8 schemata_json_schema/src/Normalizer/json/DataDefinitionNormalizer.php \Drupal\schemata_json_schema\Normalizer\json\DataDefinitionNormalizer
  3. 8 schemata_json_schema/src/Normalizer/hal/DataDefinitionNormalizer.php \Drupal\schemata_json_schema\Normalizer\hal\DataDefinitionNormalizer

Normalizer for DataDefinitionInterface instances.

DataDefinitionInterface is the ultimate parent to all data definitions. This service must always be low priority for data definitions, otherwise the simpler normalization process it supports will take precedence over all the complexities most entity properties contain before reaching this level.

DataDefinitionNormalizer produces scalar value definitions.

Unlike the other Normalizer services in the JSON Schema module, this one is used by the hal_schemata normalizer. It is unlikely divergent requirements will develop.

All the TypedData normalizers extend from this class.

Hierarchy

  • class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
    • class \Drupal\schemata\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface
      • class \Drupal\schemata_json_schema\Normalizer\json\JsonNormalizerBase implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface

Expanded class hierarchy of DataDefinitionNormalizer

2 files declare their use of DataDefinitionNormalizer
DataDefinitionNormalizer.php in schemata_json_schema/src/Normalizer/jsonapi/DataDefinitionNormalizer.php
DataDefinitionNormalizer.php in schemata_json_schema/src/Normalizer/hal/DataDefinitionNormalizer.php
1 string reference to 'DataDefinitionNormalizer'
schemata_json_schema.services.yml in schemata_json_schema/schemata_json_schema.services.yml
schemata_json_schema/schemata_json_schema.services.yml
1 service uses DataDefinitionNormalizer
serializer.normalizer.data_definition.schema_json.json in schemata_json_schema/schemata_json_schema.services.yml
Drupal\schemata_json_schema\Normalizer\json\DataDefinitionNormalizer

File

schemata_json_schema/src/Normalizer/json/DataDefinitionNormalizer.php, line 23

Namespace

Drupal\schemata_json_schema\Normalizer\json
View source
class DataDefinitionNormalizer extends JsonNormalizerBase {

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

  /**
   * {@inheritdoc}
   */
  public function normalize($entity, $format = NULL, array $context = []) {

    /* @var $entity \Drupal\Core\TypedData\DataDefinitionInterface */

    // `text source` and `date source` produce objects not supported in the API.
    // It is not clear how the API excludes them.
    // @todo properly identify and exclude this class of computed objects.
    if ($entity
      ->getSetting('text source') || $entity
      ->getSetting('date source')) {
      return [];
    }
    $property = $this
      ->extractPropertyData($entity, $context);
    if (!empty($context['parent']) && $context['name'] == 'value') {
      if ($maxLength = $context['parent']
        ->getSetting('max_length')) {
        $property['maxLength'] = $maxLength;
      }
      if (empty($context['parent']
        ->getSetting('allowed_values_function')) && !empty($context['parent']
        ->getSetting('allowed_values'))) {
        $allowed_values = $context['parent']
          ->getSetting('allowed_values');
        $property['enum'] = array_keys($allowed_values);
      }
    }
    $normalized = [
      'properties' => [],
    ];
    $normalized['properties'][$context['name']] = $property;
    if ($this
      ->requiredProperty($entity)) {
      $normalized['required'][] = $context['name'];
    }
    return $normalized;
  }

  /**
   * Extracts property details from a data definition.
   *
   * This method includes mapping primitive types in Drupal to JSON Schema
   * type and format descriptions. This method is invoked by several of the
   * normalizers.
   *
   * @param \Drupal\Core\TypedData\DataDefinitionInterface $property
   *   The data definition from which to extract values.
   * @param array $context
   *   Serializer context.
   *
   * @return array
   *   Discrete values of the property definition
   */
  protected function extractPropertyData(DataDefinitionInterface $property, array $context = []) {
    return \Drupal::service('plugin.manager.schemata_json_schema.type_mapper')
      ->createInstance($property
      ->getDataType())
      ->getMappedValue($property);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
DataDefinitionNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides NormalizerBase::$supportedInterfaceOrClass 5
DataDefinitionNormalizer::extractPropertyData protected function Extracts property details from a data definition.
DataDefinitionNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. 5
JsonNormalizerBase::$describedFormat protected property The formats that the Normalizer can handle. 7
JsonNormalizerBase::$format protected property The formats that the Normalizer can handle. Overrides NormalizerBase::$format 7
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. Overrides NormalizerBase::checkFormat
NormalizerBase::denormalize public function Denormalizes data back into an object of the given class.
NormalizerBase::normalizeProperties protected function Normalize an array of data definitions.
NormalizerBase::requiredProperty protected function Determine if the given property is a required element of the schema.
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() Overrides NormalizerBase::supportsDenormalization
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1