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 $xmlStandalone as a new parameter #148

Merged
merged 4 commits into from
Oct 29, 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
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ After running this piece of code `$result` will contain:
</root>
```


### 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
Expand Down Expand Up @@ -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', [], true);
```

This will result in:

```xml
<?xml version="1.1" encoding="UTF-8" standalone="yes"?>
```


### Adding attributes to the root element

To add attributes to the root element provide an array with an `_attributes` key as the second argument.
Expand Down
13 changes: 10 additions & 3 deletions src/ArrayToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -55,15 +60,17 @@ 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,
$rootElement,
$replaceSpacesByUnderScoresInKeyNames,
$xmlEncoding,
$xmlVersion,
$domProperties
$domProperties,
$xmlStandalone
);

return $converter->toXml();
Expand Down
6 changes: 6 additions & 0 deletions tests/ArrayToXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

return '<?xml version="1.0" standalone="no"?>
<root/>
';