You are here

class XmlUtility in Feeds extensible parsers 8

Simple XML helpers.

Hierarchy

Expanded class hierarchy of XmlUtility

6 files declare their use of XmlUtility
HtmlParserTest.php in tests/src/Unit/Feeds/Parser/HtmlParserTest.php
QueryPathHtmlParserTest.php in tests/src/Unit/Feeds/Parser/QueryPathHtmlParserTest.php
QueryPathXmlParserTest.php in tests/src/Unit/Feeds/Parser/QueryPathXmlParserTest.php
XmlParser.php in src/Feeds/Parser/XmlParser.php
XmlParserTest.php in tests/src/Unit/Feeds/Parser/XmlParserTest.php

... See full list

1 string reference to 'XmlUtility'
feeds_ex.services.yml in ./feeds_ex.services.yml
feeds_ex.services.yml
1 service uses XmlUtility
feeds_ex.xml_utility in ./feeds_ex.services.yml
Drupal\feeds_ex\Utility\XmlUtility

File

src/Utility/XmlUtility.php, line 12

Namespace

Drupal\feeds_ex\Utility
View source
class XmlUtility {
  use StringTranslationTrait;

  /**
   * Creates an HTML document.
   *
   * @param string $source
   *   The string containing the HTML.
   * @param int $options
   *   (optional) Bitwise OR of the libxml option constants. Defaults to 0.
   *
   * @return \DOMDocument
   *   The newly created DOMDocument.
   *
   * @throws \RuntimeException
   *   Thrown if there is a fatal error parsing the XML.
   */
  public function createHtmlDocument($source, $options = 0) {

    // Fun hack to force parsing as utf-8.
    $source = '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />' . "\n" . $source;
    $document = $this
      ->buildDomDocument();
    $options |= LIBXML_NONET;
    $options |= defined('LIBXML_COMPACT') ? LIBXML_COMPACT : 0;
    $options |= defined('LIBXML_PARSEHUGE') ? LIBXML_PARSEHUGE : 0;
    $success = $document
      ->loadHTML($source, $options);
    if (!$success) {
      throw new RuntimeException($this
        ->t('There was an error parsing the HTML document.'));
    }
    return $document;
  }

  /**
   * Converts named HTML entities to their UTF-8 equivalent.
   *
   * @param string $markup
   *   The string.
   *
   * @return string
   *   The converted string.
   */
  public function decodeNamedHtmlEntities($markup) {
    $map = array_flip(get_html_translation_table(HTML_ENTITIES, ENT_NOQUOTES | ENT_HTML5, 'UTF-8'));
    unset($map['&amp;'], $map['&lt;'], $map['&gt;']);
    return strtr($markup, $map);
  }

  /**
   * Builds a DOMDocument setting some default values.
   *
   * @return \DOMDocument
   *   A new DOMDocument.
   */
  protected function buildDomDocument() {
    $document = new DOMDocument();
    $document->strictErrorChecking = FALSE;
    $document->resolveExternals = FALSE;

    // Libxml specific.
    $document->substituteEntities = FALSE;
    $document->recover = TRUE;
    return $document;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
XmlUtility::buildDomDocument protected function Builds a DOMDocument setting some default values.
XmlUtility::createHtmlDocument public function Creates an HTML document.
XmlUtility::decodeNamedHtmlEntities public function Converts named HTML entities to their UTF-8 equivalent.