1 | <?php |
||
57 | class QuiteSimpleXMLElement |
||
58 | { |
||
59 | public $namespaces; |
||
60 | public $el; |
||
61 | |||
62 | /** |
||
63 | * QuiteSimpleXMLElement constructor. |
||
64 | * |
||
65 | * @param string|SimpleXMLElement|QuiteSimpleXMLElement $elem |
||
66 | * @param QuiteSimpleXMLElement $inherit_from |
||
67 | * @throws InvalidXMLException |
||
68 | * @throws InvalidArgumentException |
||
69 | */ |
||
70 | public function __construct($elem, QuiteSimpleXMLElement $inherit_from = null) |
||
84 | |||
85 | /** |
||
86 | * Internal helper method to get a SimpleXMLElement from either a string |
||
87 | * or a SimpleXMLElement/QuiteSimpleXMLElement object. |
||
88 | * |
||
89 | * @param string|SimpleXMLElement|QuiteSimpleXMLElement $elem |
||
90 | * @return SimpleXMLElement |
||
91 | * @throws InvalidXMLException |
||
92 | * @throws InvalidArgumentException |
||
93 | */ |
||
94 | protected function getElement($elem) |
||
115 | |||
116 | /** |
||
117 | * Register a new xpath namespace. |
||
118 | * |
||
119 | * @param string $prefix |
||
120 | * @param string $uri |
||
121 | */ |
||
122 | public function registerXPathNamespace($prefix, $uri) |
||
127 | |||
128 | /** |
||
129 | * Register an array of new xpath namespaces. |
||
130 | * |
||
131 | * @param array $namespaces |
||
132 | */ |
||
133 | public function registerXPathNamespaces($namespaces) |
||
140 | |||
141 | /** |
||
142 | * Get the text of the first node matching an XPath query. By default, |
||
143 | * the text will be trimmed, but if you want the untrimmed text, set |
||
144 | * the second paramter to False. |
||
145 | * |
||
146 | * @param string $path |
||
147 | * @param bool $trim |
||
148 | * @return string |
||
149 | */ |
||
150 | public function text($path='.', $trim=true) |
||
156 | |||
157 | /** |
||
158 | * Get a node attribute value. |
||
159 | * |
||
160 | * @param string $attribute |
||
161 | * @return string |
||
162 | */ |
||
163 | public function attr($attribute) |
||
167 | |||
168 | /** |
||
169 | * Get the first node matching an XPath query, or null if no match. |
||
170 | * |
||
171 | * @param string $path |
||
172 | * @return QuiteSimpleXMLElement |
||
173 | */ |
||
174 | public function first($path) |
||
181 | |||
182 | /** |
||
183 | * Check if the document has at least one node matching an XPath query. |
||
184 | * |
||
185 | * @param string $path |
||
186 | * @return bool |
||
187 | */ |
||
188 | public function has($path) |
||
194 | |||
195 | /** |
||
196 | * Get the wrapped SimpleXMLElement object. |
||
197 | * |
||
198 | * @return SimpleXMLElement |
||
199 | */ |
||
200 | public function el() |
||
204 | |||
205 | /** |
||
206 | * Get all nodes matching an XPath query. |
||
207 | * |
||
208 | * @param string $path |
||
209 | * @return QuiteSimpleXMLElement[] |
||
210 | */ |
||
211 | public function xpath($path) |
||
217 | |||
218 | /** |
||
219 | * Alias for `xpath()`. |
||
220 | * |
||
221 | * @param $path |
||
222 | * @return QuiteSimpleXMLElement[] |
||
223 | */ |
||
224 | public function all($path) |
||
228 | |||
229 | /** |
||
230 | * Returns the *untrimmed* text content of the node. |
||
231 | * @return string |
||
232 | */ |
||
233 | public function __toString() |
||
237 | |||
238 | /** |
||
239 | * Returns the child elements. |
||
240 | * |
||
241 | * Note: By default, only children without namespace will be returned. You can |
||
242 | * specify a namespace prefix to get children with that namespace prefix. |
||
243 | * |
||
244 | * Tip: You could also use `xpath('child::node()')`. |
||
245 | * |
||
246 | * @param null $ns |
||
247 | * @return QuiteSimpleXMLElement[] |
||
248 | */ |
||
249 | public function children($ns=null) |
||
262 | |||
263 | /** |
||
264 | * Returns the number of child elements. |
||
265 | * |
||
266 | * Note: By default, only children without namespace will be counted. You can |
||
267 | * specify a namespace prefix to count children with that namespace prefix. |
||
268 | * |
||
269 | * @param null $ns |
||
270 | * @return int |
||
271 | */ |
||
272 | public function count($ns = null) |
||
276 | |||
277 | /** |
||
278 | * Returns and element's attributes. |
||
279 | * |
||
280 | * @return SimpleXMLElement a `SimpleXMLElement` object that can be |
||
281 | * iterated over to loop through the attributes on the tag. |
||
282 | */ |
||
283 | public function attributes() |
||
287 | |||
288 | /** |
||
289 | * Returns the XML as text. |
||
290 | * |
||
291 | * @return string |
||
292 | */ |
||
293 | public function asXML() |
||
297 | |||
298 | /** |
||
299 | * Get the element name. |
||
300 | * |
||
301 | * @return string |
||
302 | */ |
||
303 | public function getName() |
||
307 | |||
308 | /** |
||
309 | * Return a namespace array. |
||
310 | * |
||
311 | * @return array |
||
312 | */ |
||
313 | public function getNamespaces() |
||
317 | |||
318 | /** |
||
319 | * Set the node value. |
||
320 | * |
||
321 | * @param string $value |
||
322 | */ |
||
323 | public function setValue($value) |
||
327 | |||
328 | /** |
||
329 | * Return the current object as DOMElement. |
||
330 | * |
||
331 | * @return \DOMElement |
||
332 | */ |
||
333 | public function asDOMElement() |
||
337 | |||
338 | /** |
||
339 | * Replaces the current node. Thanks to @hakre |
||
340 | * <http://stackoverflow.com/questions/17661167/how-to-replace-xml-node-with-simplexmlelement-php>. |
||
341 | * |
||
342 | * @param QuiteSimpleXMLElement $element |
||
343 | */ |
||
344 | public function replace(QuiteSimpleXMLElement $element) |
||
353 | |||
354 | /** |
||
355 | * Static helper method to make initialization easier. |
||
356 | * |
||
357 | * @param $input |
||
358 | * @param array $ns |
||
359 | * @return QuiteSimpleXMLElement |
||
360 | */ |
||
361 | public static function make($input, $ns = []) |
||
368 | } |
||
369 |