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

Bug #9799 attributesArray is poorly named
Submitted: 2007-01-10 23:24 UTC
From: ljou1 at yahoo dot com Assigned: ashnazg
Status: Bogus Package: XML_Serializer (version 0.18.0)
PHP Version: 5.1.2 OS: windows xp
Roadmaps: (Not assigned)    
Subscription  


 [2007-01-10 23:24 UTC] ljou1 at yahoo dot com (Lianghwa Jou)
Description: ------------ When attributesArray is set to an arry, it cuases php to generate warning. Warning: Illegal offset type in isset or empty in C:\oscommerce\xampp\php\pear\X ML\Serializer.php on line 713 Test script: --------------- <?php require_once 'XML/Serializer.php'; $color = array('f'=>array(array('id'=>'blue','-'=>'red'),array('id'=>'qqq','-'=>'green'))); $options = array( 'addDecl' => true, 'rootName'=>'truecolor', 'indent'=>' ', 'mode'=>'simplexml', 'scalarAsAttributes'=>array('f'=>array('id')), 'attributesArray'=>array('-'), 'contentName'=>'-' ); $s = new XML_Serializer($options); $status = $s->serialize($color); echo $s->getSerializedData(); ?> Expected result: ---------------- The results is connect expect the warning messages. Actual result: -------------- Warning: Illegal offset type in isset or empty in C:\oscommerce\xampp\php\pear\X ML\Serializer.php on line 713 Warning: Illegal offset type in isset or empty in C:\oscommerce\xampp\php\pear\X ML\Serializer.php on line 713 Warning: Illegal offset type in isset or empty in C:\oscommerce\xampp\php\pear\X ML\Serializer.php on line 713 Warning: Illegal offset type in isset or empty in C:\oscommerce\xampp\php\pear\X ML\Serializer.php on line 713 <?xml version="1.0"?> <truecolor> <f id="blue">red</f> <f id="qqq">green</f> </truecolor>

Comments

 [2008-02-03 17:20 UTC] tvlgiao (Giao Trinh)
this patch below works fine for me: FIND: if ($this->options[XML_SERIALIZER_OPTION_ATTRIBUTES_KEY] !== null) { INSERT AFTER: // Giao: fix bug XML_SERIALIZER_OPTION_ATTRIBUTES_KEY is array {{{ if (is_array($this->options[XML_SERIALIZER_OPTION_ATTRIBUTES_KEY])) { foreach ($this->options[XML_SERIALIZER_OPTION_ATTRIBUTES_KEY] as $k) if (isset($array[$k])) { $attributes[$k] = $array[$k]; unset($array[$k]); } } else // }}} - Giao
 [2008-03-06 23:35 UTC] sharoncorrell (Sharon Correll)
That patch works, but the attributesArray option should NOT include the item named in contentName. (If it does, it gets nabbed as an attribute and never gets put in the contents.) The options should look something like: $options = array( 'addDecl' => true, 'rootName' => 'truecolor', 'indent' => ' ', 'mode' => 'simplexml', 'scalarAsAttributes' => array('f'=>array('id')), 'attributesArray' => array(), 'contentName' => '-' ); Could someone confirm that this is indeed the intended behavior of these options?
 [2008-03-07 16:24 UTC] sharoncorrell (Sharon Correll)
Also this works: $options = array( 'addDecl' => true, 'rootName' => 'truecolor', 'indent' => ' ', 'mode' => 'simplexml', 'attributesArray' => array('id'), 'contentName' => '-' ); (omitting scalarAsAttributes altogether).
 [2008-03-22 09:15 UTC] doconnor (Daniel O'Connor)
mmm. attributesArray is just poorly named; it should be attributesArrayKeyName There's a constant: XML_SERIALIZER_OPTION_ATTRIBUTES_KEY which is involved too. The code does: if (isset($user_array[$attributesArray])) { } ... so when you give it an array, rather than a key, it gets cranky. Going to suggest renaming it to AttributesArrayKey or similar
 [2008-08-20 07:24 UTC] peterk (Peter Körner)
the way it seems it was intended, is this: $in = array( 'attributelist' => array('key' => 'value'), array( 'version' => 1, 'title' => 'video' ), array( 'version' => 1, 'title' => 'event' ) ); $serializer = new XML_Serializer(array( XML_SERIALIZER_OPTION_ROOT_NAME => 'types', XML_SERIALIZER_OPTION_DEFAULT_TAG => 'type', XML_SERIALIZER_OPTION_ATTRIBUTES_KEY => 'attributelist', XML_SERIALIZER_OPTION_CONTENT_KEY => 'title' )); which results in <types key="value"> <type> <version>1</version> </type> <type> <version>1</version> </type> </types> with the appended patch it could be used like this: $in = array( array( 'version' => 1, 'title' => 'video' ), array( 'version' => 1, 'title' => 'event' ) ); $serializer = new XML_Serializer(array( XML_SERIALIZER_OPTION_ROOT_NAME => 'types', XML_SERIALIZER_OPTION_DEFAULT_TAG => 'type', XML_SERIALIZER_OPTION_ATTRIBUTES_KEY => array('version'), XML_SERIALIZER_OPTION_CONTENT_KEY => 'title' )); which leads to <types> <type version="1">video</type> <type version="1">event</type> </types> this is, in my eyes, the behaviour i expected. Maybe the to strategies should be differenced by creating to seperate options.
 [2008-09-17 21:26 UTC] ashnazg (Chuck Burgess)
Judging from the API doc, only a string is allowed in the ATTRIBUTES_KEY option...
 [2008-09-18 01:40 UTC] ashnazg (Chuck Burgess)
Also, I concur with Sharon's comment from 3/6 that the attributeArray value musn't be the same as the contentName value. Once I set attributeArray as only a string (chose to use '+'), as a string that does not collide with contentName (left it as '-'), now the result comes out as expected: <?xml version="1.0"?> <truecolor> <f id="blue">red</f> <f id="qqq">green</f> </truecolor> with no warnings.
 [2008-09-18 01:42 UTC] ashnazg (Chuck Burgess)
Are there any disagreements on this determination that the correct usage of attributeArray and contentName require: - both must be strings - value of each must differ from the value of the other ?
 [2008-10-21 18:16 UTC] ashnazg (Chuck Burgess)
No feedback given disagreeing with my assessments from September.