Skip to content

Commit

Permalink
Add standalone to XML declaration
Browse files Browse the repository at this point in the history
  • Loading branch information
mtvbrianking committed Dec 5, 2023
1 parent e6c2f10 commit 8cffa8e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
.vscode
*.cache
composer.lock
coverage.clover
Expand Down
14 changes: 11 additions & 3 deletions src/Support/ArrayToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ArrayToXml
* @param string $elementCase
* @param string $xmlVersion
* @param string $xmlEncoding
* @param bool $xmlStandalone
*
* @throws \DOMException
*/
Expand All @@ -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)) {
Expand All @@ -66,6 +72,7 @@ public function __construct(
* @param string $elementCase
* @param string $xmlVersion
* @param string $xmlEncoding
* @param bool $xmlStandalone
*
* @return string
*/
Expand All @@ -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();
}
Expand Down
6 changes: 4 additions & 2 deletions src/Support/Facades/LaravelXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function __construct()
* @param string $elementCase
* @param string $xmlVersion
* @param string $xmlEncoding
* @param bool $xmlStandalone
*
* @return string
*/
Expand All @@ -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);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Support/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@ public function testEncodeArrayToXml()
static::assertSame($xmlElement->asXML(), $xmlStr);
}

public function testEncodeArrayToXmlStandalone()
{
$xmlElement = new XmlElement('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><root><alias>jdoe</alias></root>');

$xmlStr = LaravelXml::encode(['alias' => 'jdoe'], 'root', 'snake', '1.0', 'UTF-8', true);

static::assertSame($xmlElement->asXML(), $xmlStr);
}

public function testDecodeArrayFromXml()
{
$arr = LaravelXml::decode('<document><alias>jdoe</alias></document>');
Expand Down

0 comments on commit 8cffa8e

Please sign in to comment.