Skip to content

Commit

Permalink
Port parseHuge option to 3.x
Browse files Browse the repository at this point in the history
Refs #10034
  • Loading branch information
markstory committed Jan 16, 2017
1 parent fa88503 commit 8aec91e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/Utility/Xml.php
Expand Up @@ -92,7 +92,9 @@ class Xml
* - `readFile` Set to false to disable file reading. This is important to disable when
* putting user data into Xml::build(). If enabled local files will be read if they exist.
* Defaults to true for backwards compatibility reasons.
* - If using array as input, you can pass `options` from Xml::fromArray.
* - `parseHuge` Enable the `LIBXML_PARSEHUGE` flag.
*
* If using array as input, you can pass `options` from Xml::fromArray.
*
* @param string|array $input XML string, a path to a file, a URL or an array
* @param string|array $options The options to use
Expand All @@ -104,7 +106,8 @@ public static function build($input, array $options = [])
$defaults = [
'return' => 'simplexml',
'loadEntities' => false,
'readFile' => true
'readFile' => true,
'parseHuge' => true,
];
$options += $defaults;

Expand Down Expand Up @@ -142,9 +145,13 @@ protected static function _loadXml($input, $options)
if ($hasDisable && !$options['loadEntities']) {
libxml_disable_entity_loader(true);
}
$flags = LIBXML_NOCDATA;
if (!empty($options['parseHuge'])) {
$flags |= LIBXML_PARSEHUGE;
}
try {
if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') {
$xml = new SimpleXMLElement($input, LIBXML_NOCDATA);
$xml = new SimpleXMLElement($input, $flags);
} else {
$xml = new DOMDocument();
$xml->loadXML($input);
Expand Down
13 changes: 13 additions & 0 deletions tests/TestCase/Utility/XmlTest.php
Expand Up @@ -113,6 +113,19 @@ public function testBuild()
$this->assertNotRegExp('/encoding/', $obj->saveXML());
}

/**
* test build() method with huge option
*
* @return void
*/
public function testBuildHuge()
{
$xml = '<tag>value</tag>';
$obj = Xml::build($xml, array('parseHuge' => true));
$this->assertEquals('tag', $obj->getName());
$this->assertEquals('value', (string)$obj);
}

/**
* Test that the readFile option disables local file parsing.
*
Expand Down

0 comments on commit 8aec91e

Please sign in to comment.