Skip to content

Commit 516f62a

Browse files
committed
commit
1 parent a0d1cfb commit 516f62a

11 files changed

+365
-1
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build
2+
composer.lock
3+
docs
4+
vendor

.scrutinizer.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
filter:
2+
excluded_paths: [tests/*]
3+
checks:
4+
php:
5+
code_rating: true
6+
remove_extra_empty_lines: true
7+
remove_php_closing_tag: true
8+
remove_trailing_whitespace: true
9+
fix_use_statements:
10+
remove_unused: true
11+
preserve_multiple: false
12+
preserve_blanklines: true
13+
order_alphabetically: true
14+
fix_php_opening_tag: true
15+
fix_linefeed: true
16+
fix_line_ending: true
17+
fix_identation_4spaces: true
18+
fix_doc_comments: true
19+
tools:
20+
external_code_coverage:
21+
timeout: 600
22+
runs: 3
23+
php_analyzer: true
24+
php_code_coverage: false
25+
php_code_sniffer:
26+
config:
27+
standard: PSR2
28+
filter:
29+
paths: ['src']
30+
php_loc:
31+
enabled: true
32+
excluded_dirs: [vendor, tests]
33+
php_cpd:
34+
enabled: true
35+
excluded_dirs: [vendor, tests]

.travis.yml

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
language: php
2+
3+
php:
4+
- 5.4
5+
- 5.5
6+
- 5.6
7+
- hhvm
8+
- 7
9+
10+
matrix:
11+
allow_failures:
12+
- php: 7
13+
14+
before_script:
15+
- travis_retry composer self-update
16+
- travis_retry composer install --prefer-source --no-interaction --dev
17+
18+
script: vendor/bin/phpunit

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Changelog
2+
3+
All Notable changes to `array-to-xml` will be documented in this file
4+
5+
## 1.0.0 - 2015-03-17
6+
7+
- initial release

CONTRIBUTING.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Contributing
2+
3+
Contributions are **welcome** and will be fully **credited**.
4+
5+
We accept contributions via Pull Requests on [Github](https://github.com/freekmurze/array-to-xml).
6+
7+
8+
## Pull Requests
9+
10+
- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer).
11+
12+
- **Add tests!** - Your patch won't be accepted if it doesn't have tests.
13+
14+
- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
15+
16+
- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option.
17+
18+
- **Create feature branches** - Don't ask us to pull from your master branch.
19+
20+
- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
21+
22+
- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please squash them before submitting.
23+
24+
25+
## Running Tests
26+
27+
``` bash
28+
$ phpunit
29+
```
30+
31+
32+
**Happy coding**!

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2015 Freek Van der Herten <[email protected]>
4+
5+
> Permission is hereby granted, free of charge, to any person obtaining a copy
6+
> of this software and associated documentation files (the "Software"), to deal
7+
> in the Software without restriction, including without limitation the rights
8+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
> copies of the Software, and to permit persons to whom the Software is
10+
> furnished to do so, subject to the following conditions:
11+
>
12+
> The above copyright notice and this permission notice shall be included in
13+
> all copies or substantial portions of the Software.
14+
>
15+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
> THE SOFTWARE.

README.md

100644100755
+81-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,81 @@
1-
# array-to-xml
1+
# Convert an arry to xml
2+
3+
[![Latest Version](https://img.shields.io/github/release/spatie/array-to-xml.svg?style=flat-square)](https://github.com/spatie/array-to-xml/releases)
4+
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
5+
[![Build Status](https://img.shields.io/travis/spatie/array-to-xml/master.svg?style=flat-square)](https://travis-ci.org/spatie/array-to-xml)
6+
[![Coverage Status](https://img.shields.io/scrutinizer/coverage/g/spatie/array-to-xml.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/array-to-xml/code-structure)
7+
[![Quality Score](https://img.shields.io/scrutinizer/g/spatie/array-to-xml.svg?style=flat-square)](https://scrutinizer-ci.com/g/spatie/array-to-xml)
8+
[![Total Downloads](https://img.shields.io/packagist/dt/league/array-to-xml.svg?style=flat-square)](https://packagist.org/packages/league/array-to-xml)
9+
10+
This package provides a very simple class to convert an array to an xml string.
11+
12+
## Install
13+
14+
Via Composer
15+
16+
``` bash
17+
composer require spatie/array-to-xml
18+
```
19+
20+
## Usage
21+
22+
``` php
23+
24+
use Spatie\ArrayToXml\ArrayToXml
25+
26+
...
27+
$array = [
28+
'Good guy' => [
29+
30+
'name' => 'Luke Skywalker',
31+
'weapon' => 'Lightsaber'
32+
33+
],
34+
'Bad guy' => [
35+
36+
'name' => 'Sauron',
37+
'weapon' => 'Evil Eye'
38+
39+
]
40+
];
41+
42+
$result = ArrayToXml::convert($array);
43+
```
44+
After running this piece of code `$result` will contain:
45+
46+
```xml
47+
<?xml version="1.0"?>
48+
<root>
49+
<Good_guy>
50+
<name>Luke Skywalker</name>
51+
<weapon>Lightsaber</weapon>
52+
</Good_guy>
53+
<Bad_guy>
54+
<name>Sauron</name>
55+
<weapon>Evil Eye</weapon>
56+
</Bad_guy>
57+
</root>
58+
``
59+
60+
## Testing
61+
62+
``` bash
63+
vendor/bin/phpunit
64+
```
65+
66+
## Contributing
67+
68+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
69+
70+
## Security
71+
72+
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
73+
74+
## Credits
75+
76+
- [Freek Van der Herten](https://github.com/freekmurze)
77+
- [All Contributors](../../contributors)
78+
79+
## License
80+
81+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "spatie/array-to-xml",
3+
"description": "Convert an array to xml",
4+
"keywords": [
5+
"convert",
6+
"array",
7+
"xml"
8+
],
9+
"homepage": "https://github.com/freekmurze/array-to-xml",
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "Freek Van der Herten",
14+
"email": "[email protected]",
15+
"homepage": "https://murze.be",
16+
"role": "Developer"
17+
}
18+
],
19+
"require": {
20+
"php" : ">=5.3.0"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit" : "4.*",
24+
"mockery/mockery": "0.9.*",
25+
"scrutinizer/ocular": "~1.1"
26+
},
27+
"autoload": {
28+
"psr-4": {
29+
"Spatie\\ArrayToXml\\": "src"
30+
}
31+
},
32+
"autoload-dev": {
33+
"psr-4": {
34+
"Spatie\\ArrayToXml\\Test\\": "tests"
35+
}
36+
}
37+
}

phpunit.xml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false">
12+
<testsuites>
13+
<testsuite name="Application Test Suite">
14+
<directory>./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
</phpunit>

src/ArrayToXml.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Spatie\ArrayToXml;
4+
5+
use SimpleXMLElement;
6+
7+
class ArrayToXml
8+
{
9+
/**
10+
* Convert the given array to an xml string
11+
*
12+
* @param $array
13+
* @param string $rootElementName
14+
* @param null $xml
15+
* @return string
16+
*/
17+
public static function convert($array, $rootElementName = '', $xml = null)
18+
{
19+
if ($xml === null) {
20+
$xml = new SimpleXMLElement(self::getRootElement($rootElementName));
21+
}
22+
23+
foreach ($array as $key => $value) {
24+
$key = str_replace(' ', '_', $key);
25+
26+
if (is_array($value)) {
27+
self::convert($value, $key, $xml->addChild($key));
28+
} else {
29+
$xml->addChild($key, $value);
30+
}
31+
}
32+
33+
return $xml->asXML();
34+
}
35+
36+
/**
37+
* Get the root element for the given name
38+
*
39+
* @param $name
40+
* @return string
41+
*/
42+
private static function getRootElement($name)
43+
{
44+
return '<'.($name == '' ? 'root' : $name).'/>';
45+
}
46+
}

tests/ArrayToXmlTest.php

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
use Spatie\ArrayToXml\ArrayToXml;
4+
5+
class ArrayToXmlTest extends PHPUnit_Framework_TestCase
6+
{
7+
/**
8+
* @test
9+
*/
10+
function it_can_convert_an_array_to_xml()
11+
{
12+
$array = [
13+
'Good guy' => [
14+
15+
'name' => 'Luke Skywalker',
16+
'weapon' => 'Lightsaber'
17+
18+
],
19+
'Bad guy' => [
20+
21+
'name' => 'Sauron',
22+
'weapon' => 'Evil Eye'
23+
24+
]
25+
];
26+
27+
$expectedXml = '<?xml version="1.0"?>
28+
<root><Good_guy><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name>Sauron</name><weapon>Evil Eye</weapon></Bad_guy></root>' . PHP_EOL;
29+
30+
$result = ArrayToXml::convert($array);
31+
32+
$this->assertEquals($expectedXml, $result);
33+
}
34+
35+
/**
36+
* @test
37+
*/
38+
function it_can_handle_an_empty_array()
39+
{
40+
$array = [];
41+
42+
$expectedXml = '<?xml version="1.0"?>
43+
<root/>' . PHP_EOL;
44+
45+
$result = ArrayToXml::convert($array);
46+
47+
$this->assertEquals($expectedXml, $result);
48+
}
49+
50+
/**
51+
* @test
52+
*/
53+
function it_can_receive_name_for_the_root_element()
54+
{
55+
$rootElementName = 'helloyouluckpeople';
56+
57+
$array = [];
58+
59+
$expectedXml = '<?xml version="1.0"?>
60+
<' . $rootElementName . '/>' . PHP_EOL;
61+
62+
$result = ArrayToXml::convert($array, $rootElementName);
63+
64+
$this->assertEquals($expectedXml, $result);
65+
}
66+
67+
}

0 commit comments

Comments
 (0)