You are here

public static function Reader::importString in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-feed/src/Reader/Reader.php \Zend\Feed\Reader\Reader::importString()

Import a feed from a string

Parameters

string $string:

Return value

Feed\FeedInterface

Throws

Exception\InvalidArgumentException

Exception\RuntimeException

Overrides ReaderImportInterface::importString

4 calls to Reader::importString()
DefaultParser::parse in core/modules/aggregator/src/Plugin/aggregator/parser/DefaultParser.php
Parses feed data.
Reader::import in vendor/zendframework/zend-feed/src/Reader/Reader.php
Import a feed by providing a URI
Reader::importFile in vendor/zendframework/zend-feed/src/Reader/Reader.php
Imports a feed from a file located at $filename.
Reader::importRemoteFeed in vendor/zendframework/zend-feed/src/Reader/Reader.php
Import a feed from a remote URI

File

vendor/zendframework/zend-feed/src/Reader/Reader.php, line 297

Class

Reader

Namespace

Zend\Feed\Reader

Code

public static function importString($string) {
  $trimmed = trim($string);
  if (!is_string($string) || empty($trimmed)) {
    throw new Exception\InvalidArgumentException('Only non empty strings are allowed as input');
  }
  $libxmlErrflag = libxml_use_internal_errors(true);
  $oldValue = libxml_disable_entity_loader(true);
  $dom = new DOMDocument();
  $status = $dom
    ->loadXML(trim($string));
  foreach ($dom->childNodes as $child) {
    if ($child->nodeType === XML_DOCUMENT_TYPE_NODE) {
      throw new Exception\InvalidArgumentException('Invalid XML: Detected use of illegal DOCTYPE');
    }
  }
  libxml_disable_entity_loader($oldValue);
  libxml_use_internal_errors($libxmlErrflag);
  if (!$status) {

    // Build error message
    $error = libxml_get_last_error();
    if ($error && $error->message) {
      $error->message = trim($error->message);
      $errormsg = "DOMDocument cannot parse XML: {$error->message}";
    }
    else {
      $errormsg = "DOMDocument cannot parse XML: Please check the XML document's validity";
    }
    throw new Exception\RuntimeException($errormsg);
  }
  $type = static::detectType($dom);
  static::registerCoreExtensions();
  if (substr($type, 0, 3) == 'rss') {
    $reader = new Feed\Rss($dom, $type);
  }
  elseif (substr($type, 8, 5) == 'entry') {
    $reader = new Entry\Atom($dom->documentElement, 0, self::TYPE_ATOM_10);
  }
  elseif (substr($type, 0, 4) == 'atom') {
    $reader = new Feed\Atom($dom, $type);
  }
  else {
    throw new Exception\RuntimeException('The URI used does not point to a ' . 'valid Atom, RSS or RDF feed that Zend\\Feed\\Reader can parse.');
  }
  return $reader;
}