You are here

public function ChangesNormalizer::normalize in Replication 8

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

File

src/Normalizer/ChangesNormalizer.php, line 21

Class

ChangesNormalizer

Namespace

Drupal\replication\Normalizer

Code

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,
  ];
}