Skip to content

Commit 8aec91e

Browse files
committed
Port parseHuge option to 3.x
Refs #10034
1 parent fa88503 commit 8aec91e

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/Utility/Xml.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ class Xml
9292
* - `readFile` Set to false to disable file reading. This is important to disable when
9393
* putting user data into Xml::build(). If enabled local files will be read if they exist.
9494
* Defaults to true for backwards compatibility reasons.
95-
* - If using array as input, you can pass `options` from Xml::fromArray.
95+
* - `parseHuge` Enable the `LIBXML_PARSEHUGE` flag.
96+
*
97+
* If using array as input, you can pass `options` from Xml::fromArray.
9698
*
9799
* @param string|array $input XML string, a path to a file, a URL or an array
98100
* @param string|array $options The options to use
@@ -104,7 +106,8 @@ public static function build($input, array $options = [])
104106
$defaults = [
105107
'return' => 'simplexml',
106108
'loadEntities' => false,
107-
'readFile' => true
109+
'readFile' => true,
110+
'parseHuge' => true,
108111
];
109112
$options += $defaults;
110113

@@ -142,9 +145,13 @@ protected static function _loadXml($input, $options)
142145
if ($hasDisable && !$options['loadEntities']) {
143146
libxml_disable_entity_loader(true);
144147
}
148+
$flags = LIBXML_NOCDATA;
149+
if (!empty($options['parseHuge'])) {
150+
$flags |= LIBXML_PARSEHUGE;
151+
}
145152
try {
146153
if ($options['return'] === 'simplexml' || $options['return'] === 'simplexmlelement') {
147-
$xml = new SimpleXMLElement($input, LIBXML_NOCDATA);
154+
$xml = new SimpleXMLElement($input, $flags);
148155
} else {
149156
$xml = new DOMDocument();
150157
$xml->loadXML($input);

tests/TestCase/Utility/XmlTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,19 @@ public function testBuild()
113113
$this->assertNotRegExp('/encoding/', $obj->saveXML());
114114
}
115115

116+
/**
117+
* test build() method with huge option
118+
*
119+
* @return void
120+
*/
121+
public function testBuildHuge()
122+
{
123+
$xml = '<tag>value</tag>';
124+
$obj = Xml::build($xml, array('parseHuge' => true));
125+
$this->assertEquals('tag', $obj->getName());
126+
$this->assertEquals('value', (string)$obj);
127+
}
128+
116129
/**
117130
* Test that the readFile option disables local file parsing.
118131
*

0 commit comments

Comments
 (0)