[Config] Added parameter to check prefix in an element or an attribute name in XmlUtils::convertDomElementToArray
This commit is contained in:
parent
7f5a7fd432
commit
45aab0f1e0
@ -63,18 +63,20 @@ class XmlUtilsTest extends \PHPUnit_Framework_TestCase
|
||||
/**
|
||||
* @dataProvider getDataForConvertDomToArray
|
||||
*/
|
||||
public function testConvertDomToArray($expected, $xml)
|
||||
public function testConvertDomToArray($expected, $xml, $root = false, $checkPrefix = true)
|
||||
{
|
||||
$dom = new \DOMDocument();
|
||||
$dom->loadXML('<root>'.$xml.'</root>');
|
||||
$dom->loadXML($root ? $xml : '<root>'.$xml.'</root>');
|
||||
|
||||
$this->assertSame($expected, XmlUtils::convertDomElementToArray($dom->documentElement));
|
||||
$this->assertSame($expected, XmlUtils::convertDomElementToArray($dom->documentElement, $checkPrefix));
|
||||
}
|
||||
|
||||
public function getDataForConvertDomToArray()
|
||||
{
|
||||
return array(
|
||||
array(null, ''),
|
||||
array('bar', 'bar'),
|
||||
array(array('bar' => 'foobar'), '<foo bar="foobar" />', true),
|
||||
array(array('foo' => null), '<foo />'),
|
||||
array(array('foo' => 'bar'), '<foo>bar</foo>'),
|
||||
array(array('foo' => array('foo' => 'bar')), '<foo foo="bar"/>'),
|
||||
@ -85,6 +87,9 @@ class XmlUtilsTest extends \PHPUnit_Framework_TestCase
|
||||
array(array('foo' => array(array('foo' => 'bar'), array('foo' => 'text'))), '<foo foo="bar"/><foo foo="text" />'),
|
||||
array(array('foo' => array('foo' => array('bar', 'text'))), '<foo foo="bar"><foo>text</foo></foo>'),
|
||||
array(array('foo' => 'bar'), '<foo><!-- Comment -->bar</foo>'),
|
||||
array(array('foo' => 'text'), '<foo xmlns:h="http://www.example.org/bar" h:bar="bar">text</foo>'),
|
||||
array(array('foo' => array('bar' => 'bar', 'value' => 'text')), '<foo xmlns:h="http://www.example.org/bar" h:bar="bar">text</foo>', false, false),
|
||||
array(array('attr' => 1, 'b' => 'hello'), '<foo:a xmlns:foo="http://www.example.org/foo" xmlns:h="http://www.example.org/bar" attr="1" h:bar="bar"><foo:b>hello</foo:b><h:c>2</h:c></foo:a>', true),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -111,15 +111,20 @@ class XmlUtils
|
||||
*
|
||||
* * The nested-tags are converted to keys (<foo><foo>bar</foo></foo>)
|
||||
*
|
||||
* @param \DomElement $element A \DomElement instance
|
||||
* @param \DomElement $element A \DomElement instance
|
||||
* @param Boolean $checkPrefix Check prefix in an element or an attribute name
|
||||
*
|
||||
* @return array A PHP array
|
||||
*/
|
||||
public static function convertDomElementToArray(\DomElement $element)
|
||||
public static function convertDomElementToArray(\DomElement $element, $checkPrefix = true)
|
||||
{
|
||||
$prefix = (string) $element->prefix;
|
||||
$empty = true;
|
||||
$config = array();
|
||||
foreach ($element->attributes as $name => $node) {
|
||||
if ($checkPrefix && !in_array((string) $node->prefix, array('', $prefix), true)) {
|
||||
continue;
|
||||
}
|
||||
$config[$name] = static::phpize($node->value);
|
||||
$empty = false;
|
||||
}
|
||||
@ -131,8 +136,10 @@ class XmlUtils
|
||||
$nodeValue = trim($node->nodeValue);
|
||||
$empty = false;
|
||||
}
|
||||
} elseif ($checkPrefix && $prefix != (string) $node->prefix) {
|
||||
continue;
|
||||
} elseif (!$node instanceof \DOMComment) {
|
||||
$value = static::convertDomElementToArray($node);
|
||||
$value = static::convertDomElementToArray($node, $checkPrefix);
|
||||
|
||||
$key = $node->localName;
|
||||
if (isset($config[$key])) {
|
||||
|
Reference in New Issue
Block a user