Skip to content

Commit 0f41a39

Browse files
committed
Package launch
0 parents  commit 0f41a39

File tree

8 files changed

+299
-0
lines changed

8 files changed

+299
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
composer.lock
2+
vendor
3+
.DS_Store

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Mark Townsend <mtownsend5512@gmail.com>
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

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
Easily convert valid xml to a php array.
2+
3+
## Installation
4+
5+
Install via composer:
6+
7+
```
8+
composer require mtownsend/xml-to-array
9+
```
10+
11+
## Quick start
12+
13+
### Using the class
14+
15+
```php
16+
use Mtownsend\XmlToArray\XmlToArray;
17+
18+
$xml = <<<XML
19+
<?xml version="1.0"?>
20+
<request>
21+
<carrier>fedex</carrier>
22+
<id>123</id>
23+
<tracking_number>9205590164917312751089</tracking_number>
24+
</request>
25+
XML;
26+
27+
$array = XmlToArray::convert($xml);
28+
29+
// $array is:
30+
[
31+
'carrier' => 'fedex',
32+
'id' => '123',
33+
'tracking_number' => '9205590164917312751089'
34+
];
35+
36+
```
37+
38+
### Using the global helper
39+
40+
```php
41+
$xml = <<<XML
42+
<?xml version="1.0"?>
43+
<request>
44+
<carrier>fedex</carrier>
45+
<id>123</id>
46+
<tracking_number>9205590164917312751089</tracking_number>
47+
</request>
48+
XML;
49+
50+
$array = xml_to_array($xml);
51+
52+
// $array is:
53+
[
54+
'carrier' => 'fedex',
55+
'id' => '123',
56+
'tracking_number' => '9205590164917312751089'
57+
];
58+
```
59+
60+
## Helpers, methods, and arguments
61+
62+
**Static method**
63+
64+
``XmlToArray::convert($xml, $outputRoot = false)``
65+
66+
The ``$outputRoot`` determines whether or not the php array will have a ``@root`` key. Default is ``false``.
67+
68+
**Helper**
69+
70+
``xml_to_array($xml, $outputRoot = false)``
71+
72+
Arguments are identical to ``XmlToArray::convert`` method.
73+
74+
## Purpose
75+
76+
XML has always been a challenge to work with in PHP compared to other data formats, such as JSON. This package aims to make integrating with XML files or api requests significantly easier. With this package, you might actually like interfacing with XML in your application now.
77+
78+
## Other packages you may be interested in
79+
80+
- [mtownsend/collection-xml](https://github.com/mtownsend5512/collection-xml)
81+
82+
## Credits
83+
84+
- Mark Townsend
85+
- Adrien aka Gaarf
86+
- All Contributors
87+
88+
## License
89+
90+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

composer.json

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "mtownsend/xml-to-array",
3+
"description": "Easily convert valid xml to a php array.",
4+
"keywords": [
5+
"laravel",
6+
"xml",
7+
"array",
8+
"convert"
9+
],
10+
"authors": [
11+
{
12+
"name": "Mark Townsend",
13+
"email": "mtownsend5512@gmail.com",
14+
"role": "Developer"
15+
}
16+
],
17+
"autoload": {
18+
"psr-4": {
19+
"Mtownsend\\XmlToArray\\": "src"
20+
},
21+
"files": [
22+
"src/helpers.php"
23+
]
24+
},
25+
"require": {
26+
"php": "~7.0"
27+
},
28+
"require-dev": {
29+
"phpunit/phpunit": "^6.4"
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"Mtownsend\\XmlToArray\\Test\\": "tests/"
34+
}
35+
},
36+
"minimum-stability": "stable"
37+
}

phpunit.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="MyPackage Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

src/XmlToArray.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Mtownsend\XmlToArray;
4+
5+
use DOMDocument;
6+
7+
/**
8+
* @author Adrien aka Gaarf & contributors
9+
* @author Mark Townsend
10+
*/
11+
class XmlToArray
12+
{
13+
/**
14+
* Convert valid XML to an array.
15+
*
16+
* @param string $xml
17+
* @param bool $outputRoot
18+
* @return array
19+
*/
20+
public static function convert($xml, $outputRoot = false)
21+
{
22+
$array = self::xmlStringToArray($xml);
23+
if (!$outputRoot && array_key_exists('@root', $array)) {
24+
unset($array['@root']);
25+
}
26+
return $array;
27+
}
28+
29+
protected static function xmlStringToArray($xmlstr)
30+
{
31+
$doc = new DOMDocument();
32+
$doc->loadXML($xmlstr);
33+
$root = $doc->documentElement;
34+
$output = self::domNodeToArray($root);
35+
$output['@root'] = $root->tagName;
36+
return $output;
37+
}
38+
39+
protected static function domNodeToArray($node)
40+
{
41+
$output = [];
42+
switch ($node->nodeType) {
43+
case XML_CDATA_SECTION_NODE:
44+
case XML_TEXT_NODE:
45+
$output = trim($node->textContent);
46+
break;
47+
case XML_ELEMENT_NODE:
48+
for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) {
49+
$child = $node->childNodes->item($i);
50+
$v = self::domNodeToArray($child);
51+
if (isset($child->tagName)) {
52+
$t = $child->tagName;
53+
if (!isset($output[$t])) {
54+
$output[$t] = [];
55+
}
56+
$output[$t][] = $v;
57+
} elseif ($v || $v === '0') {
58+
$output = (string) $v;
59+
}
60+
}
61+
if ($node->attributes->length && !is_array($output)) { // Has attributes but isn't an array
62+
$output = ['@content' => $output]; // Change output into an array.
63+
}
64+
if (is_array($output)) {
65+
if ($node->attributes->length) {
66+
$a = [];
67+
foreach ($node->attributes as $attrName => $attrNode) {
68+
$a[$attrName] = (string) $attrNode->value;
69+
}
70+
$output['@attributes'] = $a;
71+
}
72+
foreach ($output as $t => $v) {
73+
if (is_array($v) && count($v) == 1 && $t != '@attributes') {
74+
$output[$t] = $v[0];
75+
}
76+
}
77+
}
78+
break;
79+
}
80+
return $output;
81+
}
82+
}

src/helpers.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
if (!function_exists('xml_to_array')) {
4+
/**
5+
* Convert valid XML to an array.
6+
*
7+
* @param string $xml
8+
* @param bool $outputRoot
9+
* @return array
10+
*/
11+
function xml_to_array($xml, $outputRoot = false)
12+
{
13+
return \Mtownsend\XmlToArray\XmlToArray::convert($xml, $outputRoot);
14+
}
15+
}

tests/CollectionXmlTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
class CollectionXml extends TestCase
6+
{
7+
8+
/** @test array */
9+
protected $testArray = [];
10+
11+
/** @test string */
12+
protected $testXml;
13+
14+
public function setUp()
15+
{
16+
$this->testArray = [
17+
'carrier' => 'fedex',
18+
'id' => 123,
19+
'tracking_number' => '9205590164917312751089',
20+
];
21+
$this->testXml = '<?xml version="1.0"?><root><carrier>fedex</carrier><id>123</id><tracking_number>9205590164917312751089</tracking_number></root>';
22+
}
23+
24+
/** @test */
25+
public function xml_can_convert_to_array()
26+
{
27+
$this->assertEquals(xml_to_array($this->testXml), $this->testArray);
28+
}
29+
}

0 commit comments

Comments
 (0)