Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0cb4d8e

Browse files
amoirauddunglas
authored andcommitted
[Serializer][XmlEncoder] Allow removing empty tags in generated XML
1 parent62533f3 commit0cb4d8e

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

‎src/Symfony/Component/Serializer/Encoder/XmlEncoder.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
392392
}
393393
}elseif (is_numeric($key) || !$this->isElementNameValid($key)) {
394394
$append =$this->appendNode($parentNode,$data,'item',$key);
395-
}else {
395+
}elseif (null !==$data || !isset($this->context['remove_empty_tags']) ||false ===$this->context['remove_empty_tags']) {
396396
$append =$this->appendNode($parentNode,$data,$key);
397397
}
398398
}

‎src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,28 @@ public function testEncodeXmlAttributes()
138138
$this->assertSame($expected,$this->encoder->encode($array,'xml',$context));
139139
}
140140

141+
publicfunctiontestEncodeRemovingEmptyTags()
142+
{
143+
$array =array('person' =>array('firstname' =>'Peter','lastname' =>null));
144+
145+
$expected ='<?xml version="1.0"?>'."\n".
146+
'<response><person><firstname>Peter</firstname></person></response>'."\n";
147+
148+
$context =array('remove_empty_tags' =>true);
149+
150+
$this->assertSame($expected,$this->encoder->encode($array,'xml',$context));
151+
}
152+
153+
publicfunctiontestEncodeNotRemovingEmptyTags()
154+
{
155+
$array =array('person' =>array('firstname' =>'Peter','lastname' =>null));
156+
157+
$expected ='<?xml version="1.0"?>'."\n".
158+
'<response><person><firstname>Peter</firstname><lastname/></person></response>'."\n";
159+
160+
$this->assertSame($expected,$this->encoder->encode($array,'xml'));
161+
}
162+
141163
publicfunctiontestContext()
142164
{
143165
$array =array('person' =>array('name' =>'George Abitbol'));

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp