8000 add xml context option to ignore empty attributes · symfony/symfony@081e6df · GitHub
[go: up one dir, main page]

Skip to content

Commit 081e6df

Browse files
committed
add xml context option to ignore empty attributes
1 parent 789055e commit 081e6df

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ CHANGELOG
1313
default contexts, name converters, sets of normalizers and encoders
1414
* Add support for collection profiles of multiple serializer instances
1515
* Deprecate `AdvancedNameConverterInterface`, use `NameConverterInterface` instead
16+
* Add `XmlEncoder::IGNORE_EMPTY_ATTRIBUTES` context option to ignore empty attributes
1617

1718
7.1
1819
---

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
6060
public const VERSION = 'xml_version';
6161
public const CDATA_WRAPPING = 'cdata_wrapping';
6262
public const CDATA_WRAPPING_PATTERN = 'cdata_wrapping_pattern';
63+
public const IGNORE_EMPTY_ATTRIBUTES = 'ignore_empty_attributes';
6364

6465
private array $defaultContext = [
6566
self::AS_COLLECTION => false,
@@ -72,6 +73,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
7273
self::TYPE_CAST_ATTRIBUTES => true,
7374
self::CDATA_WRAPPING => true,
7475
self::CDATA_WRAPPING_PATTERN => '/[<>&]/',
76+
self::IGNORE_EMPTY_ATTRIBUTES => false,
7577
];
7678

7779
public function __construct(array $defaultContext = [])
@@ -355,6 +357,13 @@ private function buildXml(\DOMNode $parentNode, mixed $data, string $format, arr
355357
if (\is_bool($data)) {
356358
$data = (int) $data;
357359
}
360+
361+
if ($context[self::IGNORE_EMPTY_ATTRIBUTES] ?? $this->defaultContext[self::IGNORE_EMPTY_ATTRIBUTES]) {
362+
if (null === $data || '' === $data) {
363+
continue;
364+
}
365+
}
366+
358367
$parentNode->setAttribute($attributeName, $data);
359368
} elseif ('#' === $key) {
360369
$append = $this->selectNodeType($parentNode, $data, $format, $context);

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1004,4 +1004,17 @@ private function createXmlWithDateTimeField(): string
10041004
<response><foo dateTime="%s"/></response>
10051005
', $this->exampleDateTimeString);
10061006
}
1007+
1008+
public function testEncodeIgnoringEmptyAttribute()
1009+
{
1010+
$expected = <<<'XML'
1011+
<?xml version="1.0"?>
1012+
<response>Test</response>
1013+
1014+
XML;
1015+
1016+
$data = ['#' => 'Test', '@attribute' => '', '@attribute2' => null];
1017+
1018+
$this->assertEquals($expected, $this->encoder->encode($data, 'xml', ['ignore_empty_attributes' => true]));
1019+
}
10071020
}

0 commit comments

Comments
 (0)
0