You are here

class UriNormalizer in Tome 8

Normalizer for Uri data.

@internal

Hierarchy

Expanded class hierarchy of UriNormalizer

1 string reference to 'UriNormalizer'
tome_sync.services.yml in modules/tome_sync/tome_sync.services.yml
modules/tome_sync/tome_sync.services.yml
1 service uses UriNormalizer
serializer.normalizer.uri_tome_sync in modules/tome_sync/tome_sync.services.yml
Drupal\tome_sync\Normalizer\UriNormalizer

File

modules/tome_sync/src/Normalizer/UriNormalizer.php, line 16

Namespace

Drupal\tome_sync\Normalizer
View source
class UriNormalizer extends PrimitiveDataNormalizer implements DenormalizerInterface {

  /**
   * {@inheritdoc}
   */
  protected $supportedInterfaceOrClass = '\\Drupal\\Core\\TypedData\\Type\\UriInterface';

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The entity repository.
   *
   * @var \Drupal\Core\Entity\EntityRepositoryInterface
   */
  protected $entityRepository;

  /**
   * Constructs a UriNormalizer object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
   *   The entity field manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityRepositoryInterface $entity_repository) {
    $this->entityTypeManager = $entity_type_manager;
    $this->entityRepository = $entity_repository;
  }

  /**
   * {@inheritdoc}
   */
  public function normalize($object, $format = NULL, array $context = []) {
    $value = parent::normalize($object, $format, $context);
    if (!empty($value) && strpos($value, 'entity:') === 0) {
      $parts = explode('/', str_replace('entity:', '', $value));
      if (count($parts) >= 2 && $this->entityTypeManager
        ->hasDefinition($parts[0]) && is_numeric($parts[1])) {
        if ($entity = $this->entityTypeManager
          ->getStorage($parts[0])
          ->load($parts[1])) {
          $parts[1] = $entity
            ->uuid();
          $value = 'entity:' . implode('/', $parts);
        }
      }
    }
    return $value;
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $type, $format = NULL, array $context = []) {
    if (!empty($data) && strpos($data, 'entity:') === 0) {
      $parts = explode('/', str_replace('entity:', '', $data));
      if (count($parts) >= 2 && $this->entityTypeManager
        ->hasDefinition($parts[0]) && Uuid::isValid($parts[1])) {
        if ($referenced_entity = $this->entityRepository
          ->loadEntityByUuid($parts[0], $parts[1])) {
          $parts[1] = $referenced_entity
            ->id();
          $data = 'entity:' . implode('/', $parts);
        }
      }
    }
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
NormalizerBase::$format protected property List of formats which supports (de-)normalization. 3
NormalizerBase::addCacheableDependency protected function Adds cacheability if applicable.
NormalizerBase::checkFormat protected function Checks if the provided format is supported by this normalizer. 2
NormalizerBase::supportsDenormalization public function Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization() 1
NormalizerBase::supportsNormalization public function Checks whether the given class is supported for normalization by this normalizer. 1
SerializedColumnNormalizerTrait::checkForSerializedStrings protected function Checks if there is a serialized string for a column.
SerializedColumnNormalizerTrait::dataHasStringForSerializeColumn protected function Checks if the data contains string value for serialize column.
SerializedColumnNormalizerTrait::getCustomSerializedPropertyNames protected function Gets the names of all properties the plugin treats as serialized data.
SerializedColumnNormalizerTrait::getSerializedPropertyNames protected function Gets the names of all serialized properties.
UriNormalizer::$entityRepository protected property The entity repository.
UriNormalizer::$entityTypeManager protected property The entity type manager.
UriNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides PrimitiveDataNormalizer::$supportedInterfaceOrClass
UriNormalizer::denormalize public function
UriNormalizer::normalize public function Normalizes an object into a set of arrays/scalars. Overrides PrimitiveDataNormalizer::normalize
UriNormalizer::__construct public function Constructs a UriNormalizer object.