@@ -94,7 +94,7 @@ discard block |
||
94 | 94 | } |
95 | 95 | |
96 | 96 | /** |
97 | - * @param $title |
|
97 | + * @param string $title |
|
98 | 98 | * @param $data |
99 | 99 | * @param int $indent |
100 | 100 | * @param bool $backtick |
@@ -254,7 +254,7 @@ discard block |
||
254 | 254 | } |
255 | 255 | |
256 | 256 | /** |
257 | - * @param $index |
|
257 | + * @param integer $index |
|
258 | 258 | * @return string |
259 | 259 | */ |
260 | 260 | private static function getHeaderLine($index): string { |
@@ -490,8 +490,8 @@ discard block |
||
490 | 490 | |
491 | 491 | /** |
492 | 492 | * @param $item |
493 | - * @param $depth |
|
494 | - * @param $include_string_content |
|
493 | + * @param integer $depth |
|
494 | + * @param boolean $include_string_content |
|
495 | 495 | * @return string |
496 | 496 | */ |
497 | 497 | private static function recursivelyProcessNode(\SimpleXMLElement $item, |
@@ -17,25 +17,25 @@ discard block |
||
17 | 17 | class SxmlDebug { |
18 | 18 | |
19 | 19 | |
20 | - /** |
|
21 | - * Character to use for indenting strings |
|
22 | - */ |
|
23 | - const INDENT = "\t"; |
|
24 | - /** |
|
25 | - * How much of a string to extract |
|
26 | - */ |
|
27 | - const EXTRACT_SIZE = 15; |
|
28 | - |
|
29 | - /** |
|
30 | - * Output a summary of the node or list of nodes referenced by a particular |
|
31 | - * SimpleXML object Rather than attempting a recursive inspection, presents |
|
32 | - * statistics aimed at understanding what your SimpleXML code is doing. |
|
33 | - * |
|
34 | - * @param \SimpleXMLElement $sxml The object to inspect |
|
35 | - * @return string output string |
|
36 | - * |
|
37 | - */ |
|
38 | - public static function dump(\SimpleXMLElement $sxml) { |
|
20 | + /** |
|
21 | + * Character to use for indenting strings |
|
22 | + */ |
|
23 | + const INDENT = "\t"; |
|
24 | + /** |
|
25 | + * How much of a string to extract |
|
26 | + */ |
|
27 | + const EXTRACT_SIZE = 15; |
|
28 | + |
|
29 | + /** |
|
30 | + * Output a summary of the node or list of nodes referenced by a particular |
|
31 | + * SimpleXML object Rather than attempting a recursive inspection, presents |
|
32 | + * statistics aimed at understanding what your SimpleXML code is doing. |
|
33 | + * |
|
34 | + * @param \SimpleXMLElement $sxml The object to inspect |
|
35 | + * @return string output string |
|
36 | + * |
|
37 | + */ |
|
38 | + public static function dump(\SimpleXMLElement $sxml) { |
|
39 | 39 | |
40 | 40 | $dump = ''; |
41 | 41 | // Note that the header is added at the end, so we can add stats |
@@ -48,72 +48,72 @@ discard block |
||
48 | 48 | $item_index = 0; |
49 | 49 | while (isset($sxml[$item_index])) { |
50 | 50 | |
51 | - /** @var \SimpleXMLElement $item */ |
|
52 | - $item = $sxml[$item_index]; |
|
53 | - $item_index++; |
|
51 | + /** @var \SimpleXMLElement $item */ |
|
52 | + $item = $sxml[$item_index]; |
|
53 | + $item_index++; |
|
54 | 54 | |
55 | - // It's surprisingly hard to find something which behaves consistently differently for an attribute and an element within SimpleXML |
|
56 | - // The below relies on the fact that the DOM makes a much clearer distinction |
|
57 | - // Note that this is not an expensive conversion, as we are only swapping PHP wrappers around an existing LibXML resource |
|
58 | - if (dom_import_simplexml($item) instanceOf \DOMAttr) { |
|
55 | + // It's surprisingly hard to find something which behaves consistently differently for an attribute and an element within SimpleXML |
|
56 | + // The below relies on the fact that the DOM makes a much clearer distinction |
|
57 | + // Note that this is not an expensive conversion, as we are only swapping PHP wrappers around an existing LibXML resource |
|
58 | + if (dom_import_simplexml($item) instanceOf \DOMAttr) { |
|
59 | 59 | $dump .= self::dumpAddAttribute($item); |
60 | - } else { |
|
60 | + } else { |
|
61 | 61 | $dump .= self::dumpAddElement($item); |
62 | - } |
|
62 | + } |
|
63 | 63 | } |
64 | 64 | $dump .= ']' . PHP_EOL; |
65 | 65 | |
66 | 66 | // Add on the header line, with the total number of items output |
67 | 67 | return self::getHeaderLine($item_index) . $dump; |
68 | - } |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * @param \SimpleXMLElement $item |
|
72 | - * @return string |
|
73 | - */ |
|
74 | - private static function dumpAddNamespace(\SimpleXMLElement $item): string { |
|
70 | + /** |
|
71 | + * @param \SimpleXMLElement $item |
|
72 | + * @return string |
|
73 | + */ |
|
74 | + private static function dumpAddNamespace(\SimpleXMLElement $item): string { |
|
75 | 75 | |
76 | 76 | $dump = ''; |
77 | 77 | // To what namespace does this attribute belong? Returns array( alias => URI ) |
78 | 78 | $ns = $item->getNamespaces(FALSE); |
79 | 79 | if ($ns) { |
80 | - $dump .= self::INDENT . self::INDENT . 'Namespace: \'' . reset($ns) . |
|
81 | - '\'' . |
|
82 | - PHP_EOL; |
|
83 | - if (key($ns) == '') { |
|
80 | + $dump .= self::INDENT . self::INDENT . 'Namespace: \'' . reset($ns) . |
|
81 | + '\'' . |
|
82 | + PHP_EOL; |
|
83 | + if (key($ns) == '') { |
|
84 | 84 | $dump .= self::INDENT . self::INDENT . '(Default Namespace)' . PHP_EOL; |
85 | - } else { |
|
85 | + } else { |
|
86 | 86 | $dump .= self::INDENT . self::INDENT . 'Namespace Alias: \'' . |
87 | - key($ns) . |
|
88 | - '\'' . |
|
89 | - PHP_EOL; |
|
90 | - } |
|
87 | + key($ns) . |
|
88 | + '\'' . |
|
89 | + PHP_EOL; |
|
90 | + } |
|
91 | 91 | } |
92 | 92 | |
93 | 93 | return $dump; |
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * @param $title |
|
98 | - * @param $data |
|
99 | - * @param int $indent |
|
100 | - * @param bool $backtick |
|
101 | - * @return string |
|
102 | - */ |
|
103 | - private static function dumpGetLine($title, |
|
104 | - $data, |
|
105 | - $indent = 1, |
|
106 | - $backtick = TRUE): string { |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * @param $title |
|
98 | + * @param $data |
|
99 | + * @param int $indent |
|
100 | + * @param bool $backtick |
|
101 | + * @return string |
|
102 | + */ |
|
103 | + private static function dumpGetLine($title, |
|
104 | + $data, |
|
105 | + $indent = 1, |
|
106 | + $backtick = TRUE): string { |
|
107 | 107 | return str_repeat(self::INDENT, $indent) . $title . ': ' . |
108 | - ($backtick ? '\'' : '') . $data . |
|
109 | - ($backtick ? '\'' : '') . PHP_EOL; |
|
110 | - } |
|
108 | + ($backtick ? '\'' : '') . $data . |
|
109 | + ($backtick ? '\'' : '') . PHP_EOL; |
|
110 | + } |
|
111 | 111 | |
112 | - /** |
|
113 | - * @param \SimpleXMLElement $item |
|
114 | - * @return string |
|
115 | - */ |
|
116 | - private static function dumpAddAttribute(\SimpleXMLElement $item): string { |
|
112 | + /** |
|
113 | + * @param \SimpleXMLElement $item |
|
114 | + * @return string |
|
115 | + */ |
|
116 | + private static function dumpAddAttribute(\SimpleXMLElement $item): string { |
|
117 | 117 | |
118 | 118 | $dump = self::INDENT . 'Attribute {' . PHP_EOL; |
119 | 119 | |
@@ -125,13 +125,13 @@ discard block |
||
125 | 125 | $dump .= self::INDENT . '}' . PHP_EOL; |
126 | 126 | return $dump; |
127 | 127 | |
128 | - } |
|
128 | + } |
|
129 | 129 | |
130 | - /** |
|
131 | - * @param \SimpleXMLElement $item |
|
132 | - * @return string |
|
133 | - */ |
|
134 | - private static function dumpAddElement(\SimpleXMLElement $item): string { |
|
130 | + /** |
|
131 | + * @param \SimpleXMLElement $item |
|
132 | + * @return string |
|
133 | + */ |
|
134 | + private static function dumpAddElement(\SimpleXMLElement $item): string { |
|
135 | 135 | |
136 | 136 | $dump = self::INDENT . 'Element {' . PHP_EOL; |
137 | 137 | |
@@ -147,16 +147,16 @@ discard block |
||
147 | 147 | $all_ns = $item->getNamespaces(TRUE); |
148 | 148 | // If the default namespace is never declared, it will never show up using the below code |
149 | 149 | if (!array_key_exists('', $all_ns)) { |
150 | - $all_ns[''] = NULL; |
|
150 | + $all_ns[''] = NULL; |
|
151 | 151 | } |
152 | 152 | |
153 | 153 | foreach ($all_ns as $ns_alias => $ns_uri) { |
154 | - $children = $item->children($ns_uri); |
|
155 | - $attributes = $item->attributes($ns_uri); |
|
154 | + $children = $item->children($ns_uri); |
|
155 | + $attributes = $item->attributes($ns_uri); |
|
156 | 156 | |
157 | - // Somewhat confusingly, in the case where a parent element is missing the xmlns declaration, |
|
158 | - // but a descendant adds it, SimpleXML will look ahead and fill $all_ns[''] incorrectly |
|
159 | - if ( |
|
157 | + // Somewhat confusingly, in the case where a parent element is missing the xmlns declaration, |
|
158 | + // but a descendant adds it, SimpleXML will look ahead and fill $all_ns[''] incorrectly |
|
159 | + if ( |
|
160 | 160 | empty($ns_alias) |
161 | 161 | && |
162 | 162 | NULL !== $ns_uri |
@@ -164,123 +164,123 @@ discard block |
||
164 | 164 | count($children) === 0 |
165 | 165 | && |
166 | 166 | count($attributes) === 0 |
167 | - ) { |
|
167 | + ) { |
|
168 | 168 | // Try looking for a default namespace without a known URI |
169 | 169 | $ns_uri = NULL; |
170 | 170 | $children = $item->children($ns_uri); |
171 | 171 | $attributes = $item->attributes($ns_uri); |
172 | - } |
|
172 | + } |
|
173 | 173 | |
174 | - // Don't show zero-counts, as they're not that useful |
|
175 | - if (count($children) === 0 && count($attributes) === 0) { |
|
174 | + // Don't show zero-counts, as they're not that useful |
|
175 | + if (count($children) === 0 && count($attributes) === 0) { |
|
176 | 176 | continue; |
177 | - } |
|
177 | + } |
|
178 | 178 | |
179 | - $ns_label = (($ns_alias === '') ? 'Default Namespace' : |
|
179 | + $ns_label = (($ns_alias === '') ? 'Default Namespace' : |
|
180 | 180 | "Namespace $ns_alias"); |
181 | 181 | |
182 | - $dump .= self::INDENT . self::INDENT . 'Content in ' . $ns_label . |
|
183 | - PHP_EOL; |
|
182 | + $dump .= self::INDENT . self::INDENT . 'Content in ' . $ns_label . |
|
183 | + PHP_EOL; |
|
184 | 184 | |
185 | - if (NULL !== $ns_uri) { |
|
185 | + if (NULL !== $ns_uri) { |
|
186 | 186 | $dump .= self::dumpGetLine('Namespace URI', $ns_uri, 3); |
187 | - } |
|
187 | + } |
|
188 | 188 | |
189 | 189 | |
190 | - $dump .= self::dumpGetLine('Children', |
|
191 | - self::dumpGetChildDetails($children), |
|
192 | - 3, |
|
193 | - FALSE); |
|
190 | + $dump .= self::dumpGetLine('Children', |
|
191 | + self::dumpGetChildDetails($children), |
|
192 | + 3, |
|
193 | + FALSE); |
|
194 | 194 | |
195 | 195 | |
196 | - $dump .= self::dumpGetLine('Attributes', |
|
197 | - self::dumpGetAttributeDetails($attributes), |
|
198 | - 3, |
|
199 | - FALSE); |
|
196 | + $dump .= self::dumpGetLine('Attributes', |
|
197 | + self::dumpGetAttributeDetails($attributes), |
|
198 | + 3, |
|
199 | + FALSE); |
|
200 | 200 | } |
201 | 201 | |
202 | 202 | return $dump . self::INDENT . '}' . PHP_EOL; |
203 | - } |
|
203 | + } |
|
204 | 204 | |
205 | - /** |
|
206 | - * @param \SimpleXMLElement $children |
|
207 | - * @return string |
|
208 | - */ |
|
209 | - private static function dumpGetChildDetails(\SimpleXMLElement $children): string { |
|
205 | + /** |
|
206 | + * @param \SimpleXMLElement $children |
|
207 | + * @return string |
|
208 | + */ |
|
209 | + private static function dumpGetChildDetails(\SimpleXMLElement $children): string { |
|
210 | 210 | // Count occurrence of child element names, rather than listing them all out |
211 | 211 | $child_names = []; |
212 | 212 | foreach ($children as $sx_child) { |
213 | - // Below is a rather clunky way of saying $child_names[ $sx_child->getName() ]++; |
|
214 | - // which avoids Notices about unset array keys |
|
215 | - $child_node_name = $sx_child->getName(); |
|
216 | - if (array_key_exists($child_node_name, $child_names)) { |
|
213 | + // Below is a rather clunky way of saying $child_names[ $sx_child->getName() ]++; |
|
214 | + // which avoids Notices about unset array keys |
|
215 | + $child_node_name = $sx_child->getName(); |
|
216 | + if (array_key_exists($child_node_name, $child_names)) { |
|
217 | 217 | $child_names[$child_node_name]++; |
218 | - } else { |
|
218 | + } else { |
|
219 | 219 | $child_names[$child_node_name] = 1; |
220 | - } |
|
220 | + } |
|
221 | 221 | } |
222 | 222 | ksort($child_names); |
223 | 223 | $child_name_output = []; |
224 | 224 | foreach ($child_names as $name => $count) { |
225 | - $child_name_output[] = "$count '$name'"; |
|
225 | + $child_name_output[] = "$count '$name'"; |
|
226 | 226 | } |
227 | 227 | |
228 | 228 | $childrenString = count($children); |
229 | 229 | // Don't output a trailing " - " if there are no children |
230 | 230 | if (count($children) > 0) { |
231 | - $childrenString .= ' - ' . implode(', ', $child_name_output); |
|
231 | + $childrenString .= ' - ' . implode(', ', $child_name_output); |
|
232 | 232 | } |
233 | 233 | return $childrenString; |
234 | - } |
|
234 | + } |
|
235 | 235 | |
236 | - /** |
|
237 | - * @param \SimpleXMLElement $attributes |
|
238 | - * @return string |
|
239 | - */ |
|
240 | - private static function dumpGetAttributeDetails(\SimpleXMLElement $attributes): string { |
|
236 | + /** |
|
237 | + * @param \SimpleXMLElement $attributes |
|
238 | + * @return string |
|
239 | + */ |
|
240 | + private static function dumpGetAttributeDetails(\SimpleXMLElement $attributes): string { |
|
241 | 241 | // Attributes can't be duplicated, but I'm going to put them in alphabetical order |
242 | 242 | $attribute_names = []; |
243 | 243 | foreach ($attributes as $sx_attribute) { |
244 | - $attribute_names[] = "'" . $sx_attribute->getName() . "'"; |
|
244 | + $attribute_names[] = "'" . $sx_attribute->getName() . "'"; |
|
245 | 245 | } |
246 | 246 | ksort($attribute_names); |
247 | 247 | |
248 | 248 | $attString = count($attributes); |
249 | 249 | // Don't output a trailing " - " if there are no attributes |
250 | 250 | if (count($attributes) > 0) { |
251 | - $attString .= ' - ' . implode(', ', $attribute_names); |
|
251 | + $attString .= ' - ' . implode(', ', $attribute_names); |
|
252 | 252 | } |
253 | 253 | return $attString; |
254 | - } |
|
254 | + } |
|
255 | 255 | |
256 | - /** |
|
257 | - * @param $index |
|
258 | - * @return string |
|
259 | - */ |
|
260 | - private static function getHeaderLine($index): string { |
|
256 | + /** |
|
257 | + * @param $index |
|
258 | + * @return string |
|
259 | + */ |
|
260 | + private static function getHeaderLine($index): string { |
|
261 | 261 | |
262 | 262 | return 'SimpleXML object (' . $index . ' item' . |
263 | - ($index > 1 ? 's' : '') . ')' . PHP_EOL; |
|
264 | - } |
|
265 | - |
|
266 | - /** |
|
267 | - * Output a tree-view of the node or list of nodes referenced by a particular |
|
268 | - * SimpleXML object Unlike simplexml_dump(), this processes the entire XML |
|
269 | - * tree recursively, while attempting to be more concise and readable than |
|
270 | - * the XML itself. Additionally, the output format is designed as a hint of |
|
271 | - * the syntax needed to traverse the object. |
|
272 | - * |
|
273 | - * @param \SimpleXMLElement $sxml The object to inspect |
|
274 | - * @param boolean $include_string_content Default false. If true, |
|
275 | - * will summarise textual |
|
276 | - * content, as well as child |
|
277 | - * elements and attribute |
|
278 | - * names |
|
279 | - * @return null|string Nothing, or output, depending on $return param |
|
280 | - * |
|
281 | - */ |
|
282 | - public static function tree(\SimpleXMLElement $sxml, |
|
283 | - $include_string_content = FALSE): string { |
|
263 | + ($index > 1 ? 's' : '') . ')' . PHP_EOL; |
|
264 | + } |
|
265 | + |
|
266 | + /** |
|
267 | + * Output a tree-view of the node or list of nodes referenced by a particular |
|
268 | + * SimpleXML object Unlike simplexml_dump(), this processes the entire XML |
|
269 | + * tree recursively, while attempting to be more concise and readable than |
|
270 | + * the XML itself. Additionally, the output format is designed as a hint of |
|
271 | + * the syntax needed to traverse the object. |
|
272 | + * |
|
273 | + * @param \SimpleXMLElement $sxml The object to inspect |
|
274 | + * @param boolean $include_string_content Default false. If true, |
|
275 | + * will summarise textual |
|
276 | + * content, as well as child |
|
277 | + * elements and attribute |
|
278 | + * names |
|
279 | + * @return null|string Nothing, or output, depending on $return param |
|
280 | + * |
|
281 | + */ |
|
282 | + public static function tree(\SimpleXMLElement $sxml, |
|
283 | + $include_string_content = FALSE): string { |
|
284 | 284 | |
285 | 285 | // Get all the namespaces declared at the *root* of this document |
286 | 286 | // All the items we're looking at are in the same document, so we only need do this once |
@@ -294,30 +294,30 @@ discard block |
||
294 | 294 | // Numeric array indexes, however, operate consistently: $node[0] just returns the node |
295 | 295 | $root_item_index = 0; |
296 | 296 | while (isset($sxml[$root_item_index])) { |
297 | - $root_item = $sxml[$root_item_index]; |
|
297 | + $root_item = $sxml[$root_item_index]; |
|
298 | 298 | |
299 | - // Special case if the root is actually an attribute |
|
300 | - // It's surprisingly hard to find something which behaves consistently differently for an attribute and an element within SimpleXML |
|
301 | - // The below relies on the fact that the DOM makes a much clearer distinction |
|
302 | - // Note that this is not an expensive conversion, as we are only swapping PHP wrappers around an existing LibXML resource |
|
303 | - if (dom_import_simplexml($root_item) instanceOf \DOMAttr) { |
|
299 | + // Special case if the root is actually an attribute |
|
300 | + // It's surprisingly hard to find something which behaves consistently differently for an attribute and an element within SimpleXML |
|
301 | + // The below relies on the fact that the DOM makes a much clearer distinction |
|
302 | + // Note that this is not an expensive conversion, as we are only swapping PHP wrappers around an existing LibXML resource |
|
303 | + if (dom_import_simplexml($root_item) instanceOf \DOMAttr) { |
|
304 | 304 | // To what namespace does this attribute belong? Returns array( alias => URI ) |
305 | 305 | $ns = $root_item->getNamespaces(FALSE); |
306 | 306 | if (key($ns)) { |
307 | - $dump .= key($ns) . ':'; |
|
307 | + $dump .= key($ns) . ':'; |
|
308 | 308 | } |
309 | 309 | $dump .= $root_item->getName() . '="' . (string) $root_item . '"' . |
310 | - PHP_EOL; |
|
311 | - } else { |
|
310 | + PHP_EOL; |
|
311 | + } else { |
|
312 | 312 | // Display the root node as a numeric key reference, plus a hint as to its tag name |
313 | 313 | // e.g. '[42] // <Answer>' |
314 | 314 | |
315 | 315 | // To what namespace does this attribute belong? Returns array( alias => URI ) |
316 | 316 | $ns = $root_item->getNamespaces(FALSE); |
317 | 317 | if (key($ns)) { |
318 | - $root_node_name = key($ns) . ':' . $root_item->getName(); |
|
318 | + $root_node_name = key($ns) . ':' . $root_item->getName(); |
|
319 | 319 | } else { |
320 | - $root_node_name = $root_item->getName(); |
|
320 | + $root_node_name = $root_item->getName(); |
|
321 | 321 | } |
322 | 322 | $dump .= "[$root_item_index] // <$root_node_name>" . PHP_EOL; |
323 | 323 | |
@@ -325,13 +325,13 @@ discard block |
||
325 | 325 | // but this is managed manually using a stack rather than actual recursion |
326 | 326 | // Each item on the stack is of the form array(int $depth, SimpleXMLElement $element, string $header_row) |
327 | 327 | $dump .= SxmlDebug::recursivelyProcessNode( |
328 | - $root_item, |
|
329 | - 1, |
|
330 | - $include_string_content |
|
328 | + $root_item, |
|
329 | + 1, |
|
330 | + $include_string_content |
|
331 | 331 | ); |
332 | - } |
|
332 | + } |
|
333 | 333 | |
334 | - $root_item_index++; |
|
334 | + $root_item_index++; |
|
335 | 335 | } |
336 | 336 | |
337 | 337 | // Add on the header line, with the total number of items output |
@@ -339,39 +339,39 @@ discard block |
||
339 | 339 | |
340 | 340 | return $dump; |
341 | 341 | |
342 | - } |
|
342 | + } |
|
343 | 343 | |
344 | 344 | |
345 | - /** |
|
346 | - * @param string $stringContent |
|
347 | - * @param $depth |
|
348 | - * @return string |
|
349 | - */ |
|
350 | - private static function treeGetStringExtract(string $stringContent, |
|
351 | - $depth): string { |
|
345 | + /** |
|
346 | + * @param string $stringContent |
|
347 | + * @param $depth |
|
348 | + * @return string |
|
349 | + */ |
|
350 | + private static function treeGetStringExtract(string $stringContent, |
|
351 | + $depth): string { |
|
352 | 352 | $string_extract = preg_replace('/\s+/', ' ', trim($stringContent)); |
353 | 353 | if (strlen($string_extract) > SxmlDebug::EXTRACT_SIZE) { |
354 | - $string_extract = substr($string_extract, 0, SxmlDebug::EXTRACT_SIZE) |
|
354 | + $string_extract = substr($string_extract, 0, SxmlDebug::EXTRACT_SIZE) |
|
355 | 355 | . '...'; |
356 | 356 | } |
357 | 357 | return (strlen($stringContent) > 0) ? |
358 | 358 | str_repeat(SxmlDebug::INDENT, $depth) |
359 | - . '(string) ' |
|
360 | - . "'$string_extract'" |
|
361 | - . ' (' . strlen($stringContent) . ' chars)' |
|
362 | - . PHP_EOL : ''; |
|
363 | - |
|
364 | - } |
|
365 | - |
|
366 | - /** |
|
367 | - * @param \SimpleXMLElement $item |
|
368 | - * @return array |
|
369 | - */ |
|
370 | - private static function treeGetNamespaces(\SimpleXMLElement $item): array { |
|
359 | + . '(string) ' |
|
360 | + . "'$string_extract'" |
|
361 | + . ' (' . strlen($stringContent) . ' chars)' |
|
362 | + . PHP_EOL : ''; |
|
363 | + |
|
364 | + } |
|
365 | + |
|
366 | + /** |
|
367 | + * @param \SimpleXMLElement $item |
|
368 | + * @return array |
|
369 | + */ |
|
370 | + private static function treeGetNamespaces(\SimpleXMLElement $item): array { |
|
371 | 371 | // To what namespace does this element belong? Returns array( alias => URI ) |
372 | 372 | $item_ns = $item->getNamespaces(FALSE); |
373 | 373 | if (!$item_ns) { |
374 | - $item_ns = ['' => NULL]; |
|
374 | + $item_ns = ['' => NULL]; |
|
375 | 375 | } |
376 | 376 | |
377 | 377 | // This returns all namespaces used by this node and all its descendants, |
@@ -379,23 +379,23 @@ discard block |
||
379 | 379 | $all_ns = $item->getNamespaces(TRUE); |
380 | 380 | // If the default namespace is never declared, it will never show up using the below code |
381 | 381 | if (!array_key_exists('', $all_ns)) { |
382 | - $all_ns[''] = NULL; |
|
382 | + $all_ns[''] = NULL; |
|
383 | 383 | } |
384 | 384 | |
385 | 385 | // Prioritise "current" namespace by merging into onto the beginning of the list |
386 | 386 | // (it will be added to the beginning and the duplicate entry dropped) |
387 | 387 | return array_merge($item_ns, $all_ns); |
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * @param \SimpleXMLElement $attributes |
|
392 | - * @param string $nsAlias |
|
393 | - * @param int $depth |
|
394 | - * @param bool $isCurrentNamespace |
|
395 | - * @param bool $includeStringContent |
|
396 | - * @return string |
|
397 | - */ |
|
398 | - private static function treeProcessAttributes(\SimpleXMLElement $attributes, |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * @param \SimpleXMLElement $attributes |
|
392 | + * @param string $nsAlias |
|
393 | + * @param int $depth |
|
394 | + * @param bool $isCurrentNamespace |
|
395 | + * @param bool $includeStringContent |
|
396 | + * @return string |
|
397 | + */ |
|
398 | + private static function treeProcessAttributes(\SimpleXMLElement $attributes, |
|
399 | 399 | string $nsAlias, |
400 | 400 | int $depth, |
401 | 401 | bool $isCurrentNamespace, |
@@ -403,128 +403,128 @@ discard block |
||
403 | 403 | |
404 | 404 | $dump = ''; |
405 | 405 | if (count($attributes) > 0) { |
406 | - if (!$isCurrentNamespace) { |
|
406 | + if (!$isCurrentNamespace) { |
|
407 | 407 | $dump .= str_repeat(self::INDENT, $depth) |
408 | - . "->attributes('$nsAlias', true)" . PHP_EOL; |
|
409 | - } |
|
408 | + . "->attributes('$nsAlias', true)" . PHP_EOL; |
|
409 | + } |
|
410 | 410 | |
411 | - foreach ($attributes as $sx_attribute) { |
|
411 | + foreach ($attributes as $sx_attribute) { |
|
412 | 412 | // Output the attribute |
413 | 413 | if ($isCurrentNamespace) { |
414 | - // In current namespace |
|
415 | - // e.g. ['attribName'] |
|
416 | - $dump .= str_repeat(self::INDENT, $depth) |
|
417 | - . "['" . $sx_attribute->getName() . "']" |
|
418 | - . PHP_EOL; |
|
419 | - $string_display_depth = $depth + 1; |
|
414 | + // In current namespace |
|
415 | + // e.g. ['attribName'] |
|
416 | + $dump .= str_repeat(self::INDENT, $depth) |
|
417 | + . "['" . $sx_attribute->getName() . "']" |
|
418 | + . PHP_EOL; |
|
419 | + $string_display_depth = $depth + 1; |
|
420 | 420 | } else { |
421 | - // After a call to ->attributes() |
|
422 | - // e.g. ->attribName |
|
423 | - $dump .= str_repeat(self::INDENT, $depth + 1) |
|
424 | - . '->' . $sx_attribute->getName() |
|
425 | - . PHP_EOL; |
|
426 | - $string_display_depth = $depth + 2; |
|
421 | + // After a call to ->attributes() |
|
422 | + // e.g. ->attribName |
|
423 | + $dump .= str_repeat(self::INDENT, $depth + 1) |
|
424 | + . '->' . $sx_attribute->getName() |
|
425 | + . PHP_EOL; |
|
426 | + $string_display_depth = $depth + 2; |
|
427 | 427 | } |
428 | 428 | |
429 | 429 | if ($includeStringContent) { |
430 | - // Show a chunk of the beginning of the content string, collapsing whitespace HTML-style |
|
431 | - $dump .= self::treeGetStringExtract((string) $sx_attribute, |
|
432 | - $string_display_depth); |
|
430 | + // Show a chunk of the beginning of the content string, collapsing whitespace HTML-style |
|
431 | + $dump .= self::treeGetStringExtract((string) $sx_attribute, |
|
432 | + $string_display_depth); |
|
433 | + } |
|
433 | 434 | } |
434 | - } |
|
435 | 435 | } |
436 | 436 | return $dump; |
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * @param \SimpleXMLElement $children |
|
441 | - * @param string $nsAlias |
|
442 | - * @param int $depth |
|
443 | - * @param bool $isCurrentNamespace |
|
444 | - * @param bool $includeStringContent |
|
445 | - * @return string |
|
446 | - */ |
|
447 | - private static function treeProcessChildren(\SimpleXMLElement $children, |
|
448 | - string $nsAlias, |
|
449 | - int $depth, |
|
450 | - bool $isCurrentNamespace, |
|
451 | - bool $includeStringContent): string { |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * @param \SimpleXMLElement $children |
|
441 | + * @param string $nsAlias |
|
442 | + * @param int $depth |
|
443 | + * @param bool $isCurrentNamespace |
|
444 | + * @param bool $includeStringContent |
|
445 | + * @return string |
|
446 | + */ |
|
447 | + private static function treeProcessChildren(\SimpleXMLElement $children, |
|
448 | + string $nsAlias, |
|
449 | + int $depth, |
|
450 | + bool $isCurrentNamespace, |
|
451 | + bool $includeStringContent): string { |
|
452 | 452 | |
453 | 453 | $dump = ''; |
454 | 454 | if (count($children) > 0) { |
455 | - if ($isCurrentNamespace) { |
|
455 | + if ($isCurrentNamespace) { |
|
456 | 456 | $display_depth = $depth; |
457 | - } else { |
|
457 | + } else { |
|
458 | 458 | $dump .= str_repeat(self::INDENT, $depth) |
459 | - . "->children('$nsAlias', true)" . PHP_EOL; |
|
459 | + . "->children('$nsAlias', true)" . PHP_EOL; |
|
460 | 460 | $display_depth = $depth + 1; |
461 | - } |
|
461 | + } |
|
462 | 462 | |
463 | - // Recurse through the children with headers showing how to access them |
|
464 | - $child_names = []; |
|
465 | - foreach ($children as $sx_child) { |
|
463 | + // Recurse through the children with headers showing how to access them |
|
464 | + $child_names = []; |
|
465 | + foreach ($children as $sx_child) { |
|
466 | 466 | // Below is a rather clunky way of saying $child_names[ $sx_child->getName() ]++; |
467 | 467 | // which avoids Notices about unset array keys |
468 | 468 | $child_node_name = $sx_child->getName(); |
469 | 469 | if (array_key_exists($child_node_name, $child_names)) { |
470 | - $child_names[$child_node_name]++; |
|
470 | + $child_names[$child_node_name]++; |
|
471 | 471 | } else { |
472 | - $child_names[$child_node_name] = 1; |
|
472 | + $child_names[$child_node_name] = 1; |
|
473 | 473 | } |
474 | 474 | |
475 | 475 | // e.g. ->Foo[0] |
476 | 476 | $dump .= str_repeat(self::INDENT, $display_depth) |
477 | - . '->' . $sx_child->getName() |
|
478 | - . '[' . ($child_names[$child_node_name] - 1) . ']' |
|
479 | - . PHP_EOL; |
|
477 | + . '->' . $sx_child->getName() |
|
478 | + . '[' . ($child_names[$child_node_name] - 1) . ']' |
|
479 | + . PHP_EOL; |
|
480 | 480 | |
481 | 481 | $dump .= self::recursivelyProcessNode( |
482 | - $sx_child, |
|
483 | - $display_depth + 1, |
|
484 | - $includeStringContent |
|
482 | + $sx_child, |
|
483 | + $display_depth + 1, |
|
484 | + $includeStringContent |
|
485 | 485 | ); |
486 | - } |
|
486 | + } |
|
487 | 487 | } |
488 | 488 | return $dump; |
489 | - } |
|
490 | - |
|
491 | - /** |
|
492 | - * @param $item |
|
493 | - * @param $depth |
|
494 | - * @param $include_string_content |
|
495 | - * @return string |
|
496 | - */ |
|
497 | - private static function recursivelyProcessNode(\SimpleXMLElement $item, |
|
498 | - $depth, |
|
499 | - $include_string_content): string { |
|
489 | + } |
|
490 | + |
|
491 | + /** |
|
492 | + * @param $item |
|
493 | + * @param $depth |
|
494 | + * @param $include_string_content |
|
495 | + * @return string |
|
496 | + */ |
|
497 | + private static function recursivelyProcessNode(\SimpleXMLElement $item, |
|
498 | + $depth, |
|
499 | + $include_string_content): string { |
|
500 | 500 | |
501 | 501 | $dump = ''; |
502 | 502 | |
503 | 503 | if ($include_string_content) { |
504 | - // Show a chunk of the beginning of the content string, collapsing whitespace HTML-style |
|
505 | - $dump = self::treeGetStringExtract((string) $item, $depth); |
|
504 | + // Show a chunk of the beginning of the content string, collapsing whitespace HTML-style |
|
505 | + $dump = self::treeGetStringExtract((string) $item, $depth); |
|
506 | 506 | } |
507 | 507 | |
508 | 508 | $itemNs = self::treeGetNamespaces($item); |
509 | 509 | foreach ($itemNs as $ns_alias => $ns_uri) { |
510 | 510 | |
511 | 511 | |
512 | - // If things are in the current namespace, display them a bit differently |
|
513 | - $is_current_namespace = ($ns_uri === reset($itemNs)); |
|
512 | + // If things are in the current namespace, display them a bit differently |
|
513 | + $is_current_namespace = ($ns_uri === reset($itemNs)); |
|
514 | 514 | |
515 | - $dump .= self::treeProcessAttributes($item->attributes($ns_alias, TRUE), |
|
516 | - $ns_alias, |
|
517 | - $depth, |
|
518 | - $is_current_namespace, |
|
519 | - $include_string_content); |
|
520 | - $dump .= self::treeProcessChildren($item->children($ns_alias, TRUE), |
|
521 | - $ns_alias, |
|
522 | - $depth, |
|
523 | - $is_current_namespace, |
|
524 | - $include_string_content); |
|
515 | + $dump .= self::treeProcessAttributes($item->attributes($ns_alias, TRUE), |
|
516 | + $ns_alias, |
|
517 | + $depth, |
|
518 | + $is_current_namespace, |
|
519 | + $include_string_content); |
|
520 | + $dump .= self::treeProcessChildren($item->children($ns_alias, TRUE), |
|
521 | + $ns_alias, |
|
522 | + $depth, |
|
523 | + $is_current_namespace, |
|
524 | + $include_string_content); |
|
525 | 525 | } |
526 | 526 | |
527 | 527 | return $dump; |
528 | - } |
|
528 | + } |
|
529 | 529 | |
530 | 530 | } |
531 | 531 | \ No newline at end of file |
@@ -39,7 +39,7 @@ discard block |
||
39 | 39 | |
40 | 40 | $dump = ''; |
41 | 41 | // Note that the header is added at the end, so we can add stats |
42 | - $dump .= '[' . PHP_EOL; |
|
42 | + $dump .= '['.PHP_EOL; |
|
43 | 43 | |
44 | 44 | // SimpleXML objects can be either a single node, or (more commonly) a list of 0 or more nodes |
45 | 45 | // I haven't found a reliable way of distinguishing between the two cases |
@@ -61,10 +61,10 @@ discard block |
||
61 | 61 | $dump .= self::dumpAddElement($item); |
62 | 62 | } |
63 | 63 | } |
64 | - $dump .= ']' . PHP_EOL; |
|
64 | + $dump .= ']'.PHP_EOL; |
|
65 | 65 | |
66 | 66 | // Add on the header line, with the total number of items output |
67 | - return self::getHeaderLine($item_index) . $dump; |
|
67 | + return self::getHeaderLine($item_index).$dump; |
|
68 | 68 | } |
69 | 69 | |
70 | 70 | /** |
@@ -77,15 +77,15 @@ discard block |
||
77 | 77 | // To what namespace does this attribute belong? Returns array( alias => URI ) |
78 | 78 | $ns = $item->getNamespaces(FALSE); |
79 | 79 | if ($ns) { |
80 | - $dump .= self::INDENT . self::INDENT . 'Namespace: \'' . reset($ns) . |
|
81 | - '\'' . |
|
80 | + $dump .= self::INDENT.self::INDENT.'Namespace: \''.reset($ns). |
|
81 | + '\''. |
|
82 | 82 | PHP_EOL; |
83 | 83 | if (key($ns) == '') { |
84 | - $dump .= self::INDENT . self::INDENT . '(Default Namespace)' . PHP_EOL; |
|
84 | + $dump .= self::INDENT.self::INDENT.'(Default Namespace)'.PHP_EOL; |
|
85 | 85 | } else { |
86 | - $dump .= self::INDENT . self::INDENT . 'Namespace Alias: \'' . |
|
87 | - key($ns) . |
|
88 | - '\'' . |
|
86 | + $dump .= self::INDENT.self::INDENT.'Namespace Alias: \''. |
|
87 | + key($ns). |
|
88 | + '\''. |
|
89 | 89 | PHP_EOL; |
90 | 90 | } |
91 | 91 | } |
@@ -104,9 +104,9 @@ discard block |
||
104 | 104 | $data, |
105 | 105 | $indent = 1, |
106 | 106 | $backtick = TRUE): string { |
107 | - return str_repeat(self::INDENT, $indent) . $title . ': ' . |
|
108 | - ($backtick ? '\'' : '') . $data . |
|
109 | - ($backtick ? '\'' : '') . PHP_EOL; |
|
107 | + return str_repeat(self::INDENT, $indent).$title.': '. |
|
108 | + ($backtick ? '\'' : '').$data. |
|
109 | + ($backtick ? '\'' : '').PHP_EOL; |
|
110 | 110 | } |
111 | 111 | |
112 | 112 | /** |
@@ -115,14 +115,14 @@ discard block |
||
115 | 115 | */ |
116 | 116 | private static function dumpAddAttribute(\SimpleXMLElement $item): string { |
117 | 117 | |
118 | - $dump = self::INDENT . 'Attribute {' . PHP_EOL; |
|
118 | + $dump = self::INDENT.'Attribute {'.PHP_EOL; |
|
119 | 119 | |
120 | 120 | $dump .= self::dumpAddNamespace($item); |
121 | 121 | |
122 | 122 | $dump .= self::dumpGetLine('Name', $item->getName(), 2); |
123 | 123 | $dump .= self::dumpGetLine('Value', (string) $item, 2); |
124 | 124 | |
125 | - $dump .= self::INDENT . '}' . PHP_EOL; |
|
125 | + $dump .= self::INDENT.'}'.PHP_EOL; |
|
126 | 126 | return $dump; |
127 | 127 | |
128 | 128 | } |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | */ |
134 | 134 | private static function dumpAddElement(\SimpleXMLElement $item): string { |
135 | 135 | |
136 | - $dump = self::INDENT . 'Element {' . PHP_EOL; |
|
136 | + $dump = self::INDENT.'Element {'.PHP_EOL; |
|
137 | 137 | |
138 | 138 | $dump .= self::dumpAddNamespace($item); |
139 | 139 | |
@@ -176,10 +176,9 @@ discard block |
||
176 | 176 | continue; |
177 | 177 | } |
178 | 178 | |
179 | - $ns_label = (($ns_alias === '') ? 'Default Namespace' : |
|
180 | - "Namespace $ns_alias"); |
|
179 | + $ns_label = (($ns_alias === '') ? 'Default Namespace' : "Namespace $ns_alias"); |
|
181 | 180 | |
182 | - $dump .= self::INDENT . self::INDENT . 'Content in ' . $ns_label . |
|
181 | + $dump .= self::INDENT.self::INDENT.'Content in '.$ns_label. |
|
183 | 182 | PHP_EOL; |
184 | 183 | |
185 | 184 | if (NULL !== $ns_uri) { |
@@ -199,7 +198,7 @@ discard block |
||
199 | 198 | FALSE); |
200 | 199 | } |
201 | 200 | |
202 | - return $dump . self::INDENT . '}' . PHP_EOL; |
|
201 | + return $dump.self::INDENT.'}'.PHP_EOL; |
|
203 | 202 | } |
204 | 203 | |
205 | 204 | /** |
@@ -228,7 +227,7 @@ discard block |
||
228 | 227 | $childrenString = count($children); |
229 | 228 | // Don't output a trailing " - " if there are no children |
230 | 229 | if (count($children) > 0) { |
231 | - $childrenString .= ' - ' . implode(', ', $child_name_output); |
|
230 | + $childrenString .= ' - '.implode(', ', $child_name_output); |
|
232 | 231 | } |
233 | 232 | return $childrenString; |
234 | 233 | } |
@@ -241,14 +240,14 @@ discard block |
||
241 | 240 | // Attributes can't be duplicated, but I'm going to put them in alphabetical order |
242 | 241 | $attribute_names = []; |
243 | 242 | foreach ($attributes as $sx_attribute) { |
244 | - $attribute_names[] = "'" . $sx_attribute->getName() . "'"; |
|
243 | + $attribute_names[] = "'".$sx_attribute->getName()."'"; |
|
245 | 244 | } |
246 | 245 | ksort($attribute_names); |
247 | 246 | |
248 | 247 | $attString = count($attributes); |
249 | 248 | // Don't output a trailing " - " if there are no attributes |
250 | 249 | if (count($attributes) > 0) { |
251 | - $attString .= ' - ' . implode(', ', $attribute_names); |
|
250 | + $attString .= ' - '.implode(', ', $attribute_names); |
|
252 | 251 | } |
253 | 252 | return $attString; |
254 | 253 | } |
@@ -259,8 +258,8 @@ discard block |
||
259 | 258 | */ |
260 | 259 | private static function getHeaderLine($index): string { |
261 | 260 | |
262 | - return 'SimpleXML object (' . $index . ' item' . |
|
263 | - ($index > 1 ? 's' : '') . ')' . PHP_EOL; |
|
261 | + return 'SimpleXML object ('.$index.' item'. |
|
262 | + ($index > 1 ? 's' : '').')'.PHP_EOL; |
|
264 | 263 | } |
265 | 264 | |
266 | 265 | /** |
@@ -304,9 +303,9 @@ discard block |
||
304 | 303 | // To what namespace does this attribute belong? Returns array( alias => URI ) |
305 | 304 | $ns = $root_item->getNamespaces(FALSE); |
306 | 305 | if (key($ns)) { |
307 | - $dump .= key($ns) . ':'; |
|
306 | + $dump .= key($ns).':'; |
|
308 | 307 | } |
309 | - $dump .= $root_item->getName() . '="' . (string) $root_item . '"' . |
|
308 | + $dump .= $root_item->getName().'="'.(string) $root_item.'"'. |
|
310 | 309 | PHP_EOL; |
311 | 310 | } else { |
312 | 311 | // Display the root node as a numeric key reference, plus a hint as to its tag name |
@@ -315,11 +314,11 @@ discard block |
||
315 | 314 | // To what namespace does this attribute belong? Returns array( alias => URI ) |
316 | 315 | $ns = $root_item->getNamespaces(FALSE); |
317 | 316 | if (key($ns)) { |
318 | - $root_node_name = key($ns) . ':' . $root_item->getName(); |
|
317 | + $root_node_name = key($ns).':'.$root_item->getName(); |
|
319 | 318 | } else { |
320 | 319 | $root_node_name = $root_item->getName(); |
321 | 320 | } |
322 | - $dump .= "[$root_item_index] // <$root_node_name>" . PHP_EOL; |
|
321 | + $dump .= "[$root_item_index] // <$root_node_name>".PHP_EOL; |
|
323 | 322 | |
324 | 323 | // This function is effectively recursing depth-first through the tree, |
325 | 324 | // but this is managed manually using a stack rather than actual recursion |
@@ -335,7 +334,7 @@ discard block |
||
335 | 334 | } |
336 | 335 | |
337 | 336 | // Add on the header line, with the total number of items output |
338 | - $dump = self::getHeaderLine($root_item_index) . $dump; |
|
337 | + $dump = self::getHeaderLine($root_item_index).$dump; |
|
339 | 338 | |
340 | 339 | return $dump; |
341 | 340 | |
@@ -358,7 +357,7 @@ discard block |
||
358 | 357 | str_repeat(SxmlDebug::INDENT, $depth) |
359 | 358 | . '(string) ' |
360 | 359 | . "'$string_extract'" |
361 | - . ' (' . strlen($stringContent) . ' chars)' |
|
360 | + . ' ('.strlen($stringContent).' chars)' |
|
362 | 361 | . PHP_EOL : ''; |
363 | 362 | |
364 | 363 | } |
@@ -405,7 +404,7 @@ discard block |
||
405 | 404 | if (count($attributes) > 0) { |
406 | 405 | if (!$isCurrentNamespace) { |
407 | 406 | $dump .= str_repeat(self::INDENT, $depth) |
408 | - . "->attributes('$nsAlias', true)" . PHP_EOL; |
|
407 | + . "->attributes('$nsAlias', true)".PHP_EOL; |
|
409 | 408 | } |
410 | 409 | |
411 | 410 | foreach ($attributes as $sx_attribute) { |
@@ -414,14 +413,14 @@ discard block |
||
414 | 413 | // In current namespace |
415 | 414 | // e.g. ['attribName'] |
416 | 415 | $dump .= str_repeat(self::INDENT, $depth) |
417 | - . "['" . $sx_attribute->getName() . "']" |
|
416 | + . "['".$sx_attribute->getName()."']" |
|
418 | 417 | . PHP_EOL; |
419 | 418 | $string_display_depth = $depth + 1; |
420 | 419 | } else { |
421 | 420 | // After a call to ->attributes() |
422 | 421 | // e.g. ->attribName |
423 | 422 | $dump .= str_repeat(self::INDENT, $depth + 1) |
424 | - . '->' . $sx_attribute->getName() |
|
423 | + . '->'.$sx_attribute->getName() |
|
425 | 424 | . PHP_EOL; |
426 | 425 | $string_display_depth = $depth + 2; |
427 | 426 | } |
@@ -456,7 +455,7 @@ discard block |
||
456 | 455 | $display_depth = $depth; |
457 | 456 | } else { |
458 | 457 | $dump .= str_repeat(self::INDENT, $depth) |
459 | - . "->children('$nsAlias', true)" . PHP_EOL; |
|
458 | + . "->children('$nsAlias', true)".PHP_EOL; |
|
460 | 459 | $display_depth = $depth + 1; |
461 | 460 | } |
462 | 461 | |
@@ -474,8 +473,8 @@ discard block |
||
474 | 473 | |
475 | 474 | // e.g. ->Foo[0] |
476 | 475 | $dump .= str_repeat(self::INDENT, $display_depth) |
477 | - . '->' . $sx_child->getName() |
|
478 | - . '[' . ($child_names[$child_node_name] - 1) . ']' |
|
476 | + . '->'.$sx_child->getName() |
|
477 | + . '['.($child_names[$child_node_name] - 1).']' |
|
479 | 478 | . PHP_EOL; |
480 | 479 | |
481 | 480 | $dump .= self::recursivelyProcessNode( |