Skip to content

Commit

Permalink
Changing to Xml::toArray() return @ after attributes to dont conflict…
Browse files Browse the repository at this point in the history
… with tags with same name.
  • Loading branch information
jrbasso committed Aug 23, 2010
1 parent 527446a commit 9611ab1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cake/libs/xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ protected static function _toArray($xml, &$parentData, $namespaces) {

foreach ($namespaces as $namespace) {
foreach ($xml->attributes($namespace, true) as $key => $value) {
$data[$key] = (string)$value;
$data['@' . $key] = (string)$value;
}

foreach ($xml->children($namespace, true) as $child) {
Expand Down
56 changes: 51 additions & 5 deletions cake/tests/cases/libs/xml.test.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,22 @@ function testToArray() {
$xml = TEST_CAKE_CORE_INCLUDE_PATH . 'tests' . DS . 'fixtures' . DS . 'sample.xml';
$obj = Xml::build($xml);
$expected = array(
'tags' => array(
'tag' => array(
array(
'@id' => '1',
'name' => 'defect'
),
array(
'@id' => '2',
'name' => 'enhancement'
)
)
)
);
$this->assertEqual(Xml::toArray($obj), $expected);

$array = array(
'tags' => array(
'tag' => array(
array(
Expand All @@ -269,11 +285,25 @@ function testToArray() {
)
)
);
$this->assertEqual(Xml::toArray($obj), $expected);
$this->assertEqual(Xml::toArray(Xml::fromArray($expected)), $expected);
$this->assertEqual(Xml::toArray(Xml::fromArray($expected, 'tags')), $expected);
$this->assertEqual(Xml::toArray(Xml::fromArray($array, 'tags')), $array);

$expected = array(
'tags' => array(
'tag' => array(
array(
'@id' => '1',
'@name' => 'defect'
),
array(
'@id' => '2',
'@name' => 'enhancement'
)
)
)
);
$this->assertEqual(Xml::toArray(Xml::fromArray($array)), $expected);

$array = array(
'tags' => array(
'tag' => array(
'id' => '1',
Expand All @@ -289,7 +319,23 @@ function testToArray() {
)
)
);
$this->assertEqual(Xml::toArray(Xml::fromArray($expected)), $expected);
$expected = array(
'tags' => array(
'tag' => array(
'@id' => '1',
'posts' => array(
array('@id' => '1'),
array('@id' => '2')
)
),
'tagOther' => array(
'subtag' => array(
'@id' => '1'
)
)
)
);
$this->assertEqual(Xml::toArray(Xml::fromArray($array)), $expected);

$xml = '<root>';
$xml .= '<tag id="1">defect</tag>';
Expand All @@ -299,7 +345,7 @@ function testToArray() {
$expected = array(
'root' => array(
'tag' => array(
'id' => 1,
'@id' => 1,
'value' => 'defect'
)
)
Expand Down

0 comments on commit 9611ab1

Please sign in to comment.