From a36d2b63f8389af8bbbd321933bb47b19e90ab1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dimitri=20K=C3=B6nig?= Date: Wed, 26 Nov 2025 17:55:55 +0100 Subject: [PATCH] Extend comments feature for multiple comments --- README.md | 16 +++++++++++++++- src/ArrayToXml.php | 2 +- tests/ArrayToXmlTest.php | 10 ++++++++-- ...ToXmlTest__it_can_add_comments_to_xml__1.xml} | 8 +++++++- 4 files changed, 31 insertions(+), 5 deletions(-) rename tests/__snapshots__/{ArrayToXmlTest__it_can_add_a_comment_to_xml__1.xml => ArrayToXmlTest__it_can_add_comments_to_xml__1.xml} (55%) diff --git a/README.md b/README.md index 43aa08e..d5f3e7a 100755 --- a/README.md +++ b/README.md @@ -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 = [ @@ -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', @@ -160,6 +167,13 @@ This code will result in: Evil Eye + + + John Wick + + Pencil + + Harry Potter ``` diff --git a/src/ArrayToXml.php b/src/ArrayToXml.php index 5387ed9..4831d50 100644 --- a/src/ArrayToXml.php +++ b/src/ArrayToXml.php @@ -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('/(?appendChild(new \DOMComment($data)); } else { $this->addNode($element, $key, $data); diff --git a/tests/ArrayToXmlTest.php b/tests/ArrayToXmlTest.php index 473891e..8a4b869 100644 --- a/tests/ArrayToXmlTest.php +++ b/tests/ArrayToXmlTest.php @@ -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)); }); diff --git a/tests/__snapshots__/ArrayToXmlTest__it_can_add_a_comment_to_xml__1.xml b/tests/__snapshots__/ArrayToXmlTest__it_can_add_comments_to_xml__1.xml similarity index 55% rename from tests/__snapshots__/ArrayToXmlTest__it_can_add_a_comment_to_xml__1.xml rename to tests/__snapshots__/ArrayToXmlTest__it_can_add_comments_to_xml__1.xml index d48ab4b..050f840 100644 --- a/tests/__snapshots__/ArrayToXmlTest__it_can_add_a_comment_to_xml__1.xml +++ b/tests/__snapshots__/ArrayToXmlTest__it_can_add_comments_to_xml__1.xml @@ -3,10 +3,16 @@ Luke Skywalker Lightsaber - Sauron Evil Eye + + + John Wick + + Pencil + +