public function FeedsJSONPathParser::parse in Feeds JSONPath Parser 6
Same name and namespace in other branches
- 7 FeedsJSONPathParser.inc \FeedsJSONPathParser::parse()
Implementation of FeedsParser::parse().
Overrides FeedsParser::parse
File
- ./
FeedsJSONPathParser.inc, line 17 - Provides the Class for Feeds JSONPath Parser.
Class
- FeedsJSONPathParser
- Base class for the HTML and XML parsers.
Code
public function parse(FeedsImportBatch $batch, FeedsSource $source) {
$mappings = $source->importer->processor->config['mappings'];
$mappings = $this
->filterMappings($mappings);
$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($batch
->getRaw());
// Set link so we can set the result link attribute.
$fetcher_config = $source
->getConfigFor($source->importer->fetcher);
$batch->link = $fetcher_config['source'];
$array = json_decode(utf8_encode($raw), TRUE);
// Support JSON lines format.
if (!is_array($array)) {
$raw = preg_replace('/}\\s*{/', '},{', $raw);
$raw = '[' . $raw . ']';
$array = json_decode(utf8_encode($raw), TRUE);
}
if (is_array($array)) {
require_once 'jsonpath-0.8.1.php';
$all_items = $this
->jsonPath($array, $source_config['context']);
$this
->debug($all_items, 'context');
unset($array);
foreach ($all_items as $item) {
$parsed_item = $variables = array();
foreach ($source_config['sources'] as $source => $query) {
$parsed = $this
->parseSourceElement($item, $query, $source);
// Avoid null values.
if (isset($parsed)) {
// Variable sunstitution can't handle arrays.
if (!is_array($parsed)) {
$variables['{' . $mappings[$source] . '}'] = $parsed;
}
else {
$variables['{' . $mappings[$source] . '}'] = '';
}
$parsed_item[$source] = $parsed;
}
}
$batch->items[] = $parsed_item;
}
}
else {
throw new Exception(t('There was an error decoding the JSON document.'));
}
}