You are here

class DataReferenceDefinitionNormalizer in Schemata 8

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

Normalizer for Entity References in HAL+JSON style.

Hierarchy

Expanded class hierarchy of DataReferenceDefinitionNormalizer

File

schemata_json_schema/src/Normalizer/hal/DataReferenceDefinitionNormalizer.php, line 13

Namespace

Drupal\schemata_json_schema\Normalizer\hal
View source
class DataReferenceDefinitionNormalizer extends JsonDataReferenceDefinitionNormalizer {

  /**
   * The formats that the Normalizer can handle.
   *
   * @var array
   */
  protected $format = 'schema_json';

  /**
   * The formats that the Normalizer can handle.
   *
   * @var array
   */
  protected $describedFormat = 'hal_json';

  /**
   * The hypermedia link manager.
   *
   * @var \Drupal\hal\LinkManager\LinkManagerInterface
   */
  protected $linkManager;

  /**
   * Constructs an DataReferenceDefinitionNormalizer object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The Entity Type Manager.
   * @param \Drupal\hal\LinkManager\LinkManagerInterface $link_manager
   *   The hypermedia link manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, LinkManagerInterface $link_manager) {
    parent::__construct($entity_type_manager);
    $this->linkManager = $link_manager;
  }

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

    /* @var $entity \Drupal\Core\TypedData\DataReferenceDefinitionInterface */
    if (!$this
      ->validateEntity($entity)) {
      return [];
    }

    // Collect data about the reference field.
    $parentProperty = $this
      ->extractPropertyData($context['parent'], $context);
    $property = $this
      ->extractPropertyData($entity, $context);
    $target_type = $entity
      ->getConstraint('EntityType');
    $target_bundles = isset($context['settings']['handler_settings']['target_bundles']) ? $context['settings']['handler_settings']['target_bundles'] : [];

    // Build the relation URI, which is used as the property key.
    $field_uri = $this->linkManager
      ->getRelationUri($context['entityTypeId'], isset($context['bundleId']) ? $context['bundleId'] : $context['entityTypeId'], $context['name'], $context);

    // From the root of the schema object, build out object references.
    $normalized = [
      '_links' => [
        $field_uri => [
          '$ref' => '#/definitions/linkArray',
        ],
      ],
      '_embedded' => [
        $field_uri => [
          'type' => 'array',
          'items' => [],
        ],
      ],
    ];

    // Add title and description to relation definition.
    if (isset($parentProperty['title'])) {
      $normalized['_links'][$field_uri]['title'] = $parentProperty['title'];
      $normalized['_embedded'][$field_uri]['title'] = $parentProperty['title'];
    }
    if (isset($parentProperty['description'])) {
      $normalized['_links'][$field_uri]['description'] = $parentProperty['description'];
    }

    // Add Schema resource references.
    $item =& $normalized['_embedded'][$field_uri]['items'];
    if (empty($target_bundles)) {
      $generated_url = SchemaUrl::fromOptions($this->format, $this->describedFormat, $target_type)
        ->toString(TRUE);
      $item['$ref'] = $generated_url
        ->getGeneratedUrl();
    }
    elseif (count($target_bundles) == 1) {
      $generated_url = SchemaUrl::fromOptions($this->format, $this->describedFormat, $target_type, reset($target_bundles))
        ->toString(TRUE);
      $item['$ref'] = $generated_url
        ->getGeneratedUrl();
    }
    elseif (count($target_bundles) > 1) {
      $refs = [];
      foreach ($target_bundles as $bundle) {
        $generated_url = SchemaUrl::fromOptions($this->format, $this->describedFormat, $target_type, $bundle)
          ->toString(TRUE);
        $refs[] = [
          '$ref' => $generated_url
            ->getGeneratedUrl(),
        ];
      }
      $item['anyOf'] = $refs;
    }
    return [
      'properties' => $normalized,
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
DataDefinitionNormalizer::extractPropertyData protected function Extracts property details from a data definition.
DataReferenceDefinitionNormalizer::$describedFormat protected property The formats that the Normalizer can handle. Overrides JsonNormalizerBase::$describedFormat
DataReferenceDefinitionNormalizer::$entityTypeManager protected property EntityTypeManager.
DataReferenceDefinitionNormalizer::$format protected property The formats that the Normalizer can handle. Overrides JsonNormalizerBase::$format
DataReferenceDefinitionNormalizer::$linkManager protected property The hypermedia link manager.
DataReferenceDefinitionNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides DataDefinitionNormalizer::$supportedInterfaceOrClass
DataReferenceDefinitionNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides DataReferenceDefinitionNormalizer::normalize
DataReferenceDefinitionNormalizer::validateEntity protected function Ensure the entity type is one we support for schema reference.
DataReferenceDefinitionNormalizer::__construct public function Constructs an DataReferenceDefinitionNormalizer object. Overrides DataReferenceDefinitionNormalizer::__construct
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