diff --git a/.gitignore b/.gitignore index 98b4a53..01fd23e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .idea +.vscode *.cache composer.lock coverage.clover diff --git a/src/Support/ArrayToXml.php b/src/Support/ArrayToXml.php index 4dd7b87..3b5b8c6 100644 --- a/src/Support/ArrayToXml.php +++ b/src/Support/ArrayToXml.php @@ -33,6 +33,7 @@ class ArrayToXml * @param string $elementCase * @param string $xmlVersion * @param string $xmlEncoding + * @param bool $xmlStandalone * * @throws \DOMException */ @@ -41,10 +42,15 @@ public function __construct( $rootElement = 'root', $elementCase = 'snake', $xmlVersion = '1.0', - $xmlEncoding = 'UTF-8' + $xmlEncoding = 'UTF-8', + $xmlStandalone = false ) { $this->domDocument = new DOMDocument($xmlVersion, $xmlEncoding); + if ($xmlStandalone) { + $this->domDocument->xmlStandalone = true; + } + $this->elementCase = $elementCase; if ($this->isArrayAllKeySequential($content) && ! empty($content)) { @@ -66,6 +72,7 @@ public function __construct( * @param string $elementCase * @param string $xmlVersion * @param string $xmlEncoding + * @param bool $xmlStandalone * * @return string */ @@ -74,9 +81,10 @@ public static function convert( $rootElementName = 'root', $elementCase = 'snake', $xmlVersion = '1.0', - $xmlEncoding = 'UTF-8' + $xmlEncoding = 'UTF-8', + $xmlStandalone = false ) { - $converter = new static($arr, $rootElementName, $elementCase, $xmlVersion, $xmlEncoding); + $converter = new static($arr, $rootElementName, $elementCase, $xmlVersion, $xmlEncoding, $xmlStandalone); return $converter->toXml(); } diff --git a/src/Support/Facades/LaravelXml.php b/src/Support/Facades/LaravelXml.php index d4f94a5..45ae4f3 100644 --- a/src/Support/Facades/LaravelXml.php +++ b/src/Support/Facades/LaravelXml.php @@ -21,6 +21,7 @@ public function __construct() * @param string $elementCase * @param string $xmlVersion * @param string $xmlEncoding + * @param bool $xmlStandalone * * @return string */ @@ -29,9 +30,10 @@ public function encode( $rootElementName = 'document', $elementCase = 'snake', $xmlVersion = '1.0', - $xmlEncoding = 'UTF-8' + $xmlEncoding = 'UTF-8', + $xmlStandalone = false ) { - return ArrayToXml::convert($arr, $rootElementName, $elementCase, $xmlVersion, $xmlEncoding); + return ArrayToXml::convert($arr, $rootElementName, $elementCase, $xmlVersion, $xmlEncoding, $xmlStandalone); } /** diff --git a/src/Support/helpers.php b/src/Support/helpers.php index 2c583d2..3f49682 100644 --- a/src/Support/helpers.php +++ b/src/Support/helpers.php @@ -13,12 +13,13 @@ * @param string $elementCase * @param string $xmlVersion * @param string $xmlEncoding + * @param bool $xmlStandalone * * @return string */ - function xml_encode(array $array, $rootElementName = 'document', $elementCase = 'slug', $xmlVersion = '1.0', $xmlEncoding = 'UTF-8') + function xml_encode(array $array, $rootElementName = 'document', $elementCase = 'slug', $xmlVersion = '1.0', $xmlEncoding = 'UTF-8', $xmlStandalone = false) { - return ArrayToXml::convert($array, $rootElementName, $elementCase, $xmlVersion, $xmlEncoding); + return ArrayToXml::convert($array, $rootElementName, $elementCase, $xmlVersion, $xmlEncoding, $xmlStandalone); } } diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php index 2801427..45a14b2 100644 --- a/tests/ExampleTest.php +++ b/tests/ExampleTest.php @@ -82,6 +82,15 @@ public function testEncodeArrayToXml() static::assertSame($xmlElement->asXML(), $xmlStr); } + public function testEncodeArrayToXmlStandalone() + { + $xmlElement = new XmlElement('jdoe'); + + $xmlStr = LaravelXml::encode(['alias' => 'jdoe'], 'root', 'snake', '1.0', 'UTF-8', true); + + static::assertSame($xmlElement->asXML(), $xmlStr); + } + public function testDecodeArrayFromXml() { $arr = LaravelXml::decode('jdoe');