From 860e563192eb7593c9e85c901d18e046cca8107b Mon Sep 17 00:00:00 2001 From: Adrien Cahen Date: Mon, 30 Jul 2012 20:31:29 -0700 Subject: [PATCH 01/21] added @root elem tagname --- test.sh | 7 ++++++- xmlstr_to_array.php | 5 ++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/test.sh b/test.sh index 02dc458..de4bf88 100755 --- a/test.sh +++ b/test.sh @@ -11,7 +11,7 @@ function prettyPrint($title, $thing) } $xmlstr = << + Brian Chris @@ -34,6 +34,11 @@ $xmlstr = << 'tv', + + "@attributes" => array( "type" => "cartoon" ), + "show" => array( array( diff --git a/xmlstr_to_array.php b/xmlstr_to_array.php index c847de2..75bf372 100644 --- a/xmlstr_to_array.php +++ b/xmlstr_to_array.php @@ -12,7 +12,10 @@ function xmlstr_to_array($xmlstr) { $doc = new DOMDocument(); $doc->loadXML($xmlstr); - return domnode_to_array($doc->documentElement); + $root = $doc->documentElement; + $output = domnode_to_array($root); + $output['@root'] = $root->tagName; + return $output; } function domnode_to_array($node) { From 652b04918bf40698eed5a3714bf0844b480a4106 Mon Sep 17 00:00:00 2001 From: Adrien Cahen Date: Mon, 30 Jul 2012 20:35:03 -0700 Subject: [PATCH 02/21] update readme with mention of #4 --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 8c5d61d..d3548ff 100644 --- a/README.md +++ b/README.md @@ -106,3 +106,6 @@ Victory is mine! :D ### Contributions [clh-code#1] If a node has attributes, but contains only text, then the output will be an array with both ```@content``` and ```@attributes``` keys + +[reggi#4] store root element tag name in ```@root``` + From f9d6b637e6d77f49fcf5f8762bc08201b5202d07 Mon Sep 17 00:00:00 2001 From: Adrien Cahen Date: Fri, 2 Oct 2015 17:19:43 -0700 Subject: [PATCH 03/21] add unlicense, fix #9 --- LICENSE.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 LICENSE.md diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..2c31e5c --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,24 @@ +This is free and unencumbered software released into the public domain. + +Anyone is free to copy, modify, publish, use, compile, sell, or +distribute this software, either in source code form or as a compiled +binary, for any purpose, commercial or non-commercial, and by any +means. + +In jurisdictions that recognize copyright laws, the author or authors +of this software dedicate any and all copyright interest in the +software to the public domain. We make this dedication for the benefit +of the public at large and to the detriment of our heirs and +successors. We intend this dedication to be an overt act of +relinquishment in perpetuity of all present and future rights to this +software under copyright law. + +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 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. + +For more information, please refer to <> \ No newline at end of file From db9436fc87ebe89f90b6976d4139c0001e3e1583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Thu, 8 Aug 2019 09:46:57 +0200 Subject: [PATCH 04/21] Add support for PHP 7.1 + better code style --- xmlstr_to_array.php | 69 ++++++++++++++++++++++++--------------------- 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/xmlstr_to_array.php b/xmlstr_to_array.php index 75bf372..676eb41 100644 --- a/xmlstr_to_array.php +++ b/xmlstr_to_array.php @@ -1,66 +1,71 @@ loadXML($xmlstr); + */ +function xmlstr_to_array(string $xml): array +{ + assert(\class_exists('\DOMDocument')); + $doc = new \DOMDocument(); + $doc->loadXML($xml); $root = $doc->documentElement; - $output = domnode_to_array($root); + $output = (array) domnode_to_array($root); $output['@root'] = $root->tagName; - return $output; + + return $output ?? []; } -function domnode_to_array($node) { - $output = array(); +/** + * @param \DOMElement $node + * @return array|string + */ +function domnode_to_array($node) +{ + $output = []; switch ($node->nodeType) { - - case XML_CDATA_SECTION_NODE: - case XML_TEXT_NODE: + case 4: // XML_CDATA_SECTION_NODE + case 3: // XML_TEXT_NODE $output = trim($node->textContent); - break; - - case XML_ELEMENT_NODE: - for ($i=0, $m=$node->childNodes->length; $i<$m; $i++) { + break; + case 1: // XML_ELEMENT_NODE + for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) { $child = $node->childNodes->item($i); $v = domnode_to_array($child); - if(isset($child->tagName)) { + if (isset($child->tagName)) { $t = $child->tagName; - if(!isset($output[$t])) { - $output[$t] = array(); + if (!isset($output[$t])) { + $output[$t] = []; } $output[$t][] = $v; - } - elseif($v || $v === '0') { + } elseif ($v || $v === '0') { $output = (string) $v; } } - if($node->attributes->length && !is_array($output)) { //Has attributes but isn't an array - $output = array('@content'=>$output); //Change output into an array. + 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 = array(); - foreach($node->attributes as $attrName => $attrNode) { + 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') { + if ($t !== '@attributes' && is_array($v) && count($v) === 1) { $output[$t] = $v[0]; } } } - break; + break; } + return $output; } -?> \ No newline at end of file From ba07e5d60d383d55e3a17e4bc0bb76152fc36a32 Mon Sep 17 00:00:00 2001 From: Adrien Cahen Date: Fri, 9 Aug 2019 21:49:56 -0700 Subject: [PATCH 05/21] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d3548ff..9363156 100644 --- a/README.md +++ b/README.md @@ -109,3 +109,4 @@ Victory is mine! :D [reggi#4] store root element tag name in ```@root``` +[janbarasek#13] Add support for PHP 7.1 + better code style From 895bfd19cb5783fc6a2b12ba99b6fd44a5d9265d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Sat, 10 Aug 2019 19:42:21 +0200 Subject: [PATCH 06/21] Composer: Reimplementation of whole repository to Composer package. --- README.md | 174 +++++++++++++++++++++++---------------- composer.json | 25 ++++++ src/Convertor.php | 32 +++++++ src/Helper.php | 61 ++++++++++++++ test.sh => tests/test.sh | 19 +++-- xmlstr_to_array.php | 71 ---------------- 6 files changed, 234 insertions(+), 148 deletions(-) create mode 100644 composer.json create mode 100644 src/Convertor.php create mode 100644 src/Helper.php rename test.sh => tests/test.sh (80%) delete mode 100644 xmlstr_to_array.php diff --git a/README.md b/README.md index 9363156..954b467 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,26 @@ +XML to PHP array convertor +========================== + +Smart tool to convert your XML to PHP array. + +Install and simply use +---------------------- + +Use Composer: + +```shell +composer require gaarf/xml-to-php-array +``` + +And then package will be automatically installed to your project and you can simply call: + +```php +$resultArray = Convertor::covertToArray($xml); +``` + +Documentation +------------- + One common need when working in PHP is a way to convert an XML document into a serializable array. If you ever tried to serialize() and then unserialize() a SimpleXML or DOMDocument object, you know what I’m @@ -5,74 +28,82 @@ talking about. Assume the following XML snippet: - - - Brian - Chris - Meg - - +```xml + + + Brian + Chris + Meg + + +``` There’s a quick and dirty way to do convert such a document to an array, using type casting and the JSON functions to ensure there are no exotic values that would cause problems when unserializing: - +```php +$a = json_decode(json_encode((array) Convertor::covertToArray($s)), true); +``` Here is the result for our sample XML, eg if we `print_r($a)`: - Array - ( - [show] => Array - ( - [@attributes] => Array - ( - [name] => Family Guy - ) - [dog] => Brian - [kid] => Array - ( - [0] => Chris - [1] => Meg - ) - ) - ) +``` +Array +( + [show] => Array + ( + [@attributes] => Array + ( + [name] => Family Guy + ) + [dog] => Brian + [kid] => Array + ( + [0] => Chris + [1] => Meg + ) + ) +) +``` Pretty nifty, eh? But maybe we want to embed some HTML tags or something crazy along those lines. then we need a CDATA node… - - - Brian - Chris - Meg - Stewie]]> - - +```xml + + + Brian + Chris + Meg + Stewie]]> + + +``` The snippet of XML above would yield the following: - Array - ( - [show] => Array - ( - [@attributes] => Array - ( - [name] => Family Guy - ) - [dog] => Brian - [kid] => Array - ( - [0] => Chris - [1] => Meg - [2] => Array - ( - ) - ) - ) - ) +``` +Array +( + [show] => Array + ( + [@attributes] => Array + ( + [name] => Family Guy + ) + [dog] => Brian + [kid] => Array + ( + [0] => Chris + [1] => Meg + [2] => Array + ( + ) + ) + ) +) +``` That’s not very useful. We got in trouble because the CDATA node, a SimpleXMLElement, is being cast to an array instead of a string. To @@ -82,29 +113,32 @@ hereby released under a do-whatever-but-dont-sue-me license. The result, for our *Stewie* snippet: - Array - ( - [show] => Array - ( - [@attributes] => Array - ( - [name] => Family Guy - ) - [dog] => Brian - [kid] => Array - ( - [0] => Chris - [1] => Meg - [2] => Stewie - ) - ) - ) +``` +Array +( + [show] => Array + ( + [@attributes] => Array + ( + [name] => Family Guy + ) + [dog] => Brian + [kid] => Array + ( + [0] => Chris + [1] => Meg + [2] => Stewie + ) + ) +) +``` Victory is mine! :D --- ### Contributions + [clh-code#1] If a node has attributes, but contains only text, then the output will be an array with both ```@content``` and ```@attributes``` keys [reggi#4] store root element tag name in ```@root``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..43cc701 --- /dev/null +++ b/composer.json @@ -0,0 +1,25 @@ +{ + "name": "gaarf/xml-to-php-array", + "description": "XML to PHP array convertor", + "homepage": "https://github.com/gaarf/XML-string-to-PHP-array", + "authors": [ + { + "name": "Adrien Cahen", + "homepage": "http://gaarf.info" + }, + { + "name": "Jan Barášek", + "homepage": "http://baraja.cz" + } + ], + "require": { + "php": ">=7.1.0", + "ext-dom": "*" + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "minimum-stability": "stable" +} diff --git a/src/Convertor.php b/src/Convertor.php new file mode 100644 index 0000000..21ce724 --- /dev/null +++ b/src/Convertor.php @@ -0,0 +1,32 @@ +loadXML($xml); + $root = $doc->documentElement; + $output = (array) Helper::domNodeToArray($root); + $output['@root'] = $root->tagName; + + return $output ?? []; + } + +} \ No newline at end of file diff --git a/src/Helper.php b/src/Helper.php new file mode 100644 index 0000000..70138f1 --- /dev/null +++ b/src/Helper.php @@ -0,0 +1,61 @@ +nodeType) { + case 4: // XML_CDATA_SECTION_NODE + case 3: // XML_TEXT_NODE + $output = trim($node->textContent); + break; + case 1: // 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 ($t !== '@attributes' && is_array($v) && count($v) === 1) { + $output[$t] = $v[0]; + } + } + } + break; + } + + return $output; + } + + +} \ No newline at end of file diff --git a/test.sh b/tests/test.sh similarity index 80% rename from test.sh rename to tests/test.sh index de4bf88..da5ddf0 100755 --- a/test.sh +++ b/tests/test.sh @@ -1,7 +1,8 @@ #!/usr/bin/env php loadXML($xml); - $root = $doc->documentElement; - $output = (array) domnode_to_array($root); - $output['@root'] = $root->tagName; - - return $output ?? []; -} - -/** - * @param \DOMElement $node - * @return array|string - */ -function domnode_to_array($node) -{ - $output = []; - switch ($node->nodeType) { - case 4: // XML_CDATA_SECTION_NODE - case 3: // XML_TEXT_NODE - $output = trim($node->textContent); - break; - case 1: // XML_ELEMENT_NODE - for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) { - $child = $node->childNodes->item($i); - $v = domnode_to_array($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 ($t !== '@attributes' && is_array($v) && count($v) === 1) { - $output[$t] = $v[0]; - } - } - } - break; - } - - return $output; -} From 51bfc0f5deb1f1ea17252cc419d8e01789ae66b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Tue, 8 Oct 2019 20:38:13 +0200 Subject: [PATCH 07/21] README: Add information about rewrite as package. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 954b467..f868910 100644 --- a/README.md +++ b/README.md @@ -143,4 +143,4 @@ Victory is mine! :D [reggi#4] store root element tag name in ```@root``` -[janbarasek#13] Add support for PHP 7.1 + better code style +[janbarasek#13] Add support for PHP 7.1 + better code style. Rewrite repository as Composer package. From 2fc91609119016132650759f8032a7abeeb51770 Mon Sep 17 00:00:00 2001 From: Adrien Cahen Date: Tue, 8 Oct 2019 11:51:04 -0700 Subject: [PATCH 08/21] Update README.md --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f868910..dc7988a 100644 --- a/README.md +++ b/README.md @@ -143,4 +143,6 @@ Victory is mine! :D [reggi#4] store root element tag name in ```@root``` -[janbarasek#13] Add support for PHP 7.1 + better code style. Rewrite repository as Composer package. +[janbarasek#13] Add support for PHP 7.1 + better code style. + +[janbarasek#15] Rewrite repository as Composer package. From 5428f748c8e721c61dad0d3a6e3ea1994b706c7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Wed, 25 Mar 2020 11:40:19 +0100 Subject: [PATCH 09/21] Define automated tests --- .github/workflows/main.yml | 32 ++++++++++++++++++++++++++++++++ composer.json | 35 +++++++++++++++++++++++++++++++++++ phpstan.neon | 3 +++ 3 files changed, 70 insertions(+) create mode 100644 .github/workflows/main.yml create mode 100644 composer.json create mode 100644 phpstan.neon diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..463ba7e --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,32 @@ +name: Integrity check + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@master + + - name: Install PHP + uses: shivammathur/setup-php@master + with: + php-version: 7.4 + + - name: Install composer deps + run: | + composer create-project nette/code-checker temp/code-checker ^3 --no-progress + composer create-project nette/coding-standard temp/coding-standard ^2 --no-progress + + # Install app deps + composer install --no-interaction --prefer-dist + + # Check code checker and coding standards + - name: Check coding standards + run: | + php temp/code-checker/code-checker --short-arrays --strict-types --fix --no-progress + php temp/coding-standard/ecs check src --config temp/coding-standard/coding-standard-php71.yml + + - name: Check PHPStan rules + run: composer phpstan diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..4a12c33 --- /dev/null +++ b/composer.json @@ -0,0 +1,35 @@ +{ + "name": "gaarf/xml-to-php-array", + "description": "XML to PHP array convertor", + "homepage": "https://github.com/gaarf/XML-string-to-PHP-array", + "authors": [ + { + "name": "Adrien Cahen", + "homepage": "http://gaarf.info" + }, + { + "name": "Jan Barášek", + "homepage": "https://baraja.cz" + } + ], + "require": { + "php": ">=7.1.0", + "ext-dom": "*" + }, + "require-dev": { + "phpstan/phpstan": "^0.12.18", + "tracy/tracy": "^2.7", + "phpstan/phpstan-nette": "^0.12.6" + }, + "autoload": { + "classmap": [ + "src/" + ] + }, + "scripts": { + "phpstan": [ + "vendor/bin/phpstan analyse src -c phpstan.neon --level 6 --no-progress" + ] + }, + "minimum-stability": "stable" +} \ No newline at end of file diff --git a/phpstan.neon b/phpstan.neon new file mode 100644 index 0000000..25bf1ec --- /dev/null +++ b/phpstan.neon @@ -0,0 +1,3 @@ +includes: + - vendor/phpstan/phpstan-nette/extension.neon + - vendor/phpstan/phpstan-nette/rules.neon From be1325cffca37edef00410081d221f402602d90a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Wed, 25 Mar 2020 11:43:13 +0100 Subject: [PATCH 10/21] Fix codestyle --- src/Convertor.php | 1 - src/Helper.php | 1 - 2 files changed, 2 deletions(-) diff --git a/src/Convertor.php b/src/Convertor.php index 21ce724..5fd944b 100644 --- a/src/Convertor.php +++ b/src/Convertor.php @@ -28,5 +28,4 @@ public static function covertToArray(string $xml): array return $output ?? []; } - } \ No newline at end of file diff --git a/src/Helper.php b/src/Helper.php index 70138f1..545a922 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -57,5 +57,4 @@ public static function domNodeToArray($node) return $output; } - } \ No newline at end of file From 50992598c69919fe293d64d08aa415f238442e4f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Wed, 25 Mar 2020 11:44:53 +0100 Subject: [PATCH 11/21] Helper: Codestyle fix --- src/Helper.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Helper.php b/src/Helper.php index 545a922..cf8c28c 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -56,5 +56,4 @@ public static function domNodeToArray($node) return $output; } - } \ No newline at end of file From 2ffcc4bab5ac6c3595c802bf77ed9cd5cabfe5f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Wed, 25 Mar 2020 11:47:23 +0100 Subject: [PATCH 12/21] Helper: Codestyle fix --- src/Convertor.php | 2 +- src/Helper.php | 13 ++++++++++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Convertor.php b/src/Convertor.php index 5fd944b..300a360 100644 --- a/src/Convertor.php +++ b/src/Convertor.php @@ -5,7 +5,7 @@ namespace Gaarf\XmlToPhp; -class Convertor +final class Convertor { /** diff --git a/src/Helper.php b/src/Helper.php index cf8c28c..2bfdad1 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -5,12 +5,19 @@ namespace Gaarf\XmlToPhp; -class Helper +final class Helper { + /** @throws \Error */ + public function __construct() + { + throw new \Error('Class ' . get_class($this) . ' is static and cannot be instantiated.'); + } + + /** - * @param \DOMElement $node - * @return array|string + * @param \DOMElement|\DOMNode $node + * @return mixed[]|string */ public static function domNodeToArray($node) { From 13f2f11b3155ab87bf525e8817f4b97fa54fc2d4 Mon Sep 17 00:00:00 2001 From: Roland Dalmulder Date: Sun, 29 Mar 2020 16:51:53 +0200 Subject: [PATCH 13/21] Treat empty node as string Signed-off-by: Roland Dalmulder --- src/Helper.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Helper.php b/src/Helper.php index 2bfdad1..94b10a8 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -36,6 +36,9 @@ public static function domNodeToArray($node) if (!isset($output[$t])) { $output[$t] = []; } + if (empty($v)) { + $v = ''; + } $output[$t][] = $v; } elseif ($v || $v === '0') { $output = (string) $v; @@ -63,4 +66,4 @@ public static function domNodeToArray($node) return $output; } -} \ No newline at end of file +} From 07e375c72a4967374c748b0e6b8e535055c3a771 Mon Sep 17 00:00:00 2001 From: Adrien Cahen Date: Sun, 24 May 2020 12:33:42 -0700 Subject: [PATCH 14/21] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index dc7988a..4e76274 100644 --- a/README.md +++ b/README.md @@ -146,3 +146,5 @@ Victory is mine! :D [janbarasek#13] Add support for PHP 7.1 + better code style. [janbarasek#15] Rewrite repository as Composer package. + +[roland-d#18] Treat empty node as string From ac7faf27d2c6faa5a581f987c98c88486397b4e7 Mon Sep 17 00:00:00 2001 From: Adrien Cahen Date: Sun, 24 May 2020 12:35:47 -0700 Subject: [PATCH 15/21] Update test.sh --- tests/test.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test.sh b/tests/test.sh index da5ddf0..c9e9700 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -68,7 +68,7 @@ $expected = array( ), array( - "empty" => array(), + "empty" => "", "foo" => array( "@attributes" => array( "empty" => "" From bc4e30bc64cc59b22a42af93bc70b5ad6f635f6d Mon Sep 17 00:00:00 2001 From: Adrien Cahen Date: Sun, 24 May 2020 12:46:04 -0700 Subject: [PATCH 16/21] fix indentation --- src/Helper.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Helper.php b/src/Helper.php index 94b10a8..25e054d 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -36,9 +36,9 @@ public static function domNodeToArray($node) if (!isset($output[$t])) { $output[$t] = []; } - if (empty($v)) { - $v = ''; - } + if (empty($v)) { + $v = ''; + } $output[$t][] = $v; } elseif ($v || $v === '0') { $output = (string) $v; From 40ae46e188dcd3113cf3c6db77ee4ccf94e24d1b Mon Sep 17 00:00:00 2001 From: Adrien Cahen Date: Sun, 24 May 2020 12:51:26 -0700 Subject: [PATCH 17/21] Update main.yml --- .github/workflows/main.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 463ba7e..109b541 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -1,6 +1,12 @@ name: Integrity check -on: [push] +on: + push: + branches: + - master + pull_request: + branches: + - master jobs: build: @@ -30,3 +36,6 @@ jobs: - name: Check PHPStan rules run: composer phpstan + + - name: Run tests + run: bash tests/test.sh From 776c685b6fd542e76fa0cce2e078e22a35aba4c1 Mon Sep 17 00:00:00 2001 From: Adrien Cahen Date: Sun, 24 May 2020 12:57:09 -0700 Subject: [PATCH 18/21] Update main.yml --- .github/workflows/main.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 109b541..9fada8f 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -38,4 +38,4 @@ jobs: run: composer phpstan - name: Run tests - run: bash tests/test.sh + run: tests/test.sh From 00d04831b14a5d213a7bbd35ca2dc6bd9bf69eab Mon Sep 17 00:00:00 2001 From: gaarf Date: Sun, 24 May 2020 13:04:00 -0700 Subject: [PATCH 19/21] update the test again --- tests/test.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/test.sh b/tests/test.sh index c9e9700..e68c996 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -92,7 +92,8 @@ if ($result == $expected) { } else { prettyPrint('Result', 'FAILURE :-('); prettyPrint('Input', $xmlstr); - prettyPrint('Expected', $expected); - prettyPrint('Output', $result); + prettyPrint('Expected', $expected); + prettyPrint('Output', $result); prettyPrint('Result', 'FAILURE :-('); + exit(1); } From 505b4c9f712ff83892e300fe0d2f00e9551da570 Mon Sep 17 00:00:00 2001 From: gaarf Date: Sun, 24 May 2020 13:06:06 -0700 Subject: [PATCH 20/21] fix le test order --- tests/test.sh | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test.sh b/tests/test.sh index e68c996..663964b 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -36,10 +36,6 @@ EOD; $expected = array( - "@root" => 'tv', - - "@attributes" => array( "type" => "cartoon" ), - "show" => array( array( @@ -82,7 +78,11 @@ $expected = array( ) ) - ) + ), + + "@attributes" => array( "type" => "cartoon" ), + + "@root" => 'tv', ); $result = \Gaarf\XmlToPhp\Convertor::covertToArray($xmlstr); From 0b79cff84aa7340d6bde36e41dbd3d98248927a1 Mon Sep 17 00:00:00 2001 From: gaarf Date: Sun, 24 May 2020 13:14:17 -0700 Subject: [PATCH 21/21] fix 0 case cc @roland-d --- src/Helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Helper.php b/src/Helper.php index 25e054d..a4acbe4 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -36,7 +36,7 @@ public static function domNodeToArray($node) if (!isset($output[$t])) { $output[$t] = []; } - if (empty($v)) { + if (is_array($v) && empty($v)) { $v = ''; } $output[$t][] = $v;