Skip to content

Commit

Permalink
clean up attributes feature
Browse files Browse the repository at this point in the history
  • Loading branch information
freekmurze committed Feb 12, 2016
1 parent 4d3d28e commit b192a65
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 31 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All Notable changes to `array-to-xml` will be documented in this file

## 2.1.0 - 2015-10-08
- Add ability to use attributes

## 2.0.0 - 2015-10-08
- Add support to collection arrays and dynamically XML convertion when keys are numeric

Expand Down
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,40 @@ this behaviour you can set the third argument to false. We'll leave all keynames
$result = ArrayToXml::convert($array, 'customrootname', false);
```

You can use a key named `_attributes` to add attributes to a node.

```php
$array = [
'Good guy' => [
'_attributes' => ['attr1' => 'value']
'name' => 'Luke Skywalker',
'weapon' => 'Lightsaber'
],
'Bad guy' => [
'name' => 'Sauron',
'weapon' => 'Evil Eye'
]
];

$result = ArrayToXml::convert($array);
```

This code will result in:

```xml
<?xml version="1.0"?>
<root>
<Good_guy attr1="value">
<name>Luke Skywalker</name>
<weapon>Lightsaber</weapon>
</Good_guy>
<Bad_guy>
<name>Sauron</name>
<weapon>Evil Eye</weapon>
</Bad_guy>
</root>
```

If your input contains something that cannot be parsed a `DOMException` will be thrown.

## Testing
Expand Down
26 changes: 13 additions & 13 deletions src/ArrayToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public function __construct(array $array, $rootElementName = '', $replaceSpacesB
$this->document = new DOMDocument();
$this->replaceSpacesByUnderScoresInKeyNames = $replaceSpacesByUnderScoresInKeyNames;

if ($this->isArrayAllKeySequential($array) && ! empty($array)) {
throw new DOMException("Invalid Character Error");
if ($this->isArrayAllKeySequential($array) && !empty($array)) {
throw new DOMException('Invalid Character Error');
}

$root = $this->document->createElement($rootElementName == '' ? 'root' : $rootElementName);
Expand Down Expand Up @@ -83,16 +83,16 @@ private function convertElement(DOMElement $element, $value)
{
$sequential = $this->isArrayAllKeySequential($value);

if (! is_array($value)) {
if (!is_array($value)) {
$element->nodeValue = htmlspecialchars($value);

return;
}

foreach ($value as $key => $data) {
if (! $sequential) {
if ($key ==='_attributes') {
$this->addAttribtues($element, $data);
if (!$sequential) {
if ($key === '_attributes') {
$this->addAttributes($element, $data);
} elseif ($key === '_value' && is_string($data)) {
$element->nodeValue = $data;
} else {
Expand Down Expand Up @@ -175,24 +175,24 @@ protected function addSequentualNode(DOMElement $element, $value)
*/
protected function isArrayAllKeySequential($value)
{
if (! is_array($value)) {
if (!is_array($value)) {
return false;
}

if (count($value) <= 0) {
return true;
}

return array_unique(array_map("is_int", array_keys($value))) === array(true);
return array_unique(array_map('is_int', array_keys($value))) === array(true);
}

/**
* Add attributes
* Add attributes.
*
* @param \DOMElement $element
* @param string[] $data
* @param \DOMElement $element
* @param string[] $data
*/
protected function addAttribtues($element, $data)
protected function addAttributes($element, $data)
{
foreach ($data as $attrKey => $attrVal) {
$element->setAttribute($attrKey, $attrVal);
Expand Down
21 changes: 3 additions & 18 deletions tests/ArrayToXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,33 +194,18 @@ public function it_can_group_by_values_when_values_are_in_a_numeric_array()

$this->assertEquals($expectedXml, $result);
}
/**
* @test
*/
public function it_support_now_attribtues_to_xml()
{

$expectedXml = '<?xml version="1.0"?>
<root><Good_guy nameType="1"><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name>Sauron</name><weapon>Evil Eye</weapon></Bad_guy></root>'.PHP_EOL;
$withAttributes = $this->testArray;
$withAttributes['Good guy']['_attributes']= ['nameType' => 1];
$result = ArrayToXml::convert($withAttributes);

$this->assertEquals($expectedXml, $result);
}

/**
* @test
*/
public function it_support_now_attribtues_to_xml_with_value()
public function it_can_convert_attributes_to_xml()
{

$expectedXml = '<?xml version="1.0"?>
<root><Good_guy nameType="1"><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name>Sauron</name><weapon>Evil Eye</weapon></Bad_guy></root>'.PHP_EOL;
$withAttributes = $this->testArray;
$withAttributes['Good guy']['_attributes']= ['nameType' => 1];
$withAttributes['Good guy']['_attributes'] = ['nameType' => 1];
$result = ArrayToXml::convert($withAttributes);

$this->assertEquals($expectedXml, $result);
}
}
}

0 comments on commit b192a65

Please sign in to comment.