Skip to content

Commit

Permalink
Merge commit 'be43e324b8da8cf5f4a6effb3221dfe945372fa9'
Browse files Browse the repository at this point in the history
  • Loading branch information
ashenwolf committed Jul 25, 2019
2 parents 35af547 + be43e32 commit a557566
Show file tree
Hide file tree
Showing 16 changed files with 450 additions and 135 deletions.
2 changes: 0 additions & 2 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
preset: laravel

linting: true
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
language: php

php:
- 7.0
- 7.1
- 7.2
- 7.3

env:
matrix:
Expand Down
31 changes: 31 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,37 @@

All notable changes to `array-to-xml` will be documented in this file

## 2.11.1 - 2019-07-25

- do not interpret "0" as a non-empty value

## 2.11.0 - 2019-09-26

- drop support for PHP 7.1

## 2.10.0 - 2019-09-26

- add `setDomProperties`

## 2.9.0 - 2019-05-06

- add support for numeric keys

## 2.8.1 - 2019-03-15

- fix tests
- drop support for PHP 7.0

## 2.8.0 - 2018-11-29

- added support for mixed content

## 2.7.3 - 2018-10-30
- fix for `DomExeception`s being thrown

## 2.7.2 - 2018-09-17
- remove control characters

## 2.7.1 - 2018-02-02
- fix setting attributes

Expand Down
132 changes: 130 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,25 @@ 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
this argument (or set it to an empty string) "root" will be used.
```
$result = ArrayToXml::convert($array, 'customrootname');
```

### Handling key names

By default all spaces in the key names of your array will be converted to underscores. If you want to opt out of
this behaviour you can set the third argument to false. We'll leave all keynames alone.
```
$result = ArrayToXml::convert($array, 'customrootname', false);
```

You can use a key named `_attributes` to add attributes to a node.
### Adding attributes

You can use a key named `_attributes` to add attributes to a node, and `_value` to specify the value.

```php
$array = [
Expand All @@ -76,6 +82,10 @@ $array = [
'name' => 'Sauron',
'weapon' => 'Evil Eye'
]
'The survivor' => [
'_attributes' => ['house'=>'Hogwarts'],
'_value' => 'Harry Potter'
]
];

$result = ArrayToXml::convert($array);
Expand All @@ -94,9 +104,14 @@ This code will result in:
<name>Sauron</name>
<weapon>Evil Eye</weapon>
</Bad_guy>
<The_survivor house="Hogwarts">
Harry Potter
</The_survivor>
</root>
```

### Using reserved characters

It is also possible to wrap the value of a node into a CDATA section. This allows you to use reserved characters.

```php
Expand Down Expand Up @@ -134,6 +149,8 @@ This code will result in:

If your input contains something that cannot be parsed a `DOMException` will be thrown.

### Adding attributes to the root element

To add attributes to the root element provide an array with an `_attributes` key as the second argument.
The root element name can then be set using the `rootElementName` key.

Expand All @@ -143,7 +160,118 @@ $result = ArrayToXml::convert($array, [
'_attributes' => [
'xmlns' => 'https://github.com/spatie/array-to-xml',
],
]);
], true, 'UTF-8');
```

### Using a multi-dimensional array

Use a multi-dimensional array to create a collection of elements.
```php
$array = [
'Good guys' => [
'Guy' => [
['name' => 'Luke Skywalker', 'weapon' => 'Lightsaber'],
['name' => 'Captain America', 'weapon' => 'Shield'],
],
],
'Bad guys' => [
'Guy' => [
['name' => 'Sauron', 'weapon' => 'Evil Eye'],
['name' => 'Darth Vader', 'weapon' => 'Lightsaber'],
],
],
];
```

This will result in:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<helloyouluckypeople xmlns="https://github.com/spatie/array-to-xml">
<Good_guys>
<Guy>
<name>Luke Skywalker</name>
<weapon>Lightsaber</weapon>
</Guy>
<Guy>
<name>Captain America</name>
<weapon>Shield</weapon>
</Guy>
</Good_guys>
<Bad_guys>
<Guy>
<name>Sauron</name>
<weapon>Evil Eye</weapon>
</Guy>
<Guy>
<name>Darth Vader</name>
<weapon>Lightsaber</weapon>
</Guy>
</Bad_guys>
</helloyouluckypeople>
```

### Handling numeric keys

The package can also can handle numeric keys:

```php
$array = [
100 => [
'name' => 'Vladimir',
'nickname' => 'greeflas',
],
200 => [
'name' => 'Marina',
'nickname' => 'estacet',
],
];

$result = ArrayToXml::convert(['__numeric' => $array]);
```

This will result in:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<numeric_100>
<name>Vladimir</name>
<nickname>greeflas</nickname>
</numeric_100>
<numeric_200>
<name>Marina</name>
<nickname>estacet</nickname>
</numeric_200>
</root>
```

You can change key prefix with setter method called `setNumericTagNamePrefix()`.

### Setting DOMDocument properties

To set properties of the internal DOMDocument object just pass an array consisting of keys and values. For a full list of valid properties consult https://www.php.net/manual/en/class.domdocument.php.

You can use the constructor to set DOMDocument properties.

```php
$result = ArrayToXml::convert(
$array,
$rootElement,
$replaceSpacesByUnderScoresInKeyNames,
$xmlEncoding,
$xmlVersion,
['formatOutput' => true]
);

```

Alternatively you can use `setDomProperties`

```php
$arrayToXml = new ArrayToXml($array);
$arrayToXml->setDomProperties(['formatOutput' => true]);
$result = $arrayToXml->toXml();
```

## Testing
Expand Down
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@
}
],
"require": {
"php" : "^7.0"
"php" : "^7.2",
"ext-dom": "*"
},
"require-dev": {
"phpunit/phpunit" : "^6.3",
"phpunit/phpunit" : "^8.0",
"mockery/mockery": "^1.0",
"spatie/phpunit-snapshot-assertions": "^1.0"
"spatie/phpunit-snapshot-assertions": "^2.0"
},
"autoload": {
"psr-4": {
Expand Down
8 changes: 6 additions & 2 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
stopOnFailure="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
</phpunit>
Loading

0 comments on commit a557566

Please sign in to comment.