Always list attributes, with only the string content optional

This commit is contained in:
Rowan Collins 2012-10-14 01:13:24 +01:00
parent e092558286
commit 5231f710de

View file

@ -7,8 +7,8 @@
* Additionally, the output format is designed as a hint of the syntax needed to traverse the object. * Additionally, the output format is designed as a hint of the syntax needed to traverse the object.
* *
* @param SimpleXMLElement $sxml The object to inspect * @param SimpleXMLElement $sxml The object to inspect
* @param boolean $include_attributes_and_content Default false. If true, will summarise attributes * @param boolean $include_string_content Default false. If true, will summarise textual content,
* and textual content, as well as child elements * as well as child elements and attribute names
* @param boolean $return Default false. If true, return the "dumped" info rather than echoing it * @param boolean $return Default false. If true, return the "dumped" info rather than echoing it
* @return null|string Nothing, or output, depending on $return param * @return null|string Nothing, or output, depending on $return param
* *
@ -16,7 +16,7 @@
* @see https://github.com/IMSoP/simplexml_debug * @see https://github.com/IMSoP/simplexml_debug
* @license http://creativecommons.org/licenses/by/3.0/ Creative Commons Attribution 3.0 * @license http://creativecommons.org/licenses/by/3.0/ Creative Commons Attribution 3.0
*/ */
function simplexml_tree(SimpleXMLElement $sxml, $include_attributes_and_content=false, $return=false) function simplexml_tree(SimpleXMLElement $sxml, $include_string_content=false, $return=false)
{ {
$indent = "\t"; $indent = "\t";
$content_extract_size = 15; $content_extract_size = 15;
@ -73,7 +73,7 @@ function simplexml_tree(SimpleXMLElement $sxml, $include_attributes_and_content=
// Each item on the stack is of the form array(int $depth, SimpleXMLElement $element, string $header_row) // Each item on the stack is of the form array(int $depth, SimpleXMLElement $element, string $header_row)
$dump .= _simplexml_tree_recursively_process_node( $dump .= _simplexml_tree_recursively_process_node(
$root_item, 1, $root_item, 1,
$include_attributes_and_content, $indent, $content_extract_size $include_string_content, $indent, $content_extract_size
); );
} }
@ -94,11 +94,11 @@ function simplexml_tree(SimpleXMLElement $sxml, $include_attributes_and_content=
* "Private" function to perform the recursive part of simplexml_tree() * "Private" function to perform the recursive part of simplexml_tree()
* Do not call this function directly or rely on its function signature remaining stable * Do not call this function directly or rely on its function signature remaining stable
*/ */
function _simplexml_tree_recursively_process_node($item, $depth, $include_attributes_and_content, $indent, $content_extract_size) function _simplexml_tree_recursively_process_node($item, $depth, $include_string_content, $indent, $content_extract_size)
{ {
$dump = ''; $dump = '';
if ( $include_attributes_and_content ) if ( $include_string_content )
{ {
// Show a chunk of the beginning of the content string, collapsing whitespace HTML-style // Show a chunk of the beginning of the content string, collapsing whitespace HTML-style
$string_content = (string)$item; $string_content = (string)$item;
@ -148,7 +148,7 @@ function _simplexml_tree_recursively_process_node($item, $depth, $include_attrib
// If things are in the current namespace, display them a bit differently // If things are in the current namespace, display them a bit differently
$is_current_namespace = ( $ns_uri == reset($item_ns) ); $is_current_namespace = ( $ns_uri == reset($item_ns) );
if ( count($attributes) > 0 && $include_attributes_and_content ) if ( count($attributes) > 0 )
{ {
if ( ! $is_current_namespace ) if ( ! $is_current_namespace )
{ {
@ -157,6 +157,28 @@ function _simplexml_tree_recursively_process_node($item, $depth, $include_attrib
} }
foreach ( $attributes as $sx_attribute ) foreach ( $attributes as $sx_attribute )
{
// Output the attribute
if ( $is_current_namespace )
{
// In current namespace
// e.g. ['attribName']
$dump .= str_repeat($indent, $depth)
. "['" . $sx_attribute->getName() . "']"
. PHP_EOL;
$string_display_depth = $depth+1;
}
else
{
// After a call to ->attributes()
// e.g. ->attribName
$dump .= str_repeat($indent, $depth+1)
. '->' . $sx_attribute->getName()
. PHP_EOL;
$string_display_depth = $depth+2;
}
if ( $include_string_content )
{ {
// Show a chunk of the beginning of the content string, collapsing whitespace HTML-style // Show a chunk of the beginning of the content string, collapsing whitespace HTML-style
$string_content = (string)$sx_attribute; $string_content = (string)$sx_attribute;
@ -168,28 +190,7 @@ function _simplexml_tree_recursively_process_node($item, $depth, $include_attrib
. '...'; . '...';
} }
// Output the attribute $dump .= str_repeat($indent, $string_display_depth)
if ( $is_current_namespace )
{
// In current namespace
// e.g. ['attribName']
$dump .= str_repeat($indent, $depth)
. "['" . $sx_attribute->getName() . "']"
. PHP_EOL;
$dump .= str_repeat($indent, $depth+1)
. '(string) '
. "'$string_extract'"
. ' (' . strlen($string_content) . ' chars)'
. PHP_EOL;
}
else
{
// After a call to ->attributes()
// e.g. ->attribName
$dump .= str_repeat($indent, $depth+1)
. '->' . $sx_attribute->getName()
. PHP_EOL;
$dump .= str_repeat($indent, $depth+2)
. '(string) ' . '(string) '
. "'$string_extract'" . "'$string_extract'"
. ' (' . strlen($string_content) . ' chars)' . ' (' . strlen($string_content) . ' chars)'
@ -235,7 +236,7 @@ function _simplexml_tree_recursively_process_node($item, $depth, $include_attrib
$dump .= _simplexml_tree_recursively_process_node( $dump .= _simplexml_tree_recursively_process_node(
$sx_child, $display_depth+1, $sx_child, $display_depth+1,
$include_attributes_and_content, $indent, $content_extract_size $include_string_content, $indent, $content_extract_size
); );
} }
} }