8000 XML Encoder Optional Type Cast · symfony/symfony@7c59299 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7c59299

Browse files
committed
XML Encoder Optional Type Cast
- This provides the ability to forgo the attribute type casting - Updated the CHANGELOG Signed-off-by: RJ Garcia <rj@bighead.net>
1 parent 0c17767 commit 7c59299

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

src/Symfony/Component/Serializer/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ CHANGELOG
1717
* [DEPRECATION] the `Exception` interface has been renamed to `ExceptionInterface`
1818
* added `ObjectNormalizer` leveraging the `PropertyAccess` component to normalize
1919
objects containing both properties and getters / setters / issers / hassers methods.
20+
* added `xml_type_cast_attributes` context option for allowing users to opt-out of typecasting
21+
xml attributes.
2022

2123
2.6.0
2224
-----

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,10 @@ public function decode($data, $format, array $context = array())
114114
unset($data['@xmlns:xml']);
115115

116116
if (empty($data)) {
117-
return $this->parseXml($rootNode);
117+
return $this->parseXml($rootNode, $context);
118118
}
119119

120-
return array_merge($data, (array) $this->parseXml($rootNode));
120+
return array_merge($data, (array) $this->parseXml($rootNode, $context));
121121
}
122122

123123
if (!$rootNode->hasAttributes()) {
@@ -256,11 +256,11 @@ final protected function isElementNameValid($name)
256256
*
257257
* @return array|string
258258
*/
259-
private function parseXml(\DOMNode $node)
259+
private function parseXml(\DOMNode $node, array $context = array())
260260
{
261-
$data = $this->parseXmlAttributes($node);
261+
$data = $this->parseXmlAttributes($node, $context);
262262

263-
$value = $this->parseXmlValue($node);
263+
$value = $this->parseXmlValue($node, $context);
264264

265265
if (!count($data)) {
266266
return $value;
@@ -292,16 +292,17 @@ private function parseXml(\DOMNode $node)
292292
*
293293
* @return array
294294
*/
295-
private function parseXmlAttributes(\DOMNode $node)
295+
private function parseXmlAttributes(\DOMNode $node, array $context = array())
296296
{
297297
if (!$node->hasAttributes()) {
298298
return array();
299299
}
300300

301301
$data = array();
302+
$typeCastAttributes = $this->resolveXmlTypeCastAttributes($context);
302303

303304
foreach ($node->attributes as $attr) {
304-
if (!is_numeric($attr->nodeValue)) {
305+
if (!is_numeric($attr->nodeValue) || !$typeCastAttributes) {
305306
$data['@'.$attr->nodeName] = $attr->nodeValue;
306307

307308
continue;
@@ -326,7 +327,7 @@ private function parseXmlAttributes(\DOMNode $node)
326327
*
327328
* @return array|string
328329
*/
329-
private function parseXmlValue(\DOMNode $node)
330+
private function parseXmlValue(\DOMNode $node, array $context = array())
330331
{
331332
if (!$node->hasChildNodes()) {
332333
return $node->nodeValue;
@@ -343,7 +344,7 @@ private function parseXmlValue(\DOMNode $node)
343344
continue;
344345
}
345346

346-
$val = $this->parseXml($subnode);
347+
$val = $this->parseXml($subnode, $context);
347348

348349
if ('item' === $subnode->nodeName && isset($val['@key'])) {
349350
if (isset($val['#'])) {
@@ -522,6 +523,20 @@ private function resolveXmlRootName(array $context = array())
522523
: $this->rootNodeName;
523524
}
524525

526+
/**
527+
* Get XML option for type casting attributes Defaults to true.
528+
*
529+
* @param array $context
530+
*
531+
* @return bool
532+
*/
533+
private function resolveXmlTypeCastAttributes(array $context = array())
534+
{
535+
return isset($context['xml_type_cast_attributes'])
536+
? (bool) $context['xml_type_cast_attributes']
537+
: true;
538+
}
539+
525540
/**
526541
* Create a DOM document, taking serializer options into account.
527542
*

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,28 @@ public function testDecodeNegativeFloatAttribute()
262262
$this->assertSame(array('@index' => -12.11, '#' => 'Name'), $this->encoder->decode($source, 'xml'));
263263
}
264264

265+
public function testNoTypeCastAttribute()
266+
{
267+
$source = <<<XML
268+
<?xml version="1.0"?>
269+
<document a="018" b="-12.11">
270+
<node a="018" b="-12.11"/>
271+
</document>
272+
XML;
273+
274+
$data = $this->encoder->decode($source, 'xml', array('xml_type_cast_attributes' => false));
275+
$expected = array(
276+
'@a' => '018',
277+
'@b' => '-12.11',
278+
'node' => [
279+
'@a' => '018',
280+
'@b' => '-12.11',
281+
'#' => '',
282+
]
283+
);
284+
$this->assertSame($expected, $data);
285+
}
286+
265287
public function testEncode()
266288
{
267289
$source = $this->getXmlSource();

0 commit comments

Comments
 (0)
0