You are here

protected function JmesPathParser::executeContext in Feeds extensible parsers 8

Returns rows to be parsed.

Parameters

\Drupal\feeds\FeedInterface $feed: Source information.

\Drupal\feeds\Result\FetcherResultInterface $fetcher_result: The result returned by the fetcher.

\Drupal\feeds\StateInterface $state: The state object.

Return value

array|Traversable Some iterable that returns rows.

Overrides ParserBase::executeContext

File

src/Feeds/Parser/JmesPathParser.php, line 92

Class

JmesPathParser
Defines a JSON parser using JMESPath.

Namespace

Drupal\feeds_ex\Feeds\Parser

Code

protected function executeContext(FeedInterface $feed, FetcherResultInterface $fetcher_result, StateInterface $state) {
  $parsed = $this->utility
    ->decodeJsonObject($this
    ->prepareRaw($fetcher_result));
  $parsed = $this
    ->search($this->configuration['context']['value'], $parsed);
  if (!is_array($parsed) && !is_object($parsed)) {
    throw new \RuntimeException($this
      ->t('The context expression must return an object or array.'));
  }

  // If an object is returned, consider it one item.
  if (is_object($parsed)) {
    return [
      $parsed,
    ];
  }
  if (!$state->total) {
    $state->total = count($parsed);
  }
  $start = (int) $state->pointer;
  $state->pointer = $start + $this->configuration['line_limit'];
  return array_slice($parsed, $start, $this->configuration['line_limit']);
}