1 | <?php |
||
49 | class QuiteSimpleXMLElement |
||
50 | { |
||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | public $namespaces; |
||
55 | |||
56 | /** |
||
57 | * @var SimpleXMLElement |
||
58 | */ |
||
59 | public $el; |
||
60 | |||
61 | /** |
||
62 | * QuiteSimpleXMLElement constructor. |
||
63 | * |
||
64 | * @param string|SimpleXMLElement|QuiteSimpleXMLElement $elem |
||
65 | * @param QuiteSimpleXMLElement $inherit_from |
||
66 | * @throws InvalidXMLException |
||
67 | * @throws InvalidArgumentException |
||
68 | */ |
||
69 | public function __construct($elem, QuiteSimpleXMLElement $inherit_from = null) |
||
86 | |||
87 | /** |
||
88 | * Internal helper method to get a SimpleXMLElement from either a string |
||
89 | * or a SimpleXMLElement/QuiteSimpleXMLElement object. |
||
90 | * |
||
91 | * @param string|SimpleXMLElement|QuiteSimpleXMLElement $elem |
||
92 | * @return SimpleXMLElement |
||
93 | * @throws InvalidXMLException |
||
94 | * @throws InvalidArgumentException |
||
95 | */ |
||
96 | private function getSimpleXMLElement($elem) |
||
111 | |||
112 | /** |
||
113 | * Internal helper method to parse content from string. |
||
114 | * |
||
115 | * @param string $content |
||
116 | * @return SimpleXMLElement |
||
117 | */ |
||
118 | private function initFromString($content) |
||
126 | |||
127 | /** |
||
128 | * Register a new xpath namespace. |
||
129 | * |
||
130 | * @param string $prefix |
||
131 | * @param string $uri |
||
132 | */ |
||
133 | public function registerXPathNamespace($prefix, $uri) |
||
138 | |||
139 | /** |
||
140 | * Register an array of new xpath namespaces. |
||
141 | * |
||
142 | * @param array $namespaces |
||
143 | */ |
||
144 | public function registerXPathNamespaces($namespaces) |
||
151 | |||
152 | /** |
||
153 | * Get the text of the first node matching an XPath query. By default, |
||
154 | * the text will be trimmed, but if you want the untrimmed text, set |
||
155 | * the second paramter to False. |
||
156 | * |
||
157 | * @param string $path |
||
158 | * @param bool $trim |
||
159 | * @return string |
||
160 | */ |
||
161 | public function text($path='.', $trim=true) |
||
167 | |||
168 | /** |
||
169 | * Get a node attribute value. |
||
170 | * |
||
171 | * @param string $attribute |
||
172 | * @return string |
||
173 | */ |
||
174 | public function attr($attribute) |
||
178 | |||
179 | /** |
||
180 | * Get the first node matching an XPath query, or null if no match. |
||
181 | * |
||
182 | * @param string $path |
||
183 | * @return QuiteSimpleXMLElement |
||
184 | */ |
||
185 | public function first($path) |
||
192 | |||
193 | /** |
||
194 | * Check if the document has at least one node matching an XPath query. |
||
195 | * |
||
196 | * @param string $path |
||
197 | * @return bool |
||
198 | */ |
||
199 | public function has($path) |
||
205 | |||
206 | /** |
||
207 | * Get the wrapped SimpleXMLElement object. |
||
208 | * |
||
209 | * @return SimpleXMLElement |
||
210 | */ |
||
211 | public function el() |
||
215 | |||
216 | /** |
||
217 | * Get all nodes matching an XPath query. |
||
218 | * |
||
219 | * @param string $path |
||
220 | * @return QuiteSimpleXMLElement[] |
||
221 | */ |
||
222 | public function xpath($path) |
||
228 | |||
229 | /** |
||
230 | * Alias for `xpath()`. |
||
231 | * |
||
232 | * @param $path |
||
233 | * @return QuiteSimpleXMLElement[] |
||
234 | */ |
||
235 | public function all($path) |
||
239 | |||
240 | /** |
||
241 | * Returns the *untrimmed* text content of the node. |
||
242 | * @return string |
||
243 | */ |
||
244 | public function __toString() |
||
248 | |||
249 | /** |
||
250 | * Returns the child elements. |
||
251 | * |
||
252 | * Note: By default, only children without namespace will be returned. You can |
||
253 | * specify a namespace prefix to get children with that namespace prefix. |
||
254 | * |
||
255 | * Tip: You could also use `xpath('child::node()')`. |
||
256 | * |
||
257 | * @param null $ns |
||
258 | * @return QuiteSimpleXMLElement[] |
||
259 | */ |
||
260 | public function children($ns=null) |
||
273 | |||
274 | /** |
||
275 | * Returns the number of child elements. |
||
276 | * |
||
277 | * Note: By default, only children without namespace will be counted. You can |
||
278 | * specify a namespace prefix to count children with that namespace prefix. |
||
279 | * |
||
280 | * @param null $ns |
||
281 | * @return int |
||
282 | */ |
||
283 | public function count($ns = null) |
||
287 | |||
288 | /** |
||
289 | * Returns and element's attributes. |
||
290 | * |
||
291 | * @return SimpleXMLElement a `SimpleXMLElement` object that can be |
||
292 | * iterated over to loop through the attributes on the tag. |
||
293 | */ |
||
294 | public function attributes() |
||
298 | |||
299 | /** |
||
300 | * Returns the XML as text. |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | public function asXML() |
||
308 | |||
309 | /** |
||
310 | * Get the element name. |
||
311 | * |
||
312 | * @return string |
||
313 | */ |
||
314 | public function getName() |
||
318 | |||
319 | /** |
||
320 | * Return a namespace array. |
||
321 | * |
||
322 | * @return array |
||
323 | */ |
||
324 | public function getNamespaces() |
||
328 | |||
329 | /** |
||
330 | * Set the node value. |
||
331 | * |
||
332 | * @param string $value |
||
333 | */ |
||
334 | public function setValue($value) |
||
338 | |||
339 | /** |
||
340 | * Return the current object as DOMElement. |
||
341 | * |
||
342 | * @return \DOMElement |
||
343 | */ |
||
344 | public function asDOMElement() |
||
348 | |||
349 | /** |
||
350 | * Replaces the current node. Thanks to @hakre |
||
351 | * <http://stackoverflow.com/questions/17661167/how-to-replace-xml-node-with-simplexmlelement-php>. |
||
352 | * |
||
353 | * @param QuiteSimpleXMLElement $element |
||
354 | */ |
||
355 | public function replace(QuiteSimpleXMLElement $element) |
||
364 | |||
365 | /** |
||
366 | * Static helper method to make initialization easier. |
||
367 | * |
||
368 | * @param $input |
||
369 | * @param array $ns |
||
370 | * @return QuiteSimpleXMLElement |
||
371 | */ |
||
372 | public static function make($input, $ns = []) |
||
379 | } |
||
380 |