From a679372f5ef017ebc33c1960934d59743695e2bf Mon Sep 17 00:00:00 2001
From: Sergii Gulenok
Date: Fri, 26 Jul 2019 16:42:57 +0300
Subject: [PATCH] migrated DOMDocument to XmlWriter
---
composer.json | 20 +-
src/ArrayToXml.php | 234 -----------------
src/ArrayToXmlFile.php | 248 ++++++++++++++++++
tests/ArrayToXmlTest.php | 92 ++-----
...so_be_set_in_simplexmlelement_style__1.php | 13 +-
...so_be_set_in_simplexmlelement_style__1.php | 13 +-
...oXmlTest__it_accepts_an_xml_version__1.php | 4 +-
...XmlTest__it_can_handle_numeric_keys__1.php | 55 +++-
...XmlTest__it_can_handle_numeric_keys__1.xml | 31 ---
...Test__it_can_handle_numeric_keys__1__1.php | 5 -
...__it_can_handle_values_set_as_cdata__1.php | 13 +-
...__it_can_handle_values_set_as_mixed__1.php | 13 +-
..._in_collection_and_sequential_nodes__1.php | 18 +-
13 files changed, 393 insertions(+), 366 deletions(-)
delete mode 100644 src/ArrayToXml.php
create mode 100644 src/ArrayToXmlFile.php
delete mode 100644 tests/__snapshots__/ArrayToXmlTest__it_can_handle_numeric_keys__1.xml
delete mode 100644 tests/__snapshots__/ArrayToXmlTest__it_can_handle_numeric_keys__1__1.php
diff --git a/composer.json b/composer.json
index 06ccd01..fa3218a 100755
--- a/composer.json
+++ b/composer.json
@@ -1,12 +1,14 @@
{
- "name": "spatie/array-to-xml",
- "description": "Convert an array to xml",
+ "name": "ashenwolf/array-to-xmlfile",
+ "description": "Convert an array to xml file (array-to-xml compatible but uses XmlWriter)",
"keywords": [
"convert",
"array",
- "xml"
+ "xml",
+ "xmlwriter",
+ "file"
],
- "homepage": "https://github.com/spatie/array-to-xml",
+ "homepage": "https://github.com/ashenwolf/array-to-xml",
"license": "MIT",
"authors": [
{
@@ -14,6 +16,12 @@
"email": "freek@spatie.be",
"homepage": "https://murze.be",
"role": "Developer"
+ },
+ {
+ "name": "Sergii Gulenok",
+ "email": "sergii.gulenok@gmail.com",
+ "homepage": "https://okami.tech",
+ "role": "Developer"
}
],
"require": {
@@ -27,12 +35,12 @@
},
"autoload": {
"psr-4": {
- "Spatie\\ArrayToXml\\": "src"
+ "Ashenwolf\\ArrayToXml\\": "src"
}
},
"autoload-dev": {
"psr-4": {
- "Spatie\\ArrayToXml\\Test\\": "tests"
+ "Ashenwolf\\ArrayToXml\\Test\\": "tests"
}
}
}
diff --git a/src/ArrayToXml.php b/src/ArrayToXml.php
deleted file mode 100644
index e6bb991..0000000
--- a/src/ArrayToXml.php
+++ /dev/null
@@ -1,234 +0,0 @@
-document = new DOMDocument($xmlVersion, $xmlEncoding);
-
- if (! empty($domProperties)) {
- $this->setDomProperties($domProperties);
- }
-
- $this->replaceSpacesByUnderScoresInKeyNames = $replaceSpacesByUnderScoresInKeyNames;
-
- if ($this->isArrayAllKeySequential($array) && ! empty($array)) {
- throw new DOMException('Invalid Character Error');
- }
-
- $root = $this->createRootElement($rootElement);
-
- $this->document->appendChild($root);
-
- $this->convertElement($root, $array);
- }
-
- public function setNumericTagNamePrefix(string $prefix)
- {
- $this->numericTagNamePrefix = $prefix;
- }
-
- public static function convert(
- array $array,
- $rootElement = '',
- bool $replaceSpacesByUnderScoresInKeyNames = true,
- string $xmlEncoding = null,
- string $xmlVersion = '1.0',
- array $domProperties = []
- ) {
- $converter = new static(
- $array,
- $rootElement,
- $replaceSpacesByUnderScoresInKeyNames,
- $xmlEncoding,
- $xmlVersion,
- $domProperties
- );
-
- return $converter->toXml();
- }
-
- public function toXml(): string
- {
- return $this->document->saveXML();
- }
-
- public function toDom(): DOMDocument
- {
- return $this->document;
- }
-
- protected function ensureValidDomProperties(array $domProperties)
- {
- foreach ($domProperties as $key => $value) {
- if (! property_exists($this->document, $key)) {
- throw new Exception($key.' is not a valid property of DOMDocument');
- }
- }
- }
-
- public function setDomProperties(array $domProperties)
- {
- $this->ensureValidDomProperties($domProperties);
-
- foreach ($domProperties as $key => $value) {
- $this->document->{$key} = $value;
- }
-
- return $this;
- }
-
- private function convertElement(DOMElement $element, $value)
- {
- $sequential = $this->isArrayAllKeySequential($value);
-
- if (! is_array($value)) {
- $value = htmlspecialchars($value);
-
- $value = $this->removeControlCharacters($value);
-
- $element->nodeValue = $value;
-
- return;
- }
-
- foreach ($value as $key => $data) {
- if (! $sequential) {
- if (($key === '_attributes') || ($key === '@attributes')) {
- $this->addAttributes($element, $data);
- } elseif ((($key === '_value') || ($key === '@value')) && is_string($data)) {
- $element->nodeValue = htmlspecialchars($data);
- } elseif ((($key === '_cdata') || ($key === '@cdata')) && is_string($data)) {
- $element->appendChild($this->document->createCDATASection($data));
- } elseif ((($key === '_mixed') || ($key === '@mixed')) && is_string($data)) {
- $fragment = $this->document->createDocumentFragment();
- $fragment->appendXML($data);
- $element->appendChild($fragment);
- } elseif ($key === '__numeric') {
- $this->addNumericNode($element, $data);
- } else {
- $this->addNode($element, $key, $data);
- }
- } elseif (is_array($data)) {
- $this->addCollectionNode($element, $data);
- } else {
- $this->addSequentialNode($element, $data);
- }
- }
- }
-
- protected function addNumericNode(DOMElement $element, $value)
- {
- foreach ($value as $key => $item) {
- $this->convertElement($element, [$this->numericTagNamePrefix.$key => $value]);
- }
- }
-
- protected function addNode(DOMElement $element, $key, $value)
- {
- if ($this->replaceSpacesByUnderScoresInKeyNames) {
- $key = str_replace(' ', '_', $key);
- }
-
- $child = $this->document->createElement($key);
- $element->appendChild($child);
- $this->convertElement($child, $value);
- }
-
- protected function addCollectionNode(DOMElement $element, $value)
- {
- if ($element->childNodes->length === 0 && $element->attributes->length === 0) {
- $this->convertElement($element, $value);
-
- return;
- }
-
- $child = $this->document->createElement($element->tagName);
- $element->parentNode->appendChild($child);
- $this->convertElement($child, $value);
- }
-
- protected function addSequentialNode(DOMElement $element, $value)
- {
- if (empty($element->nodeValue) && ! is_numeric($element->nodeValue)) {
- $element->nodeValue = htmlspecialchars($value);
-
- return;
- }
-
- $child = new DOMElement($element->tagName);
- $child->nodeValue = htmlspecialchars($value);
- $element->parentNode->appendChild($child);
- }
-
- protected function isArrayAllKeySequential($value)
- {
- if (! is_array($value)) {
- return false;
- }
-
- if (count($value) <= 0) {
- return true;
- }
-
- if (\key($value) === '__numeric') {
- return false;
- }
-
- return array_unique(array_map('is_int', array_keys($value))) === [true];
- }
-
- protected function addAttributes(DOMElement $element, array $data)
- {
- foreach ($data as $attrKey => $attrVal) {
- $element->setAttribute($attrKey, $attrVal);
- }
- }
-
- protected function createRootElement($rootElement): DOMElement
- {
- if (is_string($rootElement)) {
- $rootElementName = $rootElement ?: 'root';
-
- return $this->document->createElement($rootElementName);
- }
-
- $rootElementName = $rootElement['rootElementName'] ?? 'root';
-
- $element = $this->document->createElement($rootElementName);
-
- foreach ($rootElement as $key => $value) {
- if ($key !== '_attributes' && $key !== '@attributes') {
- continue;
- }
-
- $this->addAttributes($element, $rootElement[$key]);
- }
-
- return $element;
- }
-
- protected function removeControlCharacters(string $value): string
- {
- return preg_replace('/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F]/', '', $value);
- }
-}
diff --git a/src/ArrayToXmlFile.php b/src/ArrayToXmlFile.php
new file mode 100644
index 0000000..3906e1b
--- /dev/null
+++ b/src/ArrayToXmlFile.php
@@ -0,0 +1,248 @@
+writer = new XmlWriter();
+
+ $this->replaceSpacesByUnderScoresInKeyNames = $replaceSpacesByUnderScoresInKeyNames;
+
+ if ($this->isArrayAllKeySequential($array) && ! empty($array)) {
+ throw new DOMException('Invalid Character Error');
+ }
+
+ if ($outputUri) {
+ $this->writer->openUri($outputUri);
+ $this->inMemory = false;
+ }
+ else {
+ $this->writer->openMemory();
+ $this->inMemory = true;
+ }
+
+ $this->writer->setIndent(true);
+
+ $this->data = $array;
+ $this->rootElement = $rootElement;
+ $this->xmlEncoding = $xmlEncoding;
+ $this->xmlVersion = $xmlVersion;
+ $this->nodesToFlush = $nodesToFlush;
+ $this->nodesCount = 0;
+ }
+
+ public function setNumericTagNamePrefix(string $prefix)
+ {
+ $this->numericTagNamePrefix = $prefix;
+ }
+
+ public static function convert(
+ array $array,
+ string $outputUri = null,
+ $rootElement = '',
+ bool $replaceSpacesByUnderScoresInKeyNames = true,
+ string $xmlEncoding = null,
+ string $xmlVersion = '1.0',
+ int $nodesToFlush = 1000
+ ) {
+ $converter = new static(
+ $array,
+ $outputUri,
+ $rootElement,
+ $replaceSpacesByUnderScoresInKeyNames,
+ $xmlEncoding,
+ $xmlVersion
+ );
+
+ return $converter->toXml();
+ }
+
+ public function toXml(): string
+ {
+ $this->writer->startDocument($this->xmlVersion, $this->xmlEncoding);
+
+ $this->createRootElement($this->rootElement, function() {
+ $this->convertElement($this->data);
+ });
+
+ $this->writer->endDocument();
+
+ $result = $this->inMemory ? $this->writer->outputMemory() : $this->writer->flush();
+
+ unset($this->writer);
+
+ return $result;
+ }
+
+ private function convertElement($value, $siblingKey = null)
+ {
+ $sequential = $this->isArrayAllKeySequential($value);
+
+ if (! is_array($value)) {
+ $value = $value;
+
+ $value = $this->removeControlCharacters($value);
+
+ $this->writer->text($value);
+
+ return;
+ }
+
+ if (!$sequential) {
+ if ($value['_attributes'] ?? false) $this->addAttributes($value['_attributes']);
+ if ($value['@attributes'] ?? false) $this->addAttributes($value['@attributes']);
+
+ $nodeValues = array_filter($value, function ($key) { return !in_array($key, ['_attributes', '@attributes']); }, ARRAY_FILTER_USE_KEY);
+ foreach ($nodeValues as $key => $data) {
+ if ((($key === '_value') || ($key === '@value')) && is_string($data)) {
+ $this->writer->text($value[$key]);
+ } elseif ((($key === '_cdata') || ($key === '@cdata')) && is_string($data)) {
+ $this->writer->writeCdata($value[$key]);
+ } elseif ((($key === '_mixed') || ($key === '@mixed')) && is_string($data)) {
+ $this->writer->writeRaw($value[$key]);
+ } elseif ($key === '__numeric') {
+ $this->addNumericNode($data);
+ } else {
+ $this->addNode($key, $data);
+ }
+ }
+ } else {
+ foreach ($value as $key => $data) {
+ if (is_array($data)) {
+ $this->addCollectionNode($data, $siblingKey);
+ }
+ else {
+ $this->addSequentialNode($data, $siblingKey);
+ }
+ }
+ }
+ }
+
+ protected function addNumericNode($value)
+ {
+ foreach ($value as $key => $item) {
+ $this->convertElement([$this->numericTagNamePrefix.$key => $value]);
+ }
+ }
+
+ protected function addNode($key, $value)
+ {
+ if ($this->replaceSpacesByUnderScoresInKeyNames) {
+ $key = str_replace(' ', '_', $key);
+ }
+
+ $sequential = $this->isArrayAllKeySequential($value);
+ if (!$sequential) $this->startElement($key);
+ $this->convertElement($value, $key);
+ if (!$sequential) $this->endElement();
+ }
+
+ protected function addCollectionNode($value, $siblingKey)
+ {
+ $this->startElement($siblingKey);
+ $this->convertElement($value);
+ $this->endElement();
+ }
+
+ protected function addSequentialNode($value, $siblingKey)
+ {
+ $this->startElement($siblingKey);
+ $this->writer->text(htmlspecialchars($value));
+ $this->endElement();
+ }
+
+ protected function isArrayAllKeySequential($value)
+ {
+ if (! is_array($value)) {
+ return false;
+ }
+
+ if (count($value) <= 0) {
+ return true;
+ }
+
+ if (\key($value) === '__numeric') {
+ return false;
+ }
+
+ return array_unique(array_map('is_int', array_keys($value))) === [true];
+ }
+
+ protected function addAttributes(array $data)
+ {
+ foreach ($data as $attrKey => $attrVal) {
+ $this->writer->writeAttribute($attrKey, $attrVal);
+ }
+ }
+
+ protected function createRootElement($rootElement, $innerDocument)
+ {
+ $rootElementName =
+ is_string($rootElement) ? ($rootElement ?: 'root') : ($rootElement['rootElementName'] ?? 'root');
+
+ $this->startElement($rootElementName);
+
+ if (is_array($rootElement)) {
+ $this->addAttributes($rootElement["_attributes"] ?? []);
+ $this->addAttributes($rootElement["@attributes"] ?? []);
+ }
+
+ $innerDocument();
+
+ $this->endElement();
+ }
+
+ protected function removeControlCharacters(string $value): string
+ {
+ return preg_replace('/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F]/', '', $value);
+ }
+
+ protected function endElement()
+ {
+ $this->writer->endElement();
+ if (!$this->inMemory && $this->nodesCount++ > $this->nodesToFlush)
+ {
+ $this->writer->flush();
+ }
+ }
+
+ protected function startElement($name)
+ {
+ try {
+ $result = $this->writer->startElement($name);
+ } catch (exception $e) {
+ throw new DOMException($e->getMessage());
+ }
+ if (!$result)
+ throw new DOMException(error_get_last());
+ }
+}
diff --git a/tests/ArrayToXmlTest.php b/tests/ArrayToXmlTest.php
index 8f84957..f57962e 100644
--- a/tests/ArrayToXmlTest.php
+++ b/tests/ArrayToXmlTest.php
@@ -1,7 +1,7 @@
assertMatchesXmlSnapshot(ArrayToXml::convert($this->testArray));
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert($this->testArray));
}
/** @test */
public function it_can_handle_an_empty_array()
{
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert([]));
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert([]));
}
/** @test */
public function it_can_receive_name_for_the_root_element()
{
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert([], 'helloyouluckpeople'));
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert([], null, 'helloyouluckpeople'));
}
/** @test */
public function it_can_receive_name_from_array_for_the_root_element()
{
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert([], [
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert([], null, [
'rootElementName' => 'helloyouluckpeople',
]));
}
@@ -57,7 +57,7 @@ public function it_can_receive_name_from_array_for_the_root_element()
/** @test */
public function it_can_convert_attributes_to_xml_for_the_root_element()
{
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert([], [
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert([], null, [
'_attributes' => [
'xmlns' => 'https://github.com/spatie/array-to-xml',
],
@@ -67,7 +67,7 @@ public function it_can_convert_attributes_to_xml_for_the_root_element()
/** @test */
public function and_root_element_attributes_can_also_be_set_in_simplexmlelement_style()
{
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert([], [
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert([], null,[
'@attributes' => [
'xmlns' => 'https://github.com/spatie/array-to-xml',
],
@@ -79,7 +79,7 @@ public function it_throws_an_exception_when_converting_an_array_with_no_keys()
{
$this->expectException('DOMException');
- ArrayToXml::convert(['one', 'two', 'three']);
+ ArrayToXmlFile::convert(['one', 'two', 'three']);
}
/** @test */
@@ -87,7 +87,7 @@ public function it_throws_an_exception_when_converting_an_array_with_invalid_cha
{
$this->expectException('DOMException');
- echo ArrayToXml::convert(['tom & jerry' => 'cartoon characters'], '', false);
+ echo ArrayToXmlFile::convert(['tom & jerry' => 'cartoon characters'], '', false);
}
/** @test */
@@ -95,13 +95,13 @@ public function it_will_raise_an_exception_when_spaces_should_not_be_replaced_an
{
$this->expectException('DOMException');
- ArrayToXml::convert($this->testArray, '', false);
+ ArrayToXmlFile::convert($this->testArray, null, '', false);
}
/** @test */
public function it_can_handle_values_as_basic_collection()
{
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert([
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert([
'user' => ['one', 'two', 'three'],
]));
}
@@ -109,7 +109,7 @@ public function it_can_handle_values_as_basic_collection()
/** @test */
public function it_can_handle_zero_values_in_beginning_of_basic_collection()
{
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert([
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert([
'user' => ['0', '1', '0'],
]));
}
@@ -117,19 +117,19 @@ public function it_can_handle_zero_values_in_beginning_of_basic_collection()
/** @test */
public function it_accepts_an_xml_encoding_type()
{
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert([], '', false, 'UTF-8'));
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert([], null, '', false, 'UTF-8'));
}
/** @test */
public function it_accepts_an_xml_version()
{
- $this->assertMatchesSnapshot(ArrayToXml::convert([], '', false, null, '1.1'));
+ $this->assertMatchesSnapshot(ArrayToXmlFile::convert([], null, '', false, null, '1.1'));
}
/** @test */
public function it_can_handle_values_as_collection()
{
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert([
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert([
'user' => [
[
'name' => 'een',
@@ -146,13 +146,13 @@ public function it_can_handle_values_as_collection()
/** @test */
public function it_can_handle_values_with_special_characters()
{
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert(['name' => 'this & that']));
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert(['name' => 'this & that']));
}
/** @test */
public function it_can_handle_values_with_special_control_characters()
{
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert(['name' => "i want tothis and \x03 that"]));
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert(['name' => "i want tothis and \x03 that"]));
}
/**
@@ -160,7 +160,7 @@ public function it_can_handle_values_with_special_control_characters()
*/
public function it_can_group_by_values_when_values_are_in_a_numeric_array()
{
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert(['user' => ['foo', 'bar']]));
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert(['user' => ['foo', 'bar']]));
}
/** @test */
@@ -170,13 +170,13 @@ public function it_can_convert_attributes_to_xml()
$withAttributes['Good guy']['_attributes'] = ['nameType' => 1];
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert($withAttributes));
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert($withAttributes));
}
/** @test */
public function it_can_handle_attributes_as_collection()
{
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert([
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert([
'user' => [
[
'_attributes' => [
@@ -201,13 +201,13 @@ public function and_attributes_also_can_be_set_in_simplexmlelement_style()
$withAttributes['Good guy']['@attributes'] = ['nameType' => 1];
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert($withAttributes));
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert($withAttributes));
}
/** @test */
public function it_can_handle_values_set_with_attributes_with_special_characters()
{
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert([
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert([
'movie' => [
[
'title' => [
@@ -228,7 +228,7 @@ public function it_can_handle_values_set_with_attributes_with_special_characters
/** @test */
public function and_value_also_can_be_set_in_simplexmlelement_style()
{
- $this->assertMatchesXmlSnapshot(ArrayToXml::convert([
+ $this->assertMatchesXmlSnapshot(ArrayToXmlFile::convert([
'movie' => [
[
'title' => [
@@ -249,7 +249,7 @@ public function and_value_also_can_be_set_in_simplexmlelement_style()
/** @test */
public function it_can_handle_values_set_as_cdata()
{
- $this->assertMatchesSnapshot(ArrayToXml::convert([
+ $this->assertMatchesSnapshot(ArrayToXmlFile::convert([
'movie' => [
[
'title' => [
@@ -270,7 +270,7 @@ public function it_can_handle_values_set_as_cdata()
/** @test */
public function and_cdata_values_can_also_be_set_in_simplexmlelement_style()
{
- $this->assertMatchesSnapshot(ArrayToXml::convert([
+ $this->assertMatchesSnapshot(ArrayToXmlFile::convert([
'movie' => [
[
'title' => [
@@ -291,7 +291,7 @@ public function and_cdata_values_can_also_be_set_in_simplexmlelement_style()
/** @test */
public function it_doesnt_pollute_attributes_in_collection_and_sequential_nodes()
{
- $this->assertMatchesSnapshot(ArrayToXml::convert([
+ $this->assertMatchesSnapshot(ArrayToXmlFile::convert([
'books' => [
'book' => [
['name' => 'A', '@attributes' => ['z' => 1]],
@@ -302,21 +302,10 @@ public function it_doesnt_pollute_attributes_in_collection_and_sequential_nodes(
]));
}
- /** @test */
- public function it_can_convert_array_to_dom()
- {
- $resultDom = (new ArrayToXml($this->testArray))->toDom();
-
- $this->assertSame('Luke Skywalker', $resultDom->getElementsByTagName('name')->item(0)->nodeValue);
- $this->assertSame('Sauron', $resultDom->getElementsByTagName('name')->item(1)->nodeValue);
- $this->assertSame('Lightsaber', $resultDom->getElementsByTagName('weapon')->item(0)->nodeValue);
- $this->assertSame('Evil Eye', $resultDom->getElementsByTagName('weapon')->item(1)->nodeValue);
- }
-
/** @test */
public function it_can_handle_values_set_as_mixed()
{
- $this->assertMatchesSnapshot(ArrayToXml::convert([
+ $this->assertMatchesSnapshot(ArrayToXmlFile::convert([
'movie' => [
[
'title' => [
@@ -337,7 +326,7 @@ public function it_can_handle_values_set_as_mixed()
/** @test */
public function and_mixed_values_can_also_be_set_in_simplexmlelement_style()
{
- $this->assertMatchesSnapshot(ArrayToXml::convert([
+ $this->assertMatchesSnapshot(ArrayToXmlFile::convert([
'movie' => [
[
'title' => [
@@ -358,7 +347,7 @@ public function and_mixed_values_can_also_be_set_in_simplexmlelement_style()
/** @test */
public function it_can_handle_numeric_keys()
{
- $this->assertMatchesSnapshot(ArrayToXml::convert([
+ $this->assertMatchesSnapshot(ArrayToXmlFile::convert([
'__numeric' => [
16 => [
'parent' => 'aaa',
@@ -393,27 +382,4 @@ public function it_can_handle_numeric_keys()
],
]));
}
-
- /** @test */
- public function setting_invalid_properties_will_result_in_an_exception()
- {
- $this->expectException(\Exception::class);
- $xml2Array = new ArrayToXml($this->testArray);
-
- $xml2Array->setDomProperties(['foo' => 'bar']);
- }
-
- /** @test */
- public function it_can_set_dom_properties()
- {
- $xml2Array = new ArrayToXml($this->testArray);
- $xml2Array->setDomProperties([
- 'formatOutput' => true,
- 'version' => '1234567',
- ]);
-
- $dom = $xml2Array->toDom();
- $this->assertTrue($dom->formatOutput);
- $this->assertEquals('1234567', $dom->version);
- }
}
diff --git a/tests/__snapshots__/ArrayToXmlTest__and_cdata_values_can_also_be_set_in_simplexmlelement_style__1.php b/tests/__snapshots__/ArrayToXmlTest__and_cdata_values_can_also_be_set_in_simplexmlelement_style__1.php
index 404ef01..01112f9 100644
--- a/tests/__snapshots__/ArrayToXmlTest__and_cdata_values_can_also_be_set_in_simplexmlelement_style__1.php
+++ b/tests/__snapshots__/ArrayToXmlTest__and_cdata_values_can_also_be_set_in_simplexmlelement_style__1.php
@@ -1,5 +1,10 @@
-
-STAR WARS
]]>tom & jerry]]>
+
+
+
+ STAR WARS]]>
+
+
+ tom & jerry]]>
+
+
';
diff --git a/tests/__snapshots__/ArrayToXmlTest__and_mixed_values_can_also_be_set_in_simplexmlelement_style__1.php b/tests/__snapshots__/ArrayToXmlTest__and_mixed_values_can_also_be_set_in_simplexmlelement_style__1.php
index 1e746ec..e13b6b7 100644
--- a/tests/__snapshots__/ArrayToXmlTest__and_mixed_values_can_also_be_set_in_simplexmlelement_style__1.php
+++ b/tests/__snapshots__/ArrayToXmlTest__and_mixed_values_can_also_be_set_in_simplexmlelement_style__1.php
@@ -1,5 +1,10 @@
-
-STAR WARS Figure 1ROBOCOP Figure 2
+
+
+
+ STAR WARS Figure 1
+
+
+ ROBOCOP Figure 2
+
+
';
diff --git a/tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_version__1.php b/tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_version__1.php
index 106de26..9c72833 100644
--- a/tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_version__1.php
+++ b/tests/__snapshots__/ArrayToXmlTest__it_accepts_an_xml_version__1.php
@@ -1,5 +1,3 @@
-
+
';
diff --git a/tests/__snapshots__/ArrayToXmlTest__it_can_handle_numeric_keys__1.php b/tests/__snapshots__/ArrayToXmlTest__it_can_handle_numeric_keys__1.php
index 067aa51..a7e1878 100644
--- a/tests/__snapshots__/ArrayToXmlTest__it_can_handle_numeric_keys__1.php
+++ b/tests/__snapshots__/ArrayToXmlTest__it_can_handle_numeric_keys__1.php
@@ -1,5 +1,52 @@
-
-aaa3abc3bb3abb3abc3acb3aaa3abc3bb3abb3abc3acb3
+
+
+
+ aaa
+ 3
+
+ abc
+ 3
+
+
+
+ bb
+ 3
+
+ abb
+ 3
+
+ abc
+ 3
+
+
+
+ acb
+ 3
+
+
+
+ aaa
+ 3
+
+ abc
+ 3
+
+
+
+ bb
+ 3
+
+ abb
+ 3
+
+ abc
+ 3
+
+
+
+ acb
+ 3
+
+
+
';
diff --git a/tests/__snapshots__/ArrayToXmlTest__it_can_handle_numeric_keys__1.xml b/tests/__snapshots__/ArrayToXmlTest__it_can_handle_numeric_keys__1.xml
deleted file mode 100644
index 1175728..0000000
--- a/tests/__snapshots__/ArrayToXmlTest__it_can_handle_numeric_keys__1.xml
+++ /dev/null
@@ -1,31 +0,0 @@
-
-
-
- aaa
- 3
-
- abc
- 3
-
-
-
- bb
- 3
-
-
- abb
- 3
-
-
- abc
- 3
-
-
-
-
- acb
- 3
-
-
-
-
diff --git a/tests/__snapshots__/ArrayToXmlTest__it_can_handle_numeric_keys__1__1.php b/tests/__snapshots__/ArrayToXmlTest__it_can_handle_numeric_keys__1__1.php
deleted file mode 100644
index 067aa51..0000000
--- a/tests/__snapshots__/ArrayToXmlTest__it_can_handle_numeric_keys__1__1.php
+++ /dev/null
@@ -1,5 +0,0 @@
-
-aaa3abc3bb3abb3abc3acb3aaa3abc3bb3abb3abc3acb3
-';
diff --git a/tests/__snapshots__/ArrayToXmlTest__it_can_handle_values_set_as_cdata__1.php b/tests/__snapshots__/ArrayToXmlTest__it_can_handle_values_set_as_cdata__1.php
index 404ef01..01112f9 100644
--- a/tests/__snapshots__/ArrayToXmlTest__it_can_handle_values_set_as_cdata__1.php
+++ b/tests/__snapshots__/ArrayToXmlTest__it_can_handle_values_set_as_cdata__1.php
@@ -1,5 +1,10 @@
-
-STAR WARS]]>tom & jerry]]>
+
+
+
+ STAR WARS]]>
+
+
+ tom & jerry]]>
+
+
';
diff --git a/tests/__snapshots__/ArrayToXmlTest__it_can_handle_values_set_as_mixed__1.php b/tests/__snapshots__/ArrayToXmlTest__it_can_handle_values_set_as_mixed__1.php
index 1e746ec..e13b6b7 100644
--- a/tests/__snapshots__/ArrayToXmlTest__it_can_handle_values_set_as_mixed__1.php
+++ b/tests/__snapshots__/ArrayToXmlTest__it_can_handle_values_set_as_mixed__1.php
@@ -1,5 +1,10 @@
-
-STAR WARS Figure 1ROBOCOP Figure 2
+
+
+
+ STAR WARS Figure 1
+
+
+ ROBOCOP Figure 2
+
+
';
diff --git a/tests/__snapshots__/ArrayToXmlTest__it_doesnt_pollute_attributes_in_collection_and_sequential_nodes__1.php b/tests/__snapshots__/ArrayToXmlTest__it_doesnt_pollute_attributes_in_collection_and_sequential_nodes__1.php
index b8ef10b..d22b98d 100644
--- a/tests/__snapshots__/ArrayToXmlTest__it_doesnt_pollute_attributes_in_collection_and_sequential_nodes__1.php
+++ b/tests/__snapshots__/ArrayToXmlTest__it_doesnt_pollute_attributes_in_collection_and_sequential_nodes__1.php
@@ -1,5 +1,15 @@
-
-ABC
+
+
+
+
+ A
+
+
+ B
+
+
+ C
+
+
+
';