You are here

public function FeedsJSONPathParser::parse in Feeds JSONPath Parser 7

Same name and namespace in other branches
  1. 6 FeedsJSONPathParser.inc \FeedsJSONPathParser::parse()

Implements FeedsParser::parse().

File

./FeedsJSONPathParser.inc, line 52
Contains FeedsJSONPathParser.

Class

FeedsJSONPathParser
Parses JSON using JSONPath.

Code

public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
  $this
    ->loadLibrary();
  $mappings = $this
    ->getOwnMappings();
  $source_config = $source
    ->getConfigFor($this);

  // Allow config inheritance.
  if (empty($source_config)) {
    $source_config = $this->config;
  }
  $this->debug = array_keys(array_filter($source_config['debug']['options']));
  $raw = trim($fetcher_result
    ->getRaw());
  $result = new FeedsParserResult();

  // Set link so we can set the result link attribute.
  $fetcher_config = $source
    ->getConfigFor($source->importer->fetcher);
  $result->link = $fetcher_config['source'];
  $array = json_decode($raw, TRUE);

  // Support JSON lines format.
  if (!is_array($array)) {
    $raw = preg_replace('/}\\s*{/', '},{', $raw);
    $raw = '[' . $raw . ']';
    $array = json_decode($raw, TRUE);
  }
  if (!is_array($array)) {
    throw new Exception(t('There was an error decoding the JSON document.'));
  }
  $all_items = $this
    ->jsonPath($array, $source_config['context']);
  unset($array);

  // Batch.
  $state = $source
    ->state(FEEDS_PARSE);
  if (!$state->total) {
    $state->total = count($all_items);
  }
  $start = (int) $state->pointer;
  $state->pointer = $start + $source->importer
    ->getLimit();
  $all_items = array_slice($all_items, $start, $source->importer
    ->getLimit());

  // Set progress state.
  $state
    ->progress($state->total, $state->pointer);

  // Debug output.
  $this
    ->debug($all_items, 'context');
  foreach ($all_items as $item) {

    // Invoke a hook to check whether the item should be skipped.
    if ($this
      ->invokeHook($item, $source) === TRUE) {
      continue;
    }
    $parsed_item = $variables = array();
    foreach ($source_config['sources'] as $source_key => $query) {

      // Variable substitution.
      $query = strtr($query, $variables);
      $parsed = $this
        ->parseSourceElement($item, $query, $source_key);
      $variables['{' . $mappings[$source_key] . '}'] = is_array($parsed) ? reset($parsed) : $parsed;

      // Avoid null values.
      if (isset($parsed)) {
        $parsed_item[$source_key] = $parsed;
      }
    }
    if (!empty($parsed_item)) {
      $result->items[] = $parsed_item;
    }
  }
  return $result;
}