8000 Report Xml warning · symfony/symfony@007f56d · GitHub
[go: up one dir, main page]

Skip to content

Commit 007f56d

Browse files
Report Xml warning
1 parent 31b6a56 commit 007f56d

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ public function encode(mixed $data, string $format, array $context = []): string
8282
$encoderIgnoredNodeTypes = $context[self::ENCODER_IGNORED_NODE_TYPES] ?? $this->defaultContext[self::ENCODER_IGNORED_NODE_TYPES];
8383
$ignorePiNode = \in_array(\XML_PI_NODE, $encoderIgnoredNodeTypes, true);
8484
if ($data instanceof \DOMDocument) {
85-
return $data->saveXML($ignorePiNode ? $data->documentElement : null);
85+
set_error_handler(static function ($type, $message) { throw new NotEncodableValueException($message); }, \E_ERROR | \E_WARNING);
86+
try {
87+
return $data->saveXML($ignorePiNode ? $data->documentElement : null);
88+
} finally {
89+
restore_error_handler();
90+
}
8691
}
8792

8893
$xmlRootNodeName = $context[self::ROOT_NODE_NAME] ?? $this->defaultContext[self::ROOT_NODE_NAME];
@@ -97,7 +102,12 @@ public function encode(mixed $data, string $format, array $context = []): string
97102
$this->appendNode($dom, $data, $format, $context, $xmlRootNodeName);
98103
}
99104

100-
return $dom->saveXML($ignorePiNode ? $dom->documentElement : null, $context[self::SAVE_OPTIONS] ?? $this->defaultContext[self::SAVE_OPTIONS]);
105+
set_error_handler(static function ($type, $message) { throw new NotEncodableValueException($message); }, \E_ERROR | \E_WARNING);
106+
try {
107+
return $dom->saveXML($ignorePiNode ? $dom->documentElement : null, $context[self::SAVE_OPTIONS] ?? $this->defaultContext[self::SAVE_OPTIONS]);
108+
} finally {
109+
restore_error_handler();
110+
}
101111
}
102112

103113
public function decode(string $data, string $format, array $context = []): mixed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,12 @@ public function testEncodeTraversableWhenNormalizable()
433433
$this->assertEquals($expected, $serializer->serialize(new NormalizableTraversableDummy(), 'xml'));
434434
}
435435

436+
public function testEncodeException()
437+
{
438+
$this->expectException(NotEncodableValueException::class);
439+
$this->encoder->encode('Invalid character: '.\chr(7), 'xml');
440+
}
441+
436442
public function testDecode()
437443
{
438444
$source = $this->getXmlSource();

0 commit comments

Comments
 (0)
0