Version 5.1 - All the GraphQL #32

Closed
timw4mail wants to merge 1160 commits from develop into master
2 changed files with 50 additions and 4 deletions
Showing only changes of commit 3f90e93af1 - Show all commits

View File

@ -179,15 +179,43 @@ class XML {
for ($i = 0; $i < $length; $i++) for ($i = 0; $i < $length; $i++)
{ {
$el = $nodeList->item($i); $el = $nodeList->item($i);
$current =& $root[$el->nodeName];
// It's a top level element!
if (is_a($el->childNodes->item(0), 'DomText') || ( ! $el->hasChildNodes())) if (is_a($el->childNodes->item(0), 'DomText') || ( ! $el->hasChildNodes()))
{ {
$root[$el->nodeName] = $el->textContent; $current = $el->textContent;
continue;
} }
else
// An empty value at the current root
if (is_null($current))
{ {
$root[$el->nodeName] = []; $current = [];
static::childNodesToArray($root[$el->nodeName], $el->childNodes); static::childNodesToArray($current, $el->childNodes);
continue;
} }
$keys = array_keys($current);
// Wrap the array in a containing array
// if there are only string keys
if ( ! is_numeric($keys[0]))
{
// But if there is only one key, don't wrap it in
// an array, just recurse to parse the child nodes
if (count($current) === 1)
{
static::childNodesToArray($current, $el->childNodes);
continue;
}
$current = [$current];
}
array_push($current, []);
$index = count($current) - 1;
static::childNodesToArray($current[$index], $el->childNodes);
} }
} }
@ -203,7 +231,17 @@ class XML {
{ {
foreach($data as $key => $props) foreach($data as $key => $props)
{ {
// 'Flatten' the array as you create the xml
if (is_numeric($key))
{
foreach($props as $key => $props)
{
break;
}
}
$node = $dom->createElement($key); $node = $dom->createElement($key);
if (is_array($props)) if (is_array($props))
{ {
static::arrayPropertiesToXmlNodes($dom, $node, $props); static::arrayPropertiesToXmlNodes($dom, $node, $props);

View File

@ -9,6 +9,7 @@ class XMLTest extends TestCase {
public function setUp() public function setUp()
{ {
$this->malExport = file_get_contents(__DIR__ . '/../test_data/XML/MALExport.xml');
$this->xml = file_get_contents(__DIR__ . '/../test_data/XML/xmlTestFile.xml'); $this->xml = file_get_contents(__DIR__ . '/../test_data/XML/xmlTestFile.xml');
$this->expectedXml = file_get_contents(__DIR__ . '/../test_data/XML/minifiedXmlTestFile.xml'); $this->expectedXml = file_get_contents(__DIR__ . '/../test_data/XML/minifiedXmlTestFile.xml');
@ -43,6 +44,13 @@ class XMLTest extends TestCase {
{ {
$this->assertEquals($this->array, XML::toArray($this->xml)); $this->assertEquals($this->array, XML::toArray($this->xml));
} }
public function testMALExport()
{
$array = XML::toArray($this->malExport);
$this->assertEquals($array['myanimelist']['myinfo']['user_total_anime'], count($array['myanimelist']['anime']));
// $this->assertEquals($array, XML::toArray($this->malExport));
}
public function testParse() public function testParse()
{ {