Skip to content

Commit

Permalink
Add the SimpleXmlElementTreeDumper to be able to dump simple xml tree…
Browse files Browse the repository at this point in the history
…s including content
  • Loading branch information
jspeedz committed Oct 3, 2019
1 parent bb141f5 commit fd2f004
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 2 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,25 @@ Result:
]
```


### Dumping simple XML element object trees
Converting and dumping SimpleXmlObjects into a readable tree string.

#### Example 1:

```php
dumpSimpleXmlElementTree(
simplexml_load_string('<?xml version="1.0" encoding="utf-8" ?><root><someElement>someValue</someElement></root>')
);
```
Result:

```SimpleXML object (1 item)
[0] // <root>
->someElement[0]
(string) 'someValue' (9 chars)
```

## Utilities
### Timing a block of code
```php
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"src/Component/VarDumper/Resources/functions/dump.php",
"src/Component/VarDumper/Resources/functions/dumpquery.php",
"src/Component/VarDumper/Resources/functions/dumprequest.php",
"src/Component/VarDumper/Resources/functions/dumpsimplexmlelement.php"
"src/Component/VarDumper/Resources/functions/dumpsimplexmlelement.php",
"src/Component/VarDumper/Resources/functions/dumpsimplexmlelementtree.php"
],
"exclude-from-classmap": [
"/Tests/"
Expand Down
3 changes: 2 additions & 1 deletion src/Component/VarDumper/Resources/functions/dumpquery.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
* @param string $query
* @param array $parameters
* @param int[] $types
*
* @return void
*
* @throws Jspeedz\DebugPooper\Exception\InvalidParameterException
* @throws \Jspeedz\DebugPooper\Exception\InvalidParameterCountException
*/
function dumpQuery(string $query, array $parameters = [], array $types = []): void {
QueryDumper::dump($query, $parameters, $types);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
use Jspeedz\DebugPooper\Pooper\SimpleXmlElementTreeDumper;

if(!function_exists('dumpSimpleXmlElementTree')) {
/**
* @param SimpleXMLElement $simpleXMLElement
* @param bool $includeStringContent = true If true, will summarise textual content, as well as child elements and attribute names
* @param bool $return = false
*
* @return void
*/
function dumpSimpleXmlElementTree(SimpleXMLElement $simpleXMLElement, bool $includeStringContent = true, bool $return = false): void {
SimpleXmlElementTreeDumper::dump($simpleXMLElement, $includeStringContent, $return);
}
}
31 changes: 31 additions & 0 deletions src/Pooper/SimpleXmlElementTreeDumper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Jspeedz\DebugPooper\Pooper;

use SimpleXMLElement;

require_once __DIR__ . '/../../vendor-nopackage/simplexml_debug-1.0.0/src/simplexml_tree.php';

// Load the global 💩() function (and in extension, dump())
require_once __DIR__ . '/../Component/VarDumper/Resources/functions/dump.php';

class SimpleXmlElementTreeDumper {
/**
* @param SimpleXMLElement $element
* @param bool $includeStringContent = true If true, will summarise textual content, as well as child elements and attribute names
* @param bool $return = false
*
* @return string|null
*/
public static function dump(SimpleXMLElement $element, bool $includeStringContent = true, bool $return = false) {
$result = simplexml_tree($element, $includeStringContent, true);

if(!$return) {
dump($result);

return null;
}
else {
return $result;
}
}
}
54 changes: 54 additions & 0 deletions src/Tests/Pooper/SimpleXmlElementTreeDumperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
namespace Jspeedz\DebugPooper\Tests\Pooper;

use Jspeedz\DebugPooper\Pooper\SimpleXmlElementTreeDumper;
use PHPUnit\Framework\TestCase;

class SimpleXmlElementTreeDumperTest extends TestCase {
public function testDumpReturnOutputWithoutStringContent() {
$element = simplexml_load_string('<?xml version="1.0" encoding="utf-8" ?>
<root>
<this>
<is>Some</is>
<test>
<that>Will validate</that>
</test>
<z our="dumper">xx</z>
</this>
</root>');

$result = SimpleXmlElementTreeDumper::dump($element, false, true);
$result = str_replace("\n", "", $result);
$result = str_replace("\r", "", $result);

$expected = "SimpleXML object (1 item)[0] // <root> ->this[0] ->is[0] ->test[0] ->that[0] ->z[0] ['our']";
$expected = str_replace("\n", "", $expected);
$expected = str_replace("\r", "", $expected);

$this->assertEquals($expected, $result);
}

public function testDumpReturnOutputWithStringContent() {
$element = simplexml_load_string('<?xml version="1.0" encoding="utf-8" ?>
<root>
<this>
<is>Some</is>
<test>
<that>Will validate</that>
</test>
<z our="dumper">xx</z>
</this>
</root>');

$result = SimpleXmlElementTreeDumper::dump($element, true, true);

$result = str_replace("\n", "", $result);
$result = str_replace("\r", "", $result);

$expected = "SimpleXML object (1 item)[0] // <root> (string) '' (6 chars) ->this[0] (string) '' (32 chars) ->is[0] (string) 'Some' (4 chars) ->test[0] (string) '' (22 chars) ->that[0] (string) 'Will validate' (13 chars) ->z[0] (string) 'xx' (2 chars) ['our'] (string) 'dumper' (6 chars)";
$expected = str_replace("\n", "", $expected);
$expected = str_replace("\r", "", $expected);

$this->assertEquals($expected, $result);
}
}

0 comments on commit fd2f004

Please sign in to comment.