| Newsgroups |
php.pear.bugs |
| Message-ID |
<[email protected]> |
Edit report at https://pear.php.net/bugs/bug.php?id=21052&edit=1
ID: 21052
Comment by: [email protected]
Reported By: dickson dot michael at gmail dot com
Summary: Incompatible Attributes Options
Status: Open
Type: Bug
Package: XML_Serializer
Package Version: 0.20.2
PHP Version: Irrelevant
Roadmap Versions:
New Comment:
There's a connected issue regarding array serialization.
If we run:
<?php
$array = array(
array('data' => 'world'),
array('data' => 'hello'),
);
require_once 'XML/Serializer.php';
require_once 'XML/Unserializer.php';
$S = new XML_Serializer();
$U = new XML_Unserializer();
$S->serialize($array);
$U->unserialize($S->getSerializedData());
var_dump($U->getUnserializedData());
?>
We end up with:
array(1) {
["XML_Serializer_Tag"]=>
array(2) {
[0]=>
array(1) {
["data"]=>
string(5) "world"
}
[1]=>
array(1) {
["data"]=>
string(5) "hello"
}
}
}
To avoid this, we set $U->setOption('ignoreKeys',
array('XML_Serializer_Tag'));
The test then returns the original input, as expected:
array(2) {
[0]=>
array(1) {
["data"]=>
string(5) "world"
}
[1]=>
array(1) {
["data"]=>
string(5) "hello"
}
}
However, this fails when the test data contains non-numeric indices:
<?php
$array = array(
'first line' => array('data' => 'world'),
'second line' => array('data' => 'hello'),
);
?>
Output remains as though numeric indices were used.
Previous Comments:
------------------------------------------------------------------------
[2016-04-12 06:09:40] dicksonm
I found the above patch solves the problem by creating a $dataAttribs
which does not contain the internal attributes _originalKey, _type, or
_class.
------------------------------------------------------------------------
[2016-04-12 06:05:59] dicksonm
Added #patch
bug:21052;patch:attribute-segregation.patch;revision:1460455559;.
------------------------------------------------------------------------
[2016-04-12 05:59:42] dicksonm
Description:
------------
In Unserialize.php, when 'parseAttributes' is set as well as 'typeHints'
the
output data is corrupted by package's internal attributes.
Test script:
---------------
<?php
$string = <<<EOD
<array _type="array" Hello="world" Glad="I am">
<XML_Serializer_Tag _originalKey="To see"
_type="string">You</XML_Serializer_Tag>
<XML_Serializer_Tag _originalKey="I think" _type="string">We can be
friends</XML_Serializer_Tag>
</array>
EOD;
require_once 'XML/Unserializer.php';
$engine = new XML_Unserializer();
$engine->setOption('parseAttributes', true);
$engine->setOption('typeHints', TRUE);
$engine->unserialize($string);
var_dump($engine->getUnserializedData());
?>
Expected result:
----------------
array(4) {
["Hello"]=>
string(5) "world"
["Glad"]=>
string(4) "I am"
["To see"]=>
string(3) "You"
["I think"]=>
string(17) "We can be friends"
}
Actual result:
--------------
array(5) {
["_type"]=>
string(5) "array"
["Hello"]=>
string(5) "world"
["Glad"]=>
string(4) "I am"
[0]=>
array(3) {
["_originalKey"]=>
string(6) "To see"
["_type"]=>
string(6) "string"
["_content"]=>
string(3) "You"
}
[1]=>
array(3) {
["_originalKey"]=>
string(7) "I think"
["_type"]=>
string(6) "string"
["_content"]=>
string(17) "We can be friends"
}
}
------------------------------------------------------------------------
--
Edit this bug report at https://pear.php.net/bugs/bug.php?id=21052&edit=1