Skip to content

Commit a557566

Browse files
committed
Merge commit 'be43e324b8da8cf5f4a6effb3221dfe945372fa9'
2 parents 35af547 + be43e32 commit a557566

16 files changed

+450
-135
lines changed

.styleci.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
11
preset: laravel
2-
3-
linting: true

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
language: php
22

33
php:
4-
- 7.0
5-
- 7.1
64
- 7.2
5+
- 7.3
76

87
env:
98
matrix:

CHANGELOG.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,37 @@
22

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

5+
## 2.11.1 - 2019-07-25
6+
7+
- do not interpret "0" as a non-empty value
8+
9+
## 2.11.0 - 2019-09-26
10+
11+
- drop support for PHP 7.1
12+
13+
## 2.10.0 - 2019-09-26
14+
15+
- add `setDomProperties`
16+
17+
## 2.9.0 - 2019-05-06
18+
19+
- add support for numeric keys
20+
21+
## 2.8.1 - 2019-03-15
22+
23+
- fix tests
24+
- drop support for PHP 7.0
25+
26+
## 2.8.0 - 2018-11-29
27+
28+
- added support for mixed content
29+
30+
## 2.7.3 - 2018-10-30
31+
- fix for `DomExeception`s being thrown
32+
33+
## 2.7.2 - 2018-09-17
34+
- remove control characters
35+
536
## 2.7.1 - 2018-02-02
637
- fix setting attributes
738

README.md

Lines changed: 130 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,25 @@ After running this piece of code `$result` will contain:
5151
</root>
5252
```
5353

54+
### Setting the name of the root element
55+
5456
Optionally you can set the name of the rootElement by passing it as the second argument. If you don't specify
5557
this argument (or set it to an empty string) "root" will be used.
5658
```
5759
$result = ArrayToXml::convert($array, 'customrootname');
5860
```
5961

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

66-
You can use a key named `_attributes` to add attributes to a node.
70+
### Adding attributes
71+
72+
You can use a key named `_attributes` to add attributes to a node, and `_value` to specify the value.
6773

6874
```php
6975
$array = [
@@ -76,6 +82,10 @@ $array = [
7682
'name' => 'Sauron',
7783
'weapon' => 'Evil Eye'
7884
]
85+
'The survivor' => [
86+
'_attributes' => ['house'=>'Hogwarts'],
87+
'_value' => 'Harry Potter'
88+
]
7989
];
8090

8191
$result = ArrayToXml::convert($array);
@@ -94,9 +104,14 @@ This code will result in:
94104
<name>Sauron</name>
95105
<weapon>Evil Eye</weapon>
96106
</Bad_guy>
107+
<The_survivor house="Hogwarts">
108+
Harry Potter
109+
</The_survivor>
97110
</root>
98111
```
99112

113+
### Using reserved characters
114+
100115
It is also possible to wrap the value of a node into a CDATA section. This allows you to use reserved characters.
101116

102117
```php
@@ -134,6 +149,8 @@ This code will result in:
134149

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

152+
### Adding attributes to the root element
153+
137154
To add attributes to the root element provide an array with an `_attributes` key as the second argument.
138155
The root element name can then be set using the `rootElementName` key.
139156

@@ -143,7 +160,118 @@ $result = ArrayToXml::convert($array, [
143160
'_attributes' => [
144161
'xmlns' => 'https://github.com/spatie/array-to-xml',
145162
],
146-
]);
163+
], true, 'UTF-8');
164+
```
165+
166+
### Using a multi-dimensional array
167+
168+
Use a multi-dimensional array to create a collection of elements.
169+
```php
170+
$array = [
171+
'Good guys' => [
172+
'Guy' => [
173+
['name' => 'Luke Skywalker', 'weapon' => 'Lightsaber'],
174+
['name' => 'Captain America', 'weapon' => 'Shield'],
175+
],
176+
],
177+
'Bad guys' => [
178+
'Guy' => [
179+
['name' => 'Sauron', 'weapon' => 'Evil Eye'],
180+
['name' => 'Darth Vader', 'weapon' => 'Lightsaber'],
181+
],
182+
],
183+
];
184+
```
185+
186+
This will result in:
187+
188+
```xml
189+
<?xml version="1.0" encoding="UTF-8"?>
190+
<helloyouluckypeople xmlns="https://github.com/spatie/array-to-xml">
191+
<Good_guys>
192+
<Guy>
193+
<name>Luke Skywalker</name>
194+
<weapon>Lightsaber</weapon>
195+
</Guy>
196+
<Guy>
197+
<name>Captain America</name>
198+
<weapon>Shield</weapon>
199+
</Guy>
200+
</Good_guys>
201+
<Bad_guys>
202+
<Guy>
203+
<name>Sauron</name>
204+
<weapon>Evil Eye</weapon>
205+
</Guy>
206+
<Guy>
207+
<name>Darth Vader</name>
208+
<weapon>Lightsaber</weapon>
209+
</Guy>
210+
</Bad_guys>
211+
</helloyouluckypeople>
212+
```
213+
214+
### Handling numeric keys
215+
216+
The package can also can handle numeric keys:
217+
218+
```php
219+
$array = [
220+
100 => [
221+
'name' => 'Vladimir',
222+
'nickname' => 'greeflas',
223+
],
224+
200 => [
225+
'name' => 'Marina',
226+
'nickname' => 'estacet',
227+
],
228+
];
229+
230+
$result = ArrayToXml::convert(['__numeric' => $array]);
231+
```
232+
233+
This will result in:
234+
235+
```xml
236+
<?xml version="1.0" encoding="UTF-8"?>
237+
<root>
238+
<numeric_100>
239+
<name>Vladimir</name>
240+
<nickname>greeflas</nickname>
241+
</numeric_100>
242+
<numeric_200>
243+
<name>Marina</name>
244+
<nickname>estacet</nickname>
245+
</numeric_200>
246+
</root>
247+
```
248+
249+
You can change key prefix with setter method called `setNumericTagNamePrefix()`.
250+
251+
### Setting DOMDocument properties
252+
253+
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.
254+
255+
You can use the constructor to set DOMDocument properties.
256+
257+
```php
258+
$result = ArrayToXml::convert(
259+
$array,
260+
$rootElement,
261+
$replaceSpacesByUnderScoresInKeyNames,
262+
$xmlEncoding,
263+
$xmlVersion,
264+
['formatOutput' => true]
265+
);
266+
267+
```
268+
269+
Alternatively you can use `setDomProperties`
270+
271+
```php
272+
$arrayToXml = new ArrayToXml($array);
273+
$arrayToXml->setDomProperties(['formatOutput' => true]);
274+
$result = $arrayToXml->toXml();
147275
```
148276

149277
## Testing

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@
1717
}
1818
],
1919
"require": {
20-
"php" : "^7.0"
20+
"php" : "^7.2",
21+
"ext-dom": "*"
2122
},
2223
"require-dev": {
23-
"phpunit/phpunit" : "^6.3",
24+
"phpunit/phpunit" : "^8.0",
2425
"mockery/mockery": "^1.0",
25-
"spatie/phpunit-snapshot-assertions": "^1.0"
26+
"spatie/phpunit-snapshot-assertions": "^2.0"
2627
},
2728
"autoload": {
2829
"psr-4": {

phpunit.xml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@
77
convertNoticesToExceptions="true"
88
convertWarningsToExceptions="true"
99
processIsolation="false"
10-
stopOnFailure="false"
11-
syntaxCheck="false">
10+
stopOnFailure="false">
1211
<testsuites>
1312
<testsuite name="Application Test Suite">
1413
<directory>./tests/</directory>
1514
</testsuite>
1615
</testsuites>
16+
<filter>
17+
<whitelist>
18+
<directory suffix=".php">src/</directory>
19+
</whitelist>
20+
</filter>
1721
</phpunit>

0 commit comments

Comments
 (0)