Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for dropping XML declaration #145

Merged
merged 3 commits into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,36 @@ will result in:
<root><Good_guy><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name>Sauron</name><weapon>Evil Eye</weapon></Bad_guy></root>
```

### Dropping XML declaration

Call `$arrayToXml->dropXmlDeclaration()` method on ArrayToXml object to omit default XML declaration on top of the generated XML.

Example:

```php
$root = [
'rootElementName' => 'soap:Envelope',
'_attributes' => [
'xmlns:soap' => 'http://www.w3.org/2003/05/soap-envelope/',
],
];
$array = [
'soap:Header' => [],
'soap:Body' => [
'soap:key' => 'soap:value',
],
];
$arrayToXml = new ArrayToXml($array, $root);

$result = $arrayToXml->dropXmlDeclaration()->toXml();
```

This will result in:

```xml
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"><soap:Header/><soap:Body><soap:key>soap:value</soap:key></soap:Body></soap:Envelope>
```

## Testing

```bash
Expand Down
13 changes: 13 additions & 0 deletions src/ArrayToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class ArrayToXml

protected $replaceSpacesByUnderScoresInKeyNames = true;

protected $addXmlDeclaration = true;

protected $numericTagNamePrefix = 'numeric_';

public function __construct(
Expand Down Expand Up @@ -69,6 +71,10 @@ public static function convert(

public function toXml(): string
{
if ($this->addXmlDeclaration === false) {
return $this->document->saveXml($this->document->documentElement);
}

return $this->document->saveXML();
}

Expand Down Expand Up @@ -105,6 +111,13 @@ public function prettify()
return $this;
}

public function dropXmlDeclaration()
{
$this->addXmlDeclaration = false;

return $this;
}

private function convertElement(DOMElement $element, $value)
{
$sequential = $this->isArrayAllKeySequential($value);
Expand Down
20 changes: 20 additions & 0 deletions tests/ArrayToXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,4 +453,24 @@ public function it_can_set_dom_properties()
$this->assertTrue($dom->formatOutput);
$this->assertEquals('1234567', $dom->version);
}

/** @test */
public function it_can_drop_xml_declaration()
{
$root = [
'rootElementName' => 'soap:Envelope',
'_attributes' => [
'xmlns:soap' => 'http://www.w3.org/2003/05/soap-envelope/',
],
];
$array = [
'soap:Header' => [],
'soap:Body' => [
'soap:key' => 'soap:value',
],
];
$arrayToXml = new ArrayToXml($array, $root);

$this->assertMatchesSnapshot($arrayToXml->dropXmlDeclaration()->toXml());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

return '<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"><soap:Header/><soap:Body><soap:key>soap:value</soap:key></soap:Body></soap:Envelope>';