You are here

class GenericOpmlParser in Feeds 8.3

Parses a generic OPML string into an array.

@todo Move this to Github.

Hierarchy

Expanded class hierarchy of GenericOpmlParser

2 files declare their use of GenericOpmlParser
GenericOpmlParserTest.php in tests/src/Unit/Component/GenericOpmlParserTest.php
OpmlParser.php in src/Feeds/Parser/OpmlParser.php

File

src/Component/GenericOpmlParser.php, line 10

Namespace

Drupal\feeds\Component
View source
class GenericOpmlParser {
  use XmlParserTrait;

  /**
   * The XPath query object.
   *
   * @var \DOMXpath
   */
  protected $xpath;

  /**
   * Whether to normalize the case of elements and attributes.
   *
   * @var bool
   */
  protected $normalizeCase;

  /**
   * Constructs a GenericOpmlParser object.
   *
   * @param string $xml
   *   The XML string.
   */
  public function __construct($xml) {
    $this->xpath = new \DOMXPath(static::getDomDocument($xml));
  }

  /**
   * Parses the OPML file.
   *
   * @param bool $normalize_case
   *   (optional) True to convert all attributes to lowercase. False, to leave
   *   them as is. Defaults to false.
   *
   * @return array
   *   A structured array.
   *
   * @todo Document the return value.
   */
  public function parse($normalize_case = FALSE) {
    $this->normalizeCase = $normalize_case;

    // Title is a required field, let parsers assume its existence.
    $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;
  }

  /**
   * Returns the sub-outline structure.
   *
   * @param \DOMElement $context
   *   The context element to iterate on.
   *
   * @return array
   *   The nested outline array.
   */
  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;
          }
        }
      }

      // Recurse.
      if ($sub_outlines = $this
        ->getOutlines($element)) {
        $outline['outlines'] = $sub_outlines;
      }
      $outlines[] = $outline;
    }
    return $outlines;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GenericOpmlParser::$normalizeCase protected property Whether to normalize the case of elements and attributes.
GenericOpmlParser::$xpath protected property The XPath query object.
GenericOpmlParser::getOutlines protected function Returns the sub-outline structure.
GenericOpmlParser::parse public function Parses the OPML file.
GenericOpmlParser::__construct public function Constructs a GenericOpmlParser object.
XmlParserTrait::$_elementRegex protected static property Matches the characters of an XML element.
XmlParserTrait::$_entityLoader protected static property The previous value of the entity loader.
XmlParserTrait::$_errors protected static property The errors reported during parsing.
XmlParserTrait::$_useError protected static property The previous value of libxml error reporting.
XmlParserTrait::getDomDocument protected static function Returns a new DOMDocument.
XmlParserTrait::getXmlErrors protected static function Returns the errors reported during parsing.
XmlParserTrait::removeDefaultNamespaces protected static function Strips the default namespaces from an XML string.
XmlParserTrait::startXmlErrorHandling protected static function Starts custom error handling.
XmlParserTrait::stopXmlErrorHandling protected static function Stops custom error handling.