diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..97896f7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +composer.lock +vendor +.DS_Store \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..9bc1156 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +The MIT License (MIT) + +Copyright (c) Mark Townsend + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..564d533 --- /dev/null +++ b/README.md @@ -0,0 +1,90 @@ +Easily convert valid xml to a php array. + +## Installation + +Install via composer: + +``` +composer require mtownsend/xml-to-array +``` + +## Quick start + +### Using the class + +```php +use Mtownsend\XmlToArray\XmlToArray; + +$xml = << + + fedex + 123 + 9205590164917312751089 + +XML; + +$array = XmlToArray::convert($xml); + +// $array is: +[ + 'carrier' => 'fedex', + 'id' => '123', + 'tracking_number' => '9205590164917312751089' +]; + +``` + +### Using the global helper + +```php +$xml = << + + fedex + 123 + 9205590164917312751089 + +XML; + +$array = xml_to_array($xml); + +// $array is: +[ + 'carrier' => 'fedex', + 'id' => '123', + 'tracking_number' => '9205590164917312751089' +]; +``` + +## Helpers, methods, and arguments + +**Static method** + +``XmlToArray::convert($xml, $outputRoot = false)`` + +The ``$outputRoot`` determines whether or not the php array will have a ``@root`` key. Default is ``false``. + +**Helper** + +``xml_to_array($xml, $outputRoot = false)`` + +Arguments are identical to ``XmlToArray::convert`` method. + +## Purpose + +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. + +## Other packages you may be interested in + +- [mtownsend/collection-xml](https://github.com/mtownsend5512/collection-xml) + +## Credits + +- Mark Townsend +- Adrien aka Gaarf +- All Contributors + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..d09b620 --- /dev/null +++ b/composer.json @@ -0,0 +1,37 @@ +{ + "name": "mtownsend/xml-to-array", + "description": "Easily convert valid xml to a php array.", + "keywords": [ + "laravel", + "xml", + "array", + "convert" + ], + "authors": [ + { + "name": "Mark Townsend", + "email": "mtownsend5512@gmail.com", + "role": "Developer" + } + ], + "autoload": { + "psr-4": { + "Mtownsend\\XmlToArray\\": "src" + }, + "files": [ + "src/helpers.php" + ] + }, + "require": { + "php": "~7.0" + }, + "require-dev": { + "phpunit/phpunit": "^6.4" + }, + "autoload-dev": { + "psr-4": { + "Mtownsend\\XmlToArray\\Test\\": "tests/" + } + }, + "minimum-stability": "stable" +} diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..7a16ca3 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,22 @@ + + + + + tests + + + + + src/ + + + \ No newline at end of file diff --git a/src/XmlToArray.php b/src/XmlToArray.php new file mode 100644 index 0000000..25e1396 --- /dev/null +++ b/src/XmlToArray.php @@ -0,0 +1,82 @@ +loadXML($xmlstr); + $root = $doc->documentElement; + $output = self::domNodeToArray($root); + $output['@root'] = $root->tagName; + return $output; + } + + protected static function domNodeToArray($node) + { + $output = []; + switch ($node->nodeType) { + case XML_CDATA_SECTION_NODE: + case XML_TEXT_NODE: + $output = trim($node->textContent); + break; + case XML_ELEMENT_NODE: + for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) { + $child = $node->childNodes->item($i); + $v = self::domNodeToArray($child); + if (isset($child->tagName)) { + $t = $child->tagName; + if (!isset($output[$t])) { + $output[$t] = []; + } + $output[$t][] = $v; + } elseif ($v || $v === '0') { + $output = (string) $v; + } + } + if ($node->attributes->length && !is_array($output)) { // Has attributes but isn't an array + $output = ['@content' => $output]; // Change output into an array. + } + if (is_array($output)) { + if ($node->attributes->length) { + $a = []; + foreach ($node->attributes as $attrName => $attrNode) { + $a[$attrName] = (string) $attrNode->value; + } + $output['@attributes'] = $a; + } + foreach ($output as $t => $v) { + if (is_array($v) && count($v) == 1 && $t != '@attributes') { + $output[$t] = $v[0]; + } + } + } + break; + } + return $output; + } +} diff --git a/src/helpers.php b/src/helpers.php new file mode 100644 index 0000000..b24ef31 --- /dev/null +++ b/src/helpers.php @@ -0,0 +1,15 @@ +testArray = [ + 'carrier' => 'fedex', + 'id' => 123, + 'tracking_number' => '9205590164917312751089', + ]; + $this->testXml = 'fedex1239205590164917312751089'; + } + + /** @test */ + public function xml_can_convert_to_array() + { + $this->assertEquals(xml_to_array($this->testXml), $this->testArray); + } +}