From 45aab0f1e065083f33a19484f009ddc62ec7feea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Haso=C5=88?= Date: Fri, 9 Nov 2012 11:08:58 +0100 Subject: [PATCH] [Config] Added parameter to check prefix in an element or an attribute name in XmlUtils::convertDomElementToArray --- .../Component/Config/Tests/Util/XmlUtilsTest.php | 11 ++++++++--- src/Symfony/Component/Config/Util/XmlUtils.php | 13 ++++++++++--- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php b/src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php index 0ad0b19576..a3d264e189 100644 --- a/src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php +++ b/src/Symfony/Component/Config/Tests/Util/XmlUtilsTest.php @@ -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(''.$xml.''); + $dom->loadXML($root ? $xml : ''.$xml.''); - $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'), '', true), array(array('foo' => null), ''), array(array('foo' => 'bar'), 'bar'), array(array('foo' => array('foo' => 'bar')), ''), @@ -85,6 +87,9 @@ class XmlUtilsTest extends \PHPUnit_Framework_TestCase array(array('foo' => array(array('foo' => 'bar'), array('foo' => 'text'))), ''), array(array('foo' => array('foo' => array('bar', 'text'))), 'text'), array(array('foo' => 'bar'), 'bar'), + array(array('foo' => 'text'), 'text'), + array(array('foo' => array('bar' => 'bar', 'value' => 'text')), 'text', false, false), + array(array('attr' => 1, 'b' => 'hello'), 'hello2', true), ); } diff --git a/src/Symfony/Component/Config/Util/XmlUtils.php b/src/Symfony/Component/Config/Util/XmlUtils.php index 8ab8225df5..9317e15e7c 100644 --- a/src/Symfony/Component/Config/Util/XmlUtils.php +++ b/src/Symfony/Component/Config/Util/XmlUtils.php @@ -111,15 +111,20 @@ class XmlUtils * * * The nested-tags are converted to keys (bar) * - * @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])) {