Skip to content

Commit 4e1134a

Browse files
committed
Added the ability to pass options when load HTML or XML
1 parent baff799 commit 4e1134a

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

src/DiDom/Document.php

+23-14
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,26 @@ public function appendChild($nodes)
100100
* @param string $string HTML or XML string or file path
101101
* @param bool $isFile indicates that in first parameter was passed to the file path
102102
* @param string $type Type of document
103+
* @param int $options Additional parameters
103104
*/
104-
public function load($string, $isFile = false, $type = 'html')
105+
public function load($string, $isFile = false, $type = 'html', $options = 0)
105106
{
106107
if (!is_string($string)) {
107108
throw new InvalidArgumentException(sprintf('%s expects parameter 1 to be string, %s given', __METHOD__, (is_object($string) ? get_class($string) : gettype($string))));
108109
}
109110

110-
if ($isFile) {
111-
$string = $this->loadFile($string);
112-
}
113-
114111
if (!in_array(strtolower($type), ['xml', 'html'])) {
115112
throw new InvalidArgumentException(sprintf('Document type must be "xml" or "html", %s given', __METHOD__, (is_object($type) ? get_class($type) : gettype($type))));
116113
}
117114

115+
if (!is_integer($options)) {
116+
throw new InvalidArgumentException(sprintf('%s expects parameter 4 to be integer, %s given', __METHOD__, (is_object($options) ? get_class($options) : gettype($options))));
117+
}
118+
119+
if ($isFile) {
120+
$string = $this->loadFile($string);
121+
}
122+
118123
if (substr($string, 0, 5) !== '<?xml') {
119124
$prolog = sprintf('<?xml version="1.0" encoding="%s"?>', $this->document->encoding);
120125

@@ -126,7 +131,7 @@ public function load($string, $isFile = false, $type = 'html')
126131
libxml_use_internal_errors(true);
127132
libxml_disable_entity_loader(true);
128133

129-
$this->type === 'xml' ? $this->document->loadXml($string) : $this->document->loadHtml($string);
134+
$this->type === 'xml' ? $this->document->loadXml($string, $options) : $this->document->loadHtml($string, $options);
130135

131136
libxml_clear_errors();
132137

@@ -140,60 +145,64 @@ public function load($string, $isFile = false, $type = 'html')
140145
* Load HTML from a string.
141146
*
142147
* @param string $html The HTML string
148+
* @param int $options Additional parameters
143149
*
144150
* @return \DiDom\Document
145151
*
146152
* @throws \InvalidArgumentException if the provided argument is not a string
147153
*/
148-
public function loadHtml($html)
154+
public function loadHtml($html, $options = 0)
149155
{
150-
return $this->load($html, false, 'html');
156+
return $this->load($html, false, 'html', $options);
151157
}
152158

153159
/**
154160
* Load HTML from a file.
155161
*
156162
* @param string $filepath The path to the HTML file
163+
* @param int $options Additional parameters
157164
*
158165
* @return \DiDom\Document
159166
*
160167
* @throws \InvalidArgumentException if the file path is not a string
161168
* @throws \RuntimeException if the file does not exist
162169
* @throws \RuntimeException if you are unable to load the file
163170
*/
164-
public function loadHtmlFile($filepath)
171+
public function loadHtmlFile($filepath, $options = 0)
165172
{
166-
return $this->load($filepath, true, 'html');
173+
return $this->load($filepath, true, 'html', $options);
167174
}
168175

169176
/**
170177
* Load XML from a string.
171178
*
172179
* @param string $xml The XML string
180+
* @param int $options Additional parameters
173181
*
174182
* @return \DiDom\Document
175183
*
176184
* @throws \InvalidArgumentException if the provided argument is not a string
177185
*/
178-
public function loadXml($xml)
186+
public function loadXml($xml, $options = 0)
179187
{
180-
return $this->load($xml, false, 'xml');
188+
return $this->load($xml, false, 'xml', $options);
181189
}
182190

183191
/**
184192
* Load XML from a file.
185193
*
186194
* @param string $filepath The path to the XML file
195+
* @param int $options Additional parameters
187196
*
188197
* @return \DiDom\Document
189198
*
190199
* @throws \InvalidArgumentException if the file path is not a string
191200
* @throws \RuntimeException if the file does not exist
192201
* @throws \RuntimeException if you are unable to load the file
193202
*/
194-
public function loadXmlFile($filepath)
203+
public function loadXmlFile($filepath, $options = 0)
195204
{
196-
return $this->load($filepath, true, 'xml');
205+
return $this->load($filepath, true, 'xml', $options);
197206
}
198207

199208
/**

tests/DiDom/DocumentTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ public function testLoadWithInvalidDocumentType()
8484
$document->load('foo', false, 'bar');
8585
}
8686

87+
/**
88+
* @expectedException InvalidArgumentException
89+
*/
90+
public function testLoadWithInvalidOptionsType()
91+
{
92+
$document = new Document();
93+
$document->load('foo', false, 'html', 'bar');
94+
}
95+
8796
/**
8897
* @expectedException InvalidArgumentException
8998
*/

0 commit comments

Comments
 (0)