fix handling of nullable XML attributes
As @Tobion pointed out in #11394, true and 1 are valid values in boolean XML attributes. The XmlFileLoader didn't handle 1 values properly.
This commit is contained in:
parent
65862c9947
commit
7b4d4b63eb
@ -215,7 +215,7 @@ class XmlFileLoader extends FileLoader
|
|||||||
foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) {
|
foreach ($node->getElementsByTagNameNS(self::NAMESPACE_URI, '*') as $n) {
|
||||||
switch ($n->localName) {
|
switch ($n->localName) {
|
||||||
case 'default':
|
case 'default':
|
||||||
if ($n->hasAttribute('xsi:nil') && 'true' == $n->getAttribute('xsi:nil')) {
|
if ($this->isElementValueNull($n)) {
|
||||||
$defaults[$n->getAttribute('key')] = null;
|
$defaults[$n->getAttribute('key')] = null;
|
||||||
} else {
|
} else {
|
||||||
$defaults[$n->getAttribute('key')] = trim($n->textContent);
|
$defaults[$n->getAttribute('key')] = trim($n->textContent);
|
||||||
@ -235,4 +235,15 @@ class XmlFileLoader extends FileLoader
|
|||||||
|
|
||||||
return array($defaults, $requirements, $options);
|
return array($defaults, $requirements, $options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function isElementValueNull(\DOMElement $element)
|
||||||
|
{
|
||||||
|
$namespaceUri = 'http://www.w3.org/2001/XMLSchema-instance';
|
||||||
|
|
||||||
|
if (!$element->hasAttributeNS($namespaceUri, 'nil')) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 'true' === $element->getAttributeNS($namespaceUri, 'nil') || '1' === $element->getAttributeNS($namespaceUri, 'nil');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
12
src/Symfony/Component/Routing/Tests/Fixtures/null_values.xml
Normal file
12
src/Symfony/Component/Routing/Tests/Fixtures/null_values.xml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<routes xmlns="http://symfony.com/schema/routing"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
|
||||||
|
|
||||||
|
<route id="blog_show" path="/blog/{slug}">
|
||||||
|
<default key="foo" xsi:nil="true" />
|
||||||
|
<default key="bar" xsi:nil="1" />
|
||||||
|
<default key="foobar" xsi:nil="false">foo</default>
|
||||||
|
<default key="baz" xsi:nil="0">bar</default>
|
||||||
|
</route>
|
||||||
|
</routes>
|
@ -124,4 +124,18 @@ class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase
|
|||||||
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
|
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
|
||||||
$loader->load('withdoctype.xml');
|
$loader->load('withdoctype.xml');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testNullValues()
|
||||||
|
{
|
||||||
|
$loader = new XmlFileLoader(new FileLocator(array(__DIR__.'/../Fixtures')));
|
||||||
|
$routeCollection = $loader->load('null_values.xml');
|
||||||
|
$route = $routeCollection->get('blog_show');
|
||||||
|
|
||||||
|
$this->assertTrue($route->hasDefault('foo'));
|
||||||
|
$this->assertNull($route->getDefault('foo'));
|
||||||
|
$this->assertTrue($route->hasDefault('bar'));
|
||||||
|
$this->assertNull($route->getDefault('bar'));
|
||||||
|
$this->assertEquals('foo', $route->getDefault('foobar'));
|
||||||
|
$this->assertEquals('bar', $route->getDefault('baz'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user