[Serializer] Allow for more flexible element names
This commit is contained in:
parent
8216a6ef3d
commit
f5f41696ec
|
@ -267,6 +267,8 @@ class XmlEncoder extends AbstractEncoder
|
||||||
*/
|
*/
|
||||||
protected function isElementNameValid($name)
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,6 +53,25 @@ class XmlEncoderTest extends \PHPUnit_Framework_TestCase
|
||||||
$this->assertEquals($expected, $this->encoder->encode($obj, 'xml'));
|
$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()
|
public function testEncodeSimpleXML()
|
||||||
{
|
{
|
||||||
$xml = simplexml_load_string('<firstname>Peter</firstname>');
|
$xml = simplexml_load_string('<firstname>Peter</firstname>');
|
||||||
|
|
Reference in New Issue