You are here

protected function JmesPathParser::executeSourceExpression in Feeds extensible parsers 8

Executes a single source expression.

Parameters

string $machine_name: The source machine name being executed.

string $expression: The expression to execute.

mixed $row: The row to execute on.

Return value

scalar|[]scalar Either a scalar, or a list of scalars. If null, the value will be ignored.

Overrides ParserBase::executeSourceExpression

File

src/Feeds/Parser/JmesPathParser.php, line 129

Class

JmesPathParser
Defines a JSON parser using JMESPath.

Namespace

Drupal\feeds_ex\Feeds\Parser

Code

protected function executeSourceExpression($machine_name, $expression, $row) {
  try {
    $result = $this
      ->search($expression, $row);
  } catch (\Exception $e) {

    // There was an error executing this expression, transform it to a runtime
    // exception, so that it gets properly catched by Feeds.
    throw new \RuntimeException($e
      ->getMessage());
  }
  if (is_object($result)) {
    $result = (array) $result;
  }
  if (!is_array($result)) {
    return $result;
  }
  $count = count($result);
  if ($count === 0) {
    return;
  }

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