GenericOpmlParser.php in Feeds 8.3
File
src/Component/GenericOpmlParser.php
View source
<?php
namespace Drupal\feeds\Component;
class GenericOpmlParser {
use XmlParserTrait;
protected $xpath;
protected $normalizeCase;
public function __construct($xml) {
$this->xpath = new \DOMXPath(static::getDomDocument($xml));
}
public function parse($normalize_case = FALSE) {
$this->normalizeCase = $normalize_case;
$return = [
'head' => [
'#title' => '',
],
];
foreach ($this->xpath
->query('/opml/head/*') as $element) {
if ($this->normalizeCase) {
$return['head']['#' . strtolower($element->nodeName)] = $element->nodeValue;
}
else {
$return['head']['#' . $element->nodeName] = $element->nodeValue;
}
}
if (isset($return['head']['#expansionState'])) {
$return['head']['#expansionState'] = array_filter(explode(',', $return['head']['#expansionState']));
}
$return['outlines'] = [];
if ($content = $this->xpath
->evaluate('/opml/body', $this->xpath->document)
->item(0)) {
$return['outlines'] = $this
->getOutlines($content);
}
return $return;
}
protected function getOutlines(\DOMElement $context) {
$outlines = [];
foreach ($this->xpath
->query('outline', $context) as $element) {
$outline = [];
if ($element
->hasAttributes()) {
foreach ($element->attributes as $attribute) {
if ($this->normalizeCase) {
$outline['#' . strtolower($attribute->nodeName)] = $attribute->nodeValue;
}
else {
$outline['#' . $attribute->nodeName] = $attribute->nodeValue;
}
}
}
if ($sub_outlines = $this
->getOutlines($element)) {
$outline['outlines'] = $sub_outlines;
}
$outlines[] = $outline;
}
return $outlines;
}
}