Skip to content

Commit

Permalink
Add addXmlDeclaration parameter (#216)
Browse files Browse the repository at this point in the history
* Add addXmlDeclaration parameter

* Add test
  • Loading branch information
silnex authored May 11, 2023
1 parent bd3a01a commit e210b98
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/ArrayToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public function __construct(
string | null $xmlEncoding = null,
string $xmlVersion = '1.0',
array $domProperties = [],
bool | null $xmlStandalone = null
bool | null $xmlStandalone = null,
bool $addXmlDeclaration = true
) {
$this->document = new DOMDocument($xmlVersion, $xmlEncoding ?? '');

Expand All @@ -36,6 +37,8 @@ public function __construct(
$this->setDomProperties($domProperties);
}

$this->addXmlDeclaration = $addXmlDeclaration;

$this->replaceSpacesByUnderScoresInKeyNames = $replaceSpacesByUnderScoresInKeyNames;

if (! empty($array) && $this->isArrayAllKeySequential($array)) {
Expand All @@ -61,7 +64,8 @@ public static function convert(
string $xmlEncoding = null,
string $xmlVersion = '1.0',
array $domProperties = [],
bool $xmlStandalone = null
bool $xmlStandalone = null,
bool $addXmlDeclaration = true,
): string {
$converter = new static(
$array,
Expand All @@ -70,7 +74,8 @@ public static function convert(
$xmlEncoding,
$xmlVersion,
$domProperties,
$xmlStandalone
$xmlStandalone,
$addXmlDeclaration
);

return $converter->toXml();
Expand Down
4 changes: 4 additions & 0 deletions tests/ArrayToXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
assertMatchesXmlSnapshot(ArrayToXml::convert([], '', false, null, '1.0', [], false));
});

it('accepts an xml declaration value', function () {
assertMatchesXmlSnapshot(ArrayToXml::convert([], '', false, null, '1.0', [], null, false));
});

it('can handle values as collection', function () {
assertMatchesXmlSnapshot(ArrayToXml::convert([
'user' => [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<root/>

0 comments on commit e210b98

Please sign in to comment.