Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 0.21.0

Request #14112 #default is ignored for scalarAsAttributes
Submitted: 2008-06-09 20:06 UTC
From: protricity Assigned:
Status: Analyzed Package: XML_Serializer (version 0.18.0)
PHP Version: 5.2.6 OS: Windows
Roadmaps: 0.21.0    
Subscription  


 [2008-06-09 20:06 UTC] protricity (Ari Asulin)
Description: ------------ #default is ignored for scalarAsAttributes on line 764 for Serializer.php. Documentation says #default is considered whenever an array is given for tags. Heres a patch: $scalarAsAttributes = false; if (is_array($this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES])) { $scalarAsAttributes = isset($this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES][$tagName])? $this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES][$tagName]: $this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES]['#default']; } elseif ($this->options[XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES] === true) { $scalarAsAttributes = true; } Test script: --------------- $serializer = &new XML_Serializer(array( XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES => array('#default'=>true,'tag3'=>false), )); echo serializer->serialize(array('tag'=>array('tag2'=>2,'tag3'=>'hi'))); Expected result: ---------------- #default is ignored. Actual result: -------------- #default is used to all missing tags.

Comments

 [2008-09-17 18:27 UTC] ashnazg (Chuck Burgess)
I'm not following the logic here... I see nothing in either the code or the documentation indicating that "#default" has anything to do with the SCALAR_AS_ATTRIBUTES usage. I only ever see it referenced with the XML_SERIALIZER_OPTION_DEFAULT_TAG option. I constructed test file bug-14112.phpt to test the various proper usages of the SCALAR_AS_ATTRIBUTES tag, will add it to the package's test suite once I'm finishing writing several test cases showing the usages.
 [2008-09-17 18:45 UTC] ashnazg (Chuck Burgess)
Added testcase show_ScalarAsAttributes_usage.phpt to show various usages of the scalar option... committed to CVS.
 [2008-09-17 18:47 UTC] ashnazg (Chuck Burgess)
Ari, Can you review your usage expectation here to see if I'm correct about it being misunderstood, or else clarify what documentation is telling you that your usage of SCALAR_AS_ATTRIBUTES is allowed? Thanks...
 [2008-09-17 19:06 UTC] protricity (Ari Asulin)
Basically, there is functionality in naming default tags that looks for the hash option '#default'. You can see the code on line 889 of Serializer.php. Also for XML_UNSERIALIZER_OPTION_ATTRIBUTE_KEY on line 1169 of Unserializer.php Frankly I can't remember where in the docs I found '#default' described, or if its even in the docs. It might be an undocumented feature. So as I see it, if this is applied for XML_UNSERIALIZER_OPTION_ATTRIBUTE_KEY and default tags, that it should work for XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES as well. If its not actually part of the specs, I respectfully ask that it be considered, since in the application I'm developing absolutely must have a way of setting default values for unspecified tags.
 [2008-09-18 20:27 UTC] ashnazg (Chuck Burgess)
Ari, Does this test case and resulting output match the usage you're looking for? /** * usage of '#default'... * * subtag tag3 will explicitly become an attribute on tagA... * * ANY occurrences of tag5 anywhere will become an attribute * of its parent element, which below is only tagB. */ $ser->setOption( XML_SERIALIZER_OPTION_SCALAR_AS_ATTRIBUTES, array( 'tagA' => array('tag3'), '#default' => array('tag5') ) ); $result = $ser->serialize( array( 'tagA' => array( 'tag2' => 2, 'tag3' => 'hi' ), 'tagB' => array( 'tag4' => 4, 'tag5' => 'bye' ) ) ); echo $ser->getSerializedData() . PHP_EOL . PHP_EOL; <array> <tagA tag3="hi"> <tag2>2</tag2> </tagA> <tagB tag5="bye"> <tag4>4</tag4> </tagB> </array>
 [2009-06-01 23:36 UTC] ashnazg (Chuck Burgess)
-Status: Assigned +Status: Analyzed
 [2009-06-12 08:15 UTC] ashnazg (Chuck Burgess)
-Roadmap Versions: 0.20.0 +Roadmap Versions: 0.21.0