Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4644ee9

Browse files
committed
feature #20524 [Serializer][XmlEncoder] Allow removing empty tags in generated XML (amoiraud)
This PR was squashed before being merged into the 3.3-dev branch (closes #20524). Discussion ---------- [Serializer][XmlEncoder] Allow removing empty tags in generated XML | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20398 | License | MIT | Doc PR | ~ Allow a new option in $context of XmlEncoder.php to remove empty tags if $context['remove_empty_tags'] setted to true, changing this : ```xml <node> <subnode>Value</subnode> <emptysubnode/> </node> ``` To this : ```xml <node> <subnode>Value</subnode> </node> ``` Commits ------- 0cb4d8e [Serializer][XmlEncoder] Allow removing empty tags in generated XML
2 parents 24d8135 + 0cb4d8e commit 4644ee9

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+
public function testEncodeRemovingEmptyTags()
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+
public function testEncodeNotRemovingEmptyTags()
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
public function testContext()
142164
{
143165
$array = array('person' => array('name' => 'George Abitbol'));

0 commit comments

Comments
 (0)