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

Skip to content

Commit 63de18f

Browse files
committed
Add context variable to enable/disable this
1 parent f41060f commit 63de18f

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ CHANGELOG
33

44
5.1.0
55
-----
6-
* Cast to PHP type numeric value from node for XML
6+
* Cast to PHP type numeric value from node for XML if `XmlEncoder::TYPE_CAST_NODES` set to true into context
77

88
5.0.0
99
-----

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
5252
const ROOT_NODE_NAME = 'xml_root_node_name';
5353
const STANDALONE = 'xml_standalone';
5454
const TYPE_CAST_ATTRIBUTES = 'xml_type_cast_attributes';
55+
const TYPE_CAST_NODES = 'xml_type_cast_nodes';
5556
const VERSION = 'xml_version';
5657

5758
private $defaultContext = [
@@ -62,6 +63,7 @@ class XmlEncoder implements EncoderInterface, DecoderInterface, NormalizationAwa
6263
self::REMOVE_EMPTY_TAGS => false,
6364
self::ROOT_NODE_NAME => 'response',
6465
self::TYPE_CAST_ATTRIBUTES => true,
66+
self::TYPE_CAST_NODES => true,
6567
];
6668

6769
/**
@@ -325,16 +327,18 @@ private function parseXmlValue(\DOMNode $node, array $context = [])
325327
return $node->nodeValue;
326328
}
327329

330+
$typeCastNodes = (bool) ($context[self::TYPE_CAST_NODES] ?? $this->defaultContext[self::TYPE_CAST_NODES]);
331+
328332
if (1 === $node->childNodes->length && \in_array($node->firstChild->nodeType, [XML_TEXT_NODE, XML_CDATA_SECTION_NODE])) {
329-
if (false !== $val = filter_var($node->firstChild->nodeValue, FILTER_VALIDATE_INT)) {
330-
return $val;
333+
if (!is_numeric($node->firstChild->nodeValue) || !$typeCastNodes) {
334+
return $node->firstChild->nodeValue;
331335
}
332336

333-
if (false !== $val = filter_var($node->firstChild->nodeValue, FILTER_VALIDATE_FLOAT)) {
337+
if (false !== $val = filter_var($node->firstChild->nodeValue, FILTER_VALIDATE_INT)) {
334338
return $val;
335339
}
336340

337-
return $node->firstChild->nodeValue;
341+
return (float) $node->firstChild->nodeValue;
338342
}
339343

340344
$value = [];

0 commit comments

Comments
 (0)