You are here

protected function QueryPathXmlParser::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 XmlParser::executeSourceExpression

File

src/Feeds/Parser/QueryPathXmlParser.php, line 61

Class

QueryPathXmlParser
Defines a XML parser using QueryPath.

Namespace

Drupal\feeds_ex\Feeds\Parser

Code

protected function executeSourceExpression($machine_name, $expression, $row) {
  $result = QueryPath::with($row, $expression, $this->queryPathOptions);
  if ($result
    ->size() == 0) {
    return;
  }
  $config = $this->configuration['sources'][$machine_name] + [
    'attribute' => '',
  ];
  $return = [];
  if (strlen($config['attribute'])) {
    foreach ($result as $node) {
      $return[] = $node
        ->attr($config['attribute']);
    }
  }
  elseif (!empty($config['inner'])) {
    foreach ($result as $node) {
      $return[] = $node
        ->innerXML();
    }
  }
  elseif (!empty($config['raw'])) {
    foreach ($result as $node) {
      $return[] = $this
        ->getRawValue($node);
    }
  }
  else {
    foreach ($result as $node) {
      $return[] = $node
        ->text();
    }
  }

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