You are here

class WorkspaceNormalizer in Replication 8.2

Same name and namespace in other branches
  1. 8 src/Normalizer/WorkspaceNormalizer.php \Drupal\replication\Normalizer\WorkspaceNormalizer

Workspace entity normalizer and denormalizer.

Hierarchy

  • class \Drupal\serialization\Normalizer\NormalizerBase implements \Symfony\Component\Serializer\SerializerAwareInterface, CacheableNormalizerInterface uses \Symfony\Component\Serializer\SerializerAwareTrait
    • class \Drupal\replication\Normalizer\WorkspaceNormalizer implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface

Expanded class hierarchy of WorkspaceNormalizer

1 string reference to 'WorkspaceNormalizer'
replication.services.yml in ./replication.services.yml
replication.services.yml
1 service uses WorkspaceNormalizer
replication.normalizer.workspace in ./replication.services.yml
Drupal\replication\Normalizer\WorkspaceNormalizer

File

src/Normalizer/WorkspaceNormalizer.php, line 14

Namespace

Drupal\replication\Normalizer
View source
class WorkspaceNormalizer extends NormalizerBase implements DenormalizerInterface {

  /**
   * @var string[]
   */
  protected $supportedInterfaceOrClass = [
    'Drupal\\multiversion\\Entity\\WorkspaceInterface',
  ];

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

  /**
   * Constructs an EntityNormalizer object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function normalize($entity, $format = NULL, array $context = []) {
    $context['entity_type'] = 'workspace';
    $return_data = [];
    if ($machine_name = (string) $entity
      ->getMachineName()) {
      $return_data['db_name'] = $machine_name;
    }
    if ($update_seq = $entity
      ->getUpdateSeq()) {
      $return_data['update_seq'] = (int) $update_seq;
    }
    else {

      // Replicator expects update_seq to be always set.
      $return_data['update_seq'] = 0;
    }
    if ($created = (string) $entity
      ->getStartTime()) {
      $return_data['instance_start_time'] = $created;
    }
    return $return_data;
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    if (isset($data['db_name'])) {
      $data['machine_name'] = $data['db_name'];
      unset($data['db_name']);
    }
    if (isset($data['instance_start_time'])) {
      $data['created'] = $data['instance_start_time'];
      unset($data['instance_start_time']);
    }
    $workspace_types = WorkspaceType::loadMultiple();
    $workspace_type = reset($workspace_types);
    if (!$workspace_type instanceof WorkspaceTypeInterface) {
      throw new \Exception('Invalid workspace type.');
    }
    $data['type'] = $workspace_type
      ->id();
    return $this->entityTypeManager
      ->getStorage('workspace')
      ->create($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
WorkspaceNormalizer::$entityTypeManager protected property The entity manager.
WorkspaceNormalizer::$supportedInterfaceOrClass protected property Overrides NormalizerBase::$supportedInterfaceOrClass
WorkspaceNormalizer::denormalize public function Denormalizes data back into an object of the given class.
WorkspaceNormalizer::normalize public function Normalizes an object into a set of arrays/scalars.
WorkspaceNormalizer::__construct public function Constructs an EntityNormalizer object.