[Serializer] Allow for more flexible element names

This commit is contained in:
Jordi Boggiano 2011-02-13 16:14:43 +01:00 committed by Fabien Potencier
parent 8216a6ef3d
commit f5f41696ec
2 changed files with 22 additions and 1 deletions

View File

@ -267,6 +267,8 @@ class XmlEncoder extends AbstractEncoder
*/
protected function isElementNameValid($name)
{
return $name && strpos($name, ' ') === false && preg_match('|^\w+$|', $name);
return $name &&
false === strpos($name, ' ') &&
preg_match('#^[\pL_][\pL0-9._-]+$#ui', $name);
}
}

View File

@ -53,6 +53,25 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
}
public function testElementNameValid()
{
$obj = new ScalarDummy;
$obj->xmlFoo = array(
'foo-bar' => '',
'foo_bar' => '',
'föo_bär' => '',
);
$expected = '<?xml version="1.0"?>'."\n".
'<response>'.
'<foo-bar><![CDATA[]]></foo-bar>'.
'<foo_bar><![CDATA[]]></foo_bar>'.
'<föo_bär><![CDATA[]]></föo_bär>'.
'</response>'."\n";
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
}
public function testEncodeSimpleXML()
{
$xml = simplexml_load_string('<firstname>Peter</firstname>');