class FeedsExJsonPath in Feeds extensible parsers 7.2
Same name and namespace in other branches
- 7 src/FeedsExJsonPath.inc \FeedsExJsonPath
Parses JSON via JSONPath.
Hierarchy
- class \FeedsExBase extends \FeedsParser
- class \FeedsExJsonPath
Expanded class hierarchy of FeedsExJsonPath
5 string references to 'FeedsExJsonPath'
- FeedsExJsonPathUnitTests::testBatchParsing in src/
Tests/ FeedsExJsonPath.test - Tests batch parsing.
- FeedsExJsonPathUnitTests::testErrorHandling in src/
Tests/ FeedsExJsonPath.test - Tests parsing error handling.
- FeedsExJsonPathUnitTests::testSimpleParsing in src/
Tests/ FeedsExJsonPath.test - Tests simple parsing.
- FeedsExJsonPathUnitTests::testValidateExpression in src/
Tests/ FeedsExJsonPath.test - Tests JSONPath validation.
- feeds_ex_feeds_plugins in ./
feeds_ex.feeds.inc - Implements hook_feeds_plugins().
File
- src/
FeedsExJsonPath.inc, line 11 - Contains FeedsExJsonPath.
View source
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
FeedsExBase:: |
protected | property | Whether the current system handles mb_* functions. | |
FeedsExBase:: |
protected | property | The object used to display messages to the user. | |
FeedsExBase:: |
public | function | 1 | |
FeedsExBase:: |
public | function | 1 | |
FeedsExBase:: |
protected | function | Returns a form element for a specific column. | 1 |
FeedsExBase:: |
protected | function | Reuturns the list of table headers. | 1 |
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
protected | function | Converts a string to UTF-8. | 1 |
FeedsExBase:: |
protected | function | Renders our debug messages into a list. | |
FeedsExBase:: |
protected | function | Delegates parsing to the subclass. | |
FeedsExBase:: |
protected | function | Detects the encoding of a string. | |
FeedsExBase:: |
protected | function | Executes the source expressions. | |
FeedsExBase:: |
protected | function | Returns the configuration form table header. | |
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
public | function | Returns the messenger. | |
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
protected | function | Returns whether or not this parser uses a context query. | 2 |
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
protected | function | Logs errors. | |
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
protected | function | Prepares the expressions for parsing. | |
FeedsExBase:: |
protected | function | Prepares the variable map used to substitution. | |
FeedsExBase:: |
protected | function | Prints errors to the screen. | |
FeedsExBase:: |
public | function | Sets the messenger to be used to display messages. | |
FeedsExBase:: |
public | function | Sets the multibyte handling. | |
FeedsExBase:: |
protected | function | Allows subclasses to prepare for parsing. | 3 |
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
public | function | ||
FeedsExBase:: |
protected | function | Starts internal error handling. | 1 |
FeedsExBase:: |
protected | function | Stops internal error handling. | 1 |
FeedsExBase:: |
protected | function | 1 | |
FeedsExJsonPath:: |
protected | function |
Allows subclasses to cleanup after parsing. Overrides FeedsExBase:: |
1 |
FeedsExJsonPath:: |
protected | function |
Returns rows to be parsed. Overrides FeedsExBase:: |
1 |
FeedsExJsonPath:: |
protected | function |
Executes a single source expression. Overrides FeedsExBase:: |
1 |
FeedsExJsonPath:: |
protected | function |
Returns the errors after parsing. Overrides FeedsExBase:: |
|
FeedsExJsonPath:: |
protected | function |
Loads the necessary library. Overrides FeedsExBase:: |
|
FeedsExJsonPath:: |
protected | function |
Validates an expression. Overrides FeedsExBase:: |