From bd294cabd17f0861b07d28592729de69f4d2995e Mon Sep 17 00:00:00 2001 From: Arnaud Becher Date: Thu, 29 Oct 2020 14:00:45 +0100 Subject: [PATCH 1/4] handle standalone as a parameter --- src/ArrayToXml.php | 13 ++++++++++--- tests/ArrayToXmlTest.php | 6 ++++++ ...lTest__it_accepts_an_xml_standalone_value__1.php | 3 +++ 3 files changed, 19 insertions(+), 3 deletions(-) create mode 100644 tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.php diff --git a/src/ArrayToXml.php b/src/ArrayToXml.php index 1be4cbf..616c242 100644 --- a/src/ArrayToXml.php +++ b/src/ArrayToXml.php @@ -23,10 +23,15 @@ public function __construct( $replaceSpacesByUnderScoresInKeyNames = true, $xmlEncoding = null, $xmlVersion = '1.0', - $domProperties = [] + $domProperties = [], + $xmlStandalone = null ) { $this->document = new DOMDocument($xmlVersion, $xmlEncoding); + if (! is_null($xmlStandalone)) { + $this->document->xmlStandalone = $xmlStandalone; + } + if (! empty($domProperties)) { $this->setDomProperties($domProperties); } @@ -55,7 +60,8 @@ public static function convert( bool $replaceSpacesByUnderScoresInKeyNames = true, string $xmlEncoding = null, string $xmlVersion = '1.0', - array $domProperties = [] + array $domProperties = [], + bool $xmlStandalone = null ) { $converter = new static( $array, @@ -63,7 +69,8 @@ public static function convert( $replaceSpacesByUnderScoresInKeyNames, $xmlEncoding, $xmlVersion, - $domProperties + $domProperties, + $xmlStandalone ); return $converter->toXml(); diff --git a/tests/ArrayToXmlTest.php b/tests/ArrayToXmlTest.php index a499c43..8402fa7 100644 --- a/tests/ArrayToXmlTest.php +++ b/tests/ArrayToXmlTest.php @@ -126,6 +126,12 @@ public function it_accepts_an_xml_version() $this->assertMatchesSnapshot(ArrayToXml::convert([], '', false, null, '1.1')); } + /** @test */ + public function it_accepts_an_xml_standalone_value() + { + $this->assertMatchesSnapshot(ArrayToXml::convert([], '', false, null, '1.0', [], false)); + } + /** @test */ public function it_can_handle_values_as_collection() { diff --git a/tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.php b/tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.php new file mode 100644 index 0000000..df1fe6a --- /dev/null +++ b/tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.php @@ -0,0 +1,3 @@ + + +'; From c6ddb8e871bb22154225048e7a4aad1703bddb92 Mon Sep 17 00:00:00 2001 From: Arnaud Becher Date: Thu, 29 Oct 2020 14:19:16 +0100 Subject: [PATCH 2/4] add xml declaration section to readme --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index dbd5fa7..f14a427 100755 --- a/README.md +++ b/README.md @@ -59,6 +59,7 @@ After running this piece of code `$result` will contain: ``` + ### Setting the name of the root element Optionally you can set the name of the rootElement by passing it as the second argument. If you don't specify @@ -159,6 +160,25 @@ This code will result in: If your input contains something that cannot be parsed a `DOMException` will be thrown. + +### Customize the XML declaration + +You could specify specific values in for: + - encoding as the fourth argument (string) + - version as the fifth argument (string) + - standalone as sixth argument (boolean) + +```php +$result = ArrayToXml::convert($array, [], true, 'UTF-8', '1.1', [], false); +``` + +This will result in: + +```xml + +``` + + ### Adding attributes to the root element To add attributes to the root element provide an array with an `_attributes` key as the second argument. From 36811b0d94581b2ad22ecd501f1a33d00bad663d Mon Sep 17 00:00:00 2001 From: Arnaud Becher Date: Thu, 29 Oct 2020 14:22:38 +0100 Subject: [PATCH 3/4] try to fix travis --- .../ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.php b/tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.php index df1fe6a..89a1f24 100644 --- a/tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.php +++ b/tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_standalone_value__1.php @@ -1,3 +1,5 @@ - + '; From 96626ed120970439f563b1d58f5ead1fec233eaa Mon Sep 17 00:00:00 2001 From: Arnaud Becher Date: Thu, 29 Oct 2020 14:23:58 +0100 Subject: [PATCH 4/4] fix typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f14a427..67f2c02 100755 --- a/README.md +++ b/README.md @@ -169,7 +169,7 @@ You could specify specific values in for: - standalone as sixth argument (boolean) ```php -$result = ArrayToXml::convert($array, [], true, 'UTF-8', '1.1', [], false); +$result = ArrayToXml::convert($array, [], true, 'UTF-8', '1.1', [], true); ``` This will result in: