From 87a90a84481c647239739a5845b787ae30bfbd6a Mon Sep 17 00:00:00 2001
From: Vuong V <3781991+denormalizer@users.noreply.github.com>
Date: Wed, 4 Sep 2019 18:42:17 +1000
Subject: [PATCH 1/3] Add ability to export XML to array or object (stdClass)
---
source/FluidXml/FluidXml.php | 32 ++++++++++++++++++++++++++++++++
support/composer.json | 3 ++-
2 files changed, 34 insertions(+), 1 deletion(-)
diff --git a/source/FluidXml/FluidXml.php b/source/FluidXml/FluidXml.php
index 5e05c07..bae6f3b 100644
--- a/source/FluidXml/FluidXml.php
+++ b/source/FluidXml/FluidXml.php
@@ -2,6 +2,9 @@
namespace FluidXml;
+// PEAR
+use XML_Unserializer;
+
/**
* @method array array()
* @method FluidXml namespace(...$arguments)
@@ -174,6 +177,35 @@ public function html($strip = false)
return "{$header}{$html}";
}
+ function toArray($strip = false)
+ {
+ $xml = $this->xml($strip);
+ $unserializer = $this->xmlUnserializer('array');
+ $unserializer->unserialize($xml);
+
+ return $unserializer->getUnserializedData();
+ }
+
+ function toObject($strip = false)
+ {
+ $xml = $this->xml($strip);
+ $unserializer = $this->xmlUnserializer('object');
+ $unserializer->unserialize($xml);
+
+ return $unserializer->getUnserializedData();
+ }
+
+ protected function xmlUnserializer($complexType)
+ {
+ $options = array(
+ 'parseAttributes' => true,
+ 'prependAttributes' => '@',
+ 'complexType' => $complexType
+ );
+
+ return new XML_Unserializer($options);
+ }
+
public function namespaces()
{
return $this->document->namespaces;
diff --git a/support/composer.json b/support/composer.json
index b26628a..13e1ce4 100644
--- a/support/composer.json
+++ b/support/composer.json
@@ -23,7 +23,8 @@
}
},
"require": {
- "php": ">=5.6"
+ "php": ">=5.6",
+ "pear/xml_serializer": "^0.22.0"
},
"require-dev": {
"peridot-php/peridot": "1.*",
From dd867d8ce79d9454b7fac2fe74ae7ea258dbacf1 Mon Sep 17 00:00:00 2001
From: Vuong V <3781991+denormalizer@users.noreply.github.com>
Date: Thu, 5 Sep 2019 18:03:10 +1000
Subject: [PATCH 2/3] Consolidate export function into one function. Also to
remove ambiguity between toArray() and array()
---
source/FluidXml/FluidXml.php | 15 +++------------
1 file changed, 3 insertions(+), 12 deletions(-)
diff --git a/source/FluidXml/FluidXml.php b/source/FluidXml/FluidXml.php
index bae6f3b..32fddf8 100644
--- a/source/FluidXml/FluidXml.php
+++ b/source/FluidXml/FluidXml.php
@@ -176,20 +176,11 @@ public function html($strip = false)
return "{$header}{$html}";
}
-
- function toArray($strip = false)
- {
- $xml = $this->xml($strip);
- $unserializer = $this->xmlUnserializer('array');
- $unserializer->unserialize($xml);
-
- return $unserializer->getUnserializedData();
- }
-
- function toObject($strip = false)
+
+ function exportTo($complexType, $strip = false)
{
$xml = $this->xml($strip);
- $unserializer = $this->xmlUnserializer('object');
+ $unserializer = $this->xmlUnserializer($complexType);
$unserializer->unserialize($xml);
return $unserializer->getUnserializedData();
From 74dde5b826eab8e1f953f61334d46262e7dcb44f Mon Sep 17 00:00:00 2001
From: Vuong V <3781991+denormalizer@users.noreply.github.com>
Date: Thu, 5 Sep 2019 18:03:38 +1000
Subject: [PATCH 3/3] Add unit tests
---
specs/FluidXml.php | 100 +++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 100 insertions(+)
diff --git a/specs/FluidXml.php b/specs/FluidXml.php
index 6cac1ef..2abf6ea 100644
--- a/specs/FluidXml.php
+++ b/specs/FluidXml.php
@@ -2318,6 +2318,106 @@ function addchild($parent, $i)
});
});
+ describe('.exportTo()', function () {
+ it('should return an array', function () {
+
+ $xmlStr = "\n"
+ . " Herbivore\n"
+ . " \n"
+ . " 4\n"
+ . " 1\n"
+ . " \n"
+ . "";
+
+ $xml = new FluidXml();
+ $xml->addChild($xmlStr);
+ $arr = $xml->exportTo("array");
+
+ $actual = is_array($arr);
+ $expected = false;
+
+ \assert($actual === $expected, __($actual, $expected));
+ });
+
+ it('should return an array in the correct format', function () {
+
+ $xmlStr = "\n"
+ . " Herbivore\n"
+ . " \n"
+ . " 4\n"
+ . " 1\n"
+ . " \n"
+ . "";
+
+ $xml = new FluidXml();
+ $xml->addChild($xmlStr);
+ $actual = $xml->exportTo('array');
+
+ $array = $xml->array();
+
+ $expected = array (
+ 'animal' =>
+ array (
+ '@xmlns:foo' => 'http://namespace.foo',
+ '@operation' => 'create',
+ 'type' => 'Herbivore',
+ 'attribute' =>
+ array (
+ '@xmlns:foo' => 'http://namespace.foo',
+ 'foo:legs' => '4',
+ 'foo:head' => '1',
+ ),
+ ),
+ );
+
+ \assert($actual === $expected, __($actual, $expected));
+ });
+
+ it('should return an stdClass Object', function () {
+
+ $xmlStr = "\n"
+ . " Herbivore\n"
+ . " \n"
+ . " 4\n"
+ . " 1\n"
+ . " \n"
+ . "";
+
+ $xml = new FluidXml();
+ $xml->addChild($xmlStr);
+ $object = $xml->exportTo('object');
+
+ \assert_is_a($object, '\stdClass');
+ });
+
+ it('should return a stdClass object in the correct format', function () {
+
+ $xmlStr = "\n"
+ . " Herbivore\n"
+ . " \n"
+ . " 4\n"
+ . " 1\n"
+ . " \n"
+ . "";
+
+ $xml = new FluidXml();
+ $xml->addChild($xmlStr);
+ $actual = $xml->exportTo('object');
+
+ $expected = new stdClass();
+ $expected->animal = new stdClass();
+ $expected->animal->{'@xmlns:foo'} = 'http://namespace.foo';
+ $expected->animal->{'@operation'} = 'create';
+ $expected->animal->type = 'Herbivore';
+ $expected->animal->attribute = new stdClass();
+ $expected->animal->attribute->{'@xmlns:foo'} = 'http://namespace.foo';
+ $expected->animal->attribute->{'foo:legs'} = '4';
+ $expected->animal->attribute->{'foo:head'} = '2';
+
+ \assert($actual === $expected, __($actual, $expected));
+ });
+ });
+
describe('.save()', function () {
it('should be fluid', function () {
$file = "{$this->out_dir}.test_save0.xml";