You are here

FeedsExJsonPath.inc in Feeds extensible parsers 7.2

Same filename and directory in other branches
  1. 7 src/FeedsExJsonPath.inc

Contains FeedsExJsonPath.

File

src/FeedsExJsonPath.inc
View source
<?php

/**
 * @file
 * Contains FeedsExJsonPath.
 */

/**
 * Parses JSON via JSONPath.
 */
class FeedsExJsonPath extends FeedsExBase {

  /**
   * {@inheritdoc}
   */
  protected function executeContext(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
    $raw = trim($fetcher_result
      ->getRaw());
    if (!strlen($raw)) {
      throw new FeedsExEmptyException();
    }
    if ($encoding = $this
      ->detectEncoding($raw)) {
      $raw = $this
        ->convertEncoding($raw, $encoding);
    }
    $parsed = drupal_json_decode($raw);
    $parsed = jsonPath($parsed, $this->config['context']['value']);
    $state = $source
      ->state(FEEDS_PARSE);
    if (!$state->total) {
      $state->total = count($parsed);
    }
    $start = $state->pointer ? $state->pointer : 0;
    $state->pointer = $start + $source->importer
      ->getLimit();
    return array_slice($parsed, $start, $source->importer
      ->getLimit());
  }

  /**
   * {@inheritdoc}
   */
  protected function cleanUp(FeedsSource $source, FeedsParserResult $result) {

    // Calculate progress.
    $state = $source
      ->state(FEEDS_PARSE);
    $state
      ->progress($state->total, $state->pointer);
  }

  /**
   * {@inheritdoc}
   */
  protected function executeSourceExpression($machine_name, $expression, $row) {
    $result = jsonPath($row, $expression);
    if (is_scalar($result)) {
      return $result;
    }

    // Return a single value if there's only one value.
    return count($result) === 1 ? reset($result) : $result;
  }

  /**
   * {@inheritdoc}
   */
  protected function validateExpression(&$expression) {
    $expression = trim($expression);
  }

  /**
   * {@inheritdoc}
   */
  protected function getErrors() {
    if (!function_exists('json_last_error')) {
      return array();
    }
    if (!($error = json_last_error())) {
      return array();
    }
    $message = array(
      'message' => FeedsExJsonUtility::translateError($error),
      'variables' => array(),
      'severity' => WATCHDOG_ERROR,
    );
    return array(
      $message,
    );
  }

  /**
   * {@inheritdoc}
   */
  protected function loadLibrary() {
    if (!($path = feeds_ex_jsonpath_library_path())) {
      throw new RuntimeException(t('The JSONPath library is not installed.'));
    }
    require_once DRUPAL_ROOT . '/' . $path;
  }

}

Classes

Namesort descending Description
FeedsExJsonPath Parses JSON via JSONPath.