Making Xml::fromArray() parse @ in attibutes keys.

This commit is contained in:
Juan Basso 2010-08-23 00:48:34 -03:00
parent 9611ab1027
commit f777bd983f
2 changed files with 46 additions and 1 deletions

View file

@ -85,12 +85,18 @@ class Xml {
} elseif ($value === null) {
$value = '';
}
if ($format === 'tags') {
if ($key[0] !== '@' && $format === 'tags') {
$node->addChild($key, $value);
} else {
if ($key[0] === '@') {
$key = substr($key, 1);
}
$node->addAttribute($key, $value);
}
} else {
if ($key[0] === '@') {
throw new Exception(__('Invalid array'));
}
if (array_keys($value) === range(0, count($value) - 1)) { // List
foreach ($value as $item) {
$child = $node->addChild($key);

View file

@ -157,6 +157,24 @@ class XmlTest extends CakeTestCase {
$this->assertIdentical((string)$obj->string, 'ok');
$this->assertIdentical((string)$obj->null, '');
$this->assertIdentical((string)$obj->array, '');
$xml = array(
'tags' => array(
'tag' => array(
array(
'@id' => '1',
'name' => 'defect'
),
array(
'@id' => '2',
'name' => 'enhancement'
)
)
)
);
$obj = Xml::fromArray($xml, 'tags');
$xmlText = '<' . '?xml version="1.0"?><tags><tag id="1"><name>defect</name></tag><tag id="2"><name>enhancement</name></tag></tags>';
$this->assertEqual(str_replace(array("\r", "\n"), '', $obj->asXML()), $xmlText);
}
/**
@ -219,6 +237,26 @@ class XmlTest extends CakeTestCase {
$this->assertEqual($e->getMessage(), __('Invalid array'));
}
try {
$xml = array(
'tags' => array(
'@tag' => array(
array(
'@id' => '1',
'name' => 'defect'
),
array(
'@id' => '2',
'name' => 'enhancement'
)
)
)
);
$obj = Xml::fromArray($xml);
$this->fail('No exception thrown');
} catch (Exception $e) {
$this->assertEqual($e->getMessage(), __('Invalid array'));
}
}
/**
@ -302,6 +340,7 @@ class XmlTest extends CakeTestCase {
)
);
$this->assertEqual(Xml::toArray(Xml::fromArray($array)), $expected);
$this->assertEqual(Xml::toArray(Xml::fromArray($array, 'tags')), $array);
$array = array(
'tags' => array(