FeedsExJsonPath.inc in Feeds extensible parsers 7
Contains FeedsExJsonPath.
File
src/FeedsExJsonPath.inc
View source
<?php
class FeedsExJsonPath extends FeedsExBase {
protected $parser;
protected function executeContext(FeedsSource $source, FeedsFetcherResult $fetcher_result) {
$raw = $this
->prepareRaw($fetcher_result);
$parsed = FeedsExJsonUtility::decodeJsonArray($raw);
$parsed = $this
->getParser()
->search($parsed, $this->config['context']['value']);
if (!is_array($parsed) || empty($parsed)) {
throw new RuntimeException(t('The context expression must return an object or array.'));
}
$state = $source
->state(FEEDS_PARSE);
if (!$state->total) {
$state->total = count($parsed);
}
$start = (int) $state->pointer;
$state->pointer = $start + $source->importer
->getLimit();
return array_slice($parsed, $start, $source->importer
->getLimit());
}
protected function cleanUp(FeedsSource $source, FeedsParserResult $result) {
unset($this->parser);
$state = $source
->state(FEEDS_PARSE);
$state
->progress($state->total, $state->pointer);
}
protected function executeSourceExpression($machine_name, $expression, $row) {
$result = $this
->getParser()
->search($row, $expression);
if (is_scalar($result)) {
return $result;
}
return count($result) === 1 ? reset($result) : $result;
}
protected function validateExpression(&$expression) {
$expression = trim($expression);
if (!class_exists('Flow\\JSONPath\\JSONPathLexer')) {
return;
}
try {
$class = 'Flow\\JSONPath\\JSONPathLexer';
$lexer = new $class($expression);
$lexer
->parseExpression();
} catch (Exception $e) {
return check_plain($e
->getMessage());
}
}
protected function startErrorHandling() {
json_decode('{}');
}
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,
);
}
protected function loadLibrary() {
if (!FeedsExJsonUtility::jsonPathParserInstalled()) {
if (user_access('administer site configuration')) {
$link = l(t('status report'), 'admin/reports/status');
}
else {
$link = t('status report');
}
throw new RuntimeException(t('The JSONPath library is not installed. Check the !status_report_link for more info.', array(
'!status_report_link' => $link,
)));
}
}
protected function getParser() {
if (!isset($this->parser)) {
if (class_exists('Flow\\JSONPath\\JSONPath')) {
$this->parser = new FeedsExJsonPathParserFlow();
}
}
if (!isset($this->parser)) {
throw new RuntimeException(t('Could not load the JSONPath library.'));
}
return $this->parser;
}
}
interface FeedsExJsonPathParserInterface {
public function search(array $data, $expression);
}
class FeedsExJsonPathParserFlow implements FeedsExJsonPathParserInterface {
public function search(array $data, $expression) {
$class = 'Flow\\JSONPath\\JSONPath';
$json_path = new $class($data);
return $json_path
->find($expression)
->data();
}
}