Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ This code will result in:

### Adding comments

You can use a key named `_comment` to add a comment to a node. The exact placement depends on where you put the key in your array.`
You can use a key named `_comment` to add a comment to a node. The exact placement depends on where you put the key in your array. You can also add multiple keys *starting with* `_comment` to add multiple comments.

```php
$array = [
Expand All @@ -135,6 +135,13 @@ $array = [
'weapon' => 'Evil Eye',
'_comment' => 'Finally gone',
],
'Another guy' => [
'_comment' => 'The GOAT',
'name' => 'John Wick',
'_comment2' => 'famous for',
'weapon' => 'Pencil',
'_comment_other' => 'His dog needs an entry',
];
'The survivor' => [
'_attributes' => ['house'=>'Hogwarts'],
'_value' => 'Harry Potter',
Expand All @@ -160,6 +167,13 @@ This code will result in:
<weapon>Evil Eye</weapon>
<!--Finally gone-->
</Bad_guy>
<Another_guy>
<!--The GOAT-->
<name>John Wick</name>
<!--famous for-->
<weapon>Pencil</weapon>
<!--His dog needs an entry-->
</Another_guy>
<The_survivor house="Hogwarts">Harry Potter<!--He made it--></The_survivor>
</root>
```
Expand Down
2 changes: 1 addition & 1 deletion src/ArrayToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ protected function convertElement(DOMElement $element, mixed $value): void
$this->addNumericNode($element, $data);
} elseif (str_starts_with($key, '__custom:')) {
$this->addNode($element, str_replace('\:', ':', preg_split('/(?<!\\\):/', $key)[1]), $data);
} elseif (($key === '_comment') || ($key === '@comment')) {
} elseif (strpos($key, '_comment') === 0 || strpos($key, '@comment') === 0) {
$element->appendChild(new \DOMComment($data));
} else {
$this->addNode($element, $key, $data);
Expand Down
10 changes: 8 additions & 2 deletions tests/ArrayToXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,15 @@
assertMatchesXmlSnapshot(ArrayToXml::convert($withAttributes));
});

it('can add a comment to xml', function () {
it('can add comments to xml', function () {
$withAttributes = $this->testArray;
$withAttributes['Good guy']['_comment'] = 'Short list';
$withAttributes['Another guy'] = [
'_comment' => 'The GOAT',
'name' => 'John Wick',
'_comment2' => 'famous for',
'weapon' => 'Pencil',
'_comment_other' => 'His dog needs an entry',
];

assertMatchesXmlSnapshot(ArrayToXml::convert($withAttributes));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
<Good_guy>
<name>Luke Skywalker</name>
<weapon>Lightsaber</weapon>
<!--Short list-->
</Good_guy>
<Bad_guy>
<name>Sauron</name>
<weapon>Evil Eye</weapon>
</Bad_guy>
<Another_guy>
<!--The GOAT-->
<name>John Wick</name>
<!--famous for-->
<weapon>Pencil</weapon>
<!--His dog needs an entry-->
</Another_guy>
</root>