Skip to content

Commit

Permalink
Create method for import html
Browse files Browse the repository at this point in the history
loadHtml parse html string and create either SimpleXmlElement object or a DOMDocument.
use of empty($options['loadEntities']) instead of !$options['loadEntities']
  • Loading branch information
alphp committed Jun 2, 2018
1 parent d9a61c3 commit 42aab01
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/Utility/Xml.php
Expand Up @@ -142,7 +142,7 @@ protected static function _loadXml($input, $options)
{
$hasDisable = function_exists('libxml_disable_entity_loader');
$internalErrors = libxml_use_internal_errors(true);
if ($hasDisable && !$options['loadEntities']) {
if ($hasDisable && empty($options['loadEntities'])) {
libxml_disable_entity_loader(true);
}
$flags = 0;
Expand All @@ -162,7 +162,46 @@ protected static function _loadXml($input, $options)
} catch (Exception $e) {
throw new XmlException('Xml cannot be read. ' . $e->getMessage(), null, $e);
} finally {
if ($hasDisable && !$options['loadEntities']) {
if ($hasDisable && empty($options['loadEntities'])) {
libxml_disable_entity_loader(false);
}
libxml_use_internal_errors($internalErrors);
}
}

/**
* Parse the input html string and create either a SimpleXmlElement object or a DOMDocument.
*
* @param string $input The input html string to load.
* @param array $options The options to use. See Xml::build()
* @return \SimpleXMLElement|\DOMDocument
* @throws \Cake\Utility\Exception\XmlException
*/
public static function loadHtml($input, $options)
{
$hasDisable = function_exists('libxml_disable_entity_loader');
$internalErrors = libxml_use_internal_errors(true);
if ($hasDisable && empty($options['loadEntities'])) {
libxml_disable_entity_loader(true);
}
$flags = 0;
if (!empty($options['parseHuge'])) {
$flags |= LIBXML_PARSEHUGE;
}
try {
$xml = new DOMDocument();
$xml->loadHTML($input, $flags);

if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') {
$flags |= LIBXML_NOCDATA;
$xml = simplexml_import_dom($xml);
}

return $xml;
} catch (Exception $e) {
throw new XmlException('Xml cannot be read. ' . $e->getMessage(), null, $e);
} finally {
if ($hasDisable && empty($options['loadEntities'])) {
libxml_disable_entity_loader(false);
}
libxml_use_internal_errors($internalErrors);
Expand Down

0 comments on commit 42aab01

Please sign in to comment.