You are here

class ChangesNormalizer in Replication 8

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

Hierarchy

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

Expanded class hierarchy of ChangesNormalizer

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

File

src/Normalizer/ChangesNormalizer.php, line 9

Namespace

Drupal\replication\Normalizer
View source
class ChangesNormalizer extends NormalizerBase implements DenormalizerInterface {
  protected $supportedInterfaceOrClass = [
    'Drupal\\replication\\Changes\\ChangesInterface',
  ];

  /**
   * @var string
   */
  protected $format = [
    'json',
  ];

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

    /** @var \Drupal\replication\Changes\ChangesInterface $changes */
    if (isset($context['query']['filter'])) {
      $changes
        ->filter($context['query']['filter']);
    }
    if (isset($context['query']['parameters'])) {
      $changes
        ->parameters($context['query']['parameters']);
    }
    if (isset($context['query']['limit'])) {
      $changes
        ->setLimit($context['query']['limit']);
    }
    $since = isset($context['query']['since']) && is_numeric($context['query']['since']) ? $context['query']['since'] : 0;

    // Set since if it is not already set. It could be set if there was a POST
    // request.
    if (!$changes
      ->getSince()) {
      $changes
        ->setSince($since);
    }
    $results = $changes
      ->getNormal();
    $last_result = end($results);
    $last_seq = isset($last_result['seq']) ? $last_result['seq'] : 0;
    return [
      'last_seq' => $last_seq,
      'results' => $results,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function denormalize($data, $class, $format = NULL, array $context = []) {
    if (!isset($context['workspace'])) {
      throw new LogicException('A \'workspace\' context is required to denormalize Changes data.');
    }

    // The service is not injected to avoid circular reference.

    /** @var \Drupal\replication\Changes\ChangesInterface $changes */
    $changes = \Drupal::service('replication.changes_factory')
      ->get($context['workspace']);
    if (isset($context['query']['filter'])) {
      $changes
        ->filter($context['query']['filter']);
    }
    if (isset($data['limit'])) {
      $changes
        ->setLimit($data['limit']);
    }
    $since = isset($data['since']) && is_numeric($data['since']) ? $data['since'] : 0;
    $changes
      ->setSince($since);
    $parameters = [];
    if (isset($data['parameters'])) {
      $parameters = $data['parameters'];
    }
    if (!empty($data['doc_ids'])) {
      $parameters['doc_ids'] = $data['doc_ids'];
    }
    return $changes
      ->parameters($parameters);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY constant Name of key for bubbling cacheability metadata via serialization context.
ChangesNormalizer::$format protected property Overrides NormalizerBase::$format
ChangesNormalizer::$supportedInterfaceOrClass protected property The interface or class that this Normalizer supports. Overrides NormalizerBase::$supportedInterfaceOrClass
ChangesNormalizer::denormalize public function Denormalizes data back into an object of the given class.
ChangesNormalizer::normalize public function Normalizes an object into a set of arrays/scalars.
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