The following rules applies during the conversion:
* Each tag is converted to a key value or an array
if there is more than one "value"
* The content of a tag is set under a "value" key (bar )
if the tag also has some nested tags
* The attributes are converted to keys ()
* The nested-tags are converted to keys (bar )
public static convertDomElementToArray ( DOMElement $element ) : array | ||
$element | DOMElement | A \DomElement instance |
return | array | A PHP array |
public function testConvertDomElementToArray() { $doc = new \DOMDocument('1.0'); $doc->loadXML('<foo>bar</foo>'); $this->assertEquals('bar', XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \\DomElement to an array'); $doc = new \DOMDocument('1.0'); $doc->loadXML('<foo foo="bar" />'); $this->assertEquals(array('foo' => 'bar'), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \\DomElement to an array'); $doc = new \DOMDocument('1.0'); $doc->loadXML('<foo><foo>bar</foo></foo>'); $this->assertEquals(array('foo' => 'bar'), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \\DomElement to an array'); $doc = new \DOMDocument('1.0'); $doc->loadXML('<foo><foo>bar<foo>bar</foo></foo></foo>'); $this->assertEquals(array('foo' => array('value' => 'bar', 'foo' => 'bar')), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \\DomElement to an array'); $doc = new \DOMDocument('1.0'); $doc->loadXML('<foo><foo></foo></foo>'); $this->assertEquals(array('foo' => null), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \\DomElement to an array'); $doc = new \DOMDocument('1.0'); $doc->loadXML('<foo><foo><!-- foo --></foo></foo>'); $this->assertEquals(array('foo' => null), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \\DomElement to an array'); $doc = new \DOMDocument('1.0'); $doc->loadXML('<foo><foo foo="bar"/><foo foo="bar"/></foo>'); $this->assertEquals(array('foo' => array(array('foo' => 'bar'), array('foo' => 'bar'))), XmlFileLoader::convertDomElementToArray($doc->documentElement), '::convertDomElementToArray() converts a \\DomElement to an array'); }