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) |
||
106 | |||
107 | /** |
||
108 | * Internal helper method to parse content from string. |
||
109 | * |
||
110 | * @param string $content |
||
111 | * @return SimpleXMLElement |
||
112 | */ |
||
113 | private function initFromString($content) |
||
121 | |||
122 | /** |
||
123 | * Internal helper method to get SimpleXMLElement object. |
||
124 | * |
||
125 | * @param object $elem |
||
126 | * @return mixed |
||
127 | */ |
||
128 | private function initFromObject($elem) |
||
137 | |||
138 | /** |
||
139 | * Register a new xpath namespace. |
||
140 | * |
||
141 | * @param string $prefix |
||
142 | * @param string $uri |
||
143 | */ |
||
144 | public function registerXPathNamespace($prefix, $uri) |
||
149 | |||
150 | /** |
||
151 | * Register an array of new xpath namespaces. |
||
152 | * |
||
153 | * @param array $namespaces |
||
154 | */ |
||
155 | public function registerXPathNamespaces($namespaces) |
||
162 | |||
163 | /** |
||
164 | * Get the text of the first node matching an XPath query. By default, |
||
165 | * the text will be trimmed, but if you want the untrimmed text, set |
||
166 | * the second paramter to False. |
||
167 | * |
||
168 | * @param string $path |
||
169 | * @param bool $trim |
||
170 | * @return string |
||
171 | */ |
||
172 | public function text($path='.', $trim=true) |
||
178 | |||
179 | /** |
||
180 | * Get a node attribute value. |
||
181 | * |
||
182 | * @param string $attribute |
||
183 | * @return string |
||
184 | */ |
||
185 | public function attr($attribute) |
||
189 | |||
190 | /** |
||
191 | * Get the first node matching an XPath query, or null if no match. |
||
192 | * |
||
193 | * @param string $path |
||
194 | * @return QuiteSimpleXMLElement |
||
195 | */ |
||
196 | public function first($path) |
||
203 | |||
204 | /** |
||
205 | * Check if the document has at least one node matching an XPath query. |
||
206 | * |
||
207 | * @param string $path |
||
208 | * @return bool |
||
209 | */ |
||
210 | public function has($path) |
||
216 | |||
217 | /** |
||
218 | * Get the wrapped SimpleXMLElement object. |
||
219 | * |
||
220 | * @return SimpleXMLElement |
||
221 | */ |
||
222 | public function el() |
||
226 | |||
227 | /** |
||
228 | * Get all nodes matching an XPath query. |
||
229 | * |
||
230 | * @param string $path |
||
231 | * @return QuiteSimpleXMLElement[] |
||
232 | */ |
||
233 | public function xpath($path) |
||
239 | |||
240 | /** |
||
241 | * Alias for `xpath()`. |
||
242 | * |
||
243 | * @param $path |
||
244 | * @return QuiteSimpleXMLElement[] |
||
245 | */ |
||
246 | public function all($path) |
||
250 | |||
251 | /** |
||
252 | * Returns the *untrimmed* text content of the node. |
||
253 | * @return string |
||
254 | */ |
||
255 | public function __toString() |
||
259 | |||
260 | /** |
||
261 | * Returns the child elements. |
||
262 | * |
||
263 | * Note: By default, only children without namespace will be returned. You can |
||
264 | * specify a namespace prefix to get children with that namespace prefix. |
||
265 | * |
||
266 | * Tip: You could also use `xpath('child::node()')`. |
||
267 | * |
||
268 | * @param null $ns |
||
269 | * @return QuiteSimpleXMLElement[] |
||
270 | */ |
||
271 | public function children($ns=null) |
||
284 | |||
285 | /** |
||
286 | * Returns the number of child elements. |
||
287 | * |
||
288 | * Note: By default, only children without namespace will be counted. You can |
||
289 | * specify a namespace prefix to count children with that namespace prefix. |
||
290 | * |
||
291 | * @param null $ns |
||
292 | * @return int |
||
293 | */ |
||
294 | public function count($ns = null) |
||
298 | |||
299 | /** |
||
300 | * Returns and element's attributes. |
||
301 | * |
||
302 | * @return SimpleXMLElement a `SimpleXMLElement` object that can be |
||
303 | * iterated over to loop through the attributes on the tag. |
||
304 | */ |
||
305 | public function attributes() |
||
309 | |||
310 | /** |
||
311 | * Returns the XML as text. |
||
312 | * |
||
313 | * @return string |
||
314 | */ |
||
315 | public function asXML() |
||
319 | |||
320 | /** |
||
321 | * Get the element name. |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | public function getName() |
||
329 | |||
330 | /** |
||
331 | * Return a namespace array. |
||
332 | * |
||
333 | * @return array |
||
334 | */ |
||
335 | public function getNamespaces() |
||
339 | |||
340 | /** |
||
341 | * Set the node value. |
||
342 | * |
||
343 | * @param string $value |
||
344 | */ |
||
345 | public function setValue($value) |
||
349 | |||
350 | /** |
||
351 | * Return the current object as DOMElement. |
||
352 | * |
||
353 | * @return \DOMElement |
||
354 | */ |
||
355 | public function asDOMElement() |
||
359 | |||
360 | /** |
||
361 | * Replaces the current node. Thanks to @hakre |
||
362 | * <http://stackoverflow.com/questions/17661167/how-to-replace-xml-node-with-simplexmlelement-php>. |
||
363 | * |
||
364 | * @param QuiteSimpleXMLElement $element |
||
365 | */ |
||
366 | public function replace(QuiteSimpleXMLElement $element) |
||
375 | |||
376 | /** |
||
377 | * Static helper method to make initialization easier. |
||
378 | * |
||
379 | * @param $input |
||
380 | * @param array $ns |
||
381 | * @return QuiteSimpleXMLElement |
||
382 | */ |
||
383 | public static function make($input, $ns = []) |
||
390 | } |
||
391 |