You are here

class ResourceIdentifierNormalizer in JSON:API Extras 8.3

Converts the Drupal entity reference item object to a JSON:API structure.

@internal

Hierarchy

  • class \Drupal\jsonapi_extras\Normalizer\JsonApiNormalizerDecoratorBase implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface, \Symfony\Component\Serializer\Normalizer\DenormalizerInterface, \Symfony\Component\Serializer\SerializerAwareInterface

Expanded class hierarchy of ResourceIdentifierNormalizer

1 file declares its use of ResourceIdentifierNormalizer
ResourceIdentifierNormalizerImpostor.php in src-impostor-normalizers/ResourceIdentifierNormalizerImpostor.php

File

src/Normalizer/ResourceIdentifierNormalizer.php, line 19

Namespace

Drupal\jsonapi_extras\Normalizer
View source
class ResourceIdentifierNormalizer extends JsonApiNormalizerDecoratorBase {

  /**
   * The resource type repository for changes on the target resource type.
   *
   * @var \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface
   */
  protected $resourceTypeRepository;

  /**
   * Instantiates a ResourceIdentifierNormalizer object.
   *
   * @param \Symfony\Component\Serializer\SerializerAwareInterface|\Symfony\Component\Serializer\Normalizer\NormalizerInterface|\Symfony\Component\Serializer\Normalizer\DenormalizerInterface $inner
   *   The decorated normalizer.
   * @param \Drupal\jsonapi\ResourceType\ResourceTypeRepositoryInterface $resource_type_repository
   *   The repository.
   */
  public function __construct($inner, ResourceTypeRepositoryInterface $resource_type_repository) {
    parent::__construct($inner);
    $this->resourceTypeRepository = $resource_type_repository;
  }

  /**
   * {@inheritdoc}
   */
  public function normalize($field, $format = NULL, array $context = []) {
    assert($field instanceof ResourceIdentifier);
    $normalized_output = parent::normalize($field, $format, $context);
    assert($normalized_output instanceof CacheableNormalization);
    if (!isset($context['resource_object'])) {
      return $normalized_output;
    }
    $resource_object = $context['resource_object'];

    // Find the name of the field being normalized. This is unreasonably more
    // contrived than one could expect for ResourceIdentifiers.
    $resource_type = $resource_object
      ->getResourceType();
    $field_name = $this
      ->guessFieldName($field
      ->getId(), $resource_object);
    if (!$field_name) {
      return $normalized_output;
    }
    $enhancer = $resource_type
      ->getFieldEnhancer($field_name);
    if (!$enhancer) {
      return $normalized_output;
    }

    // Apply any enhancements necessary.
    $context['field_resource_identifier'] = $field;
    $transformed = $enhancer
      ->undoTransform($normalized_output
      ->getNormalization(), new Context($context));

    // @todo Enhancers should utilize CacheableNormalization to infer additional cacheability from the enhancer.
    return new CacheableNormalization($normalized_output, array_intersect_key($transformed, array_flip([
      'id',
      'type',
      'meta',
    ])));
  }

  /**
   * Guesses the field name of a resource identifier pointing to a UUID.
   *
   * @param string $uuid
   *   The uuid being referenced.
   * @param \Drupal\jsonapi\JsonApiResource\ResourceObject $resource_object
   *   The object being normalized.
   *
   * @return string|null
   *   The field name.
   */
  protected function guessFieldName($uuid, ResourceObject $resource_object) {
    $resource_type = $resource_object
      ->getResourceType();
    assert($resource_type instanceof ConfigurableResourceType);

    // From the resource object get all the reference fields.
    $reference_field_names = array_keys($resource_type
      ->getRelatableResourceTypes());

    // Only consider the fields that contain enhancers. This is to improve
    // performance. Discard the candidates that will not have an enhancer.
    $ref_enhancers = array_filter(array_map(function ($public_field_name) use ($resource_type) {
      return $resource_type
        ->getFieldEnhancer($public_field_name, 'publicName');
    }, array_combine($reference_field_names, $reference_field_names)));

    // Get the field objects of the reference fields that have enhancers.
    $reference_fields = array_intersect_key($resource_object
      ->getFields(), array_flip(array_keys($ref_enhancers)));
    $reference_fields = array_filter($reference_fields, function ($reference_field) {

      // This is certainly a limitation.
      return $reference_field instanceof EntityReferenceFieldItemListInterface;
    });
    return array_reduce($reference_fields, function ($field_name, EntityReferenceFieldItemListInterface $object_field) use ($uuid) {
      if ($field_name) {
        return $field_name;
      }
      $referenced_entities = $object_field
        ->referencedEntities();

      // If any of the referenced entities contains the UUID of the field
      // being normalized, then we have our field name.
      $matches = array_filter($referenced_entities, function (EntityInterface $referenced_entity) use ($uuid) {
        return $uuid === $referenced_entity
          ->uuid();
      });
      return empty($matches) ? NULL : $object_field
        ->getName();
    });
  }

}

Members

Namesort descending Modifiers Type Description Overrides
JsonApiNormalizerDecoratorBase::$inner protected property The decorated (de)normalizer.
JsonApiNormalizerDecoratorBase::denormalize public function Denormalizes data back into an object of the given class. 2
JsonApiNormalizerDecoratorBase::setSerializer public function Sets the owning Serializer object.
JsonApiNormalizerDecoratorBase::supportsDenormalization public function Checks whether the given class is supported for denormalization by this normalizer.
JsonApiNormalizerDecoratorBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer.
ResourceIdentifierNormalizer::$resourceTypeRepository protected property The resource type repository for changes on the target resource type.
ResourceIdentifierNormalizer::guessFieldName protected function Guesses the field name of a resource identifier pointing to a UUID.
ResourceIdentifierNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides JsonApiNormalizerDecoratorBase::normalize
ResourceIdentifierNormalizer::__construct public function Instantiates a ResourceIdentifierNormalizer object. Overrides JsonApiNormalizerDecoratorBase::__construct