Skip to content

Commit

Permalink
Add conversion option
Browse files Browse the repository at this point in the history
  • Loading branch information
alextartan committed Sep 29, 2019
1 parent fd5b4d7 commit 1da822f
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 24 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ Convert an XML (either DOMDocument or string) to an array

// default value:
$config = [
'version' => '1.0',
'encoding' => 'UTF-8',
'attributesKey' => '@attributes',
'cdataKey' => '@cdata',
'valueKey' => '@value',
'useNamespaces' => false,
'version' => '1.0',
'encoding' => 'UTF-8',
'attributesKey' => '@attributes',
'cdataKey' => '@cdata',
'valueKey' => '@value',
'useNamespaces' => false,
'forceOneElementArray => false,
];

$xtoa = new XmlToArray($config);
Expand Down
20 changes: 16 additions & 4 deletions src/ArrayToXml/XmlToArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@
use DOMElement;
use DOMNode;
use LibXMLError;
use function array_key_exists;
use function is_array;
use function is_string;
use function libxml_clear_errors;
use function libxml_get_last_error;
use function libxml_use_internal_errors;
use function trim;
use const XML_CDATA_SECTION_NODE;
use const XML_ELEMENT_NODE;
use const XML_TEXT_NODE;

/**
* This class helps convert an XML to an array
Expand Down Expand Up @@ -190,10 +200,12 @@ private function normalizeValues($output)
return $output;
}

// if only one node of its kind, assign it directly instead if array($value);
foreach ($output as $key => $value) {
if (is_array($value) && count($value) === 1) {
$output[$key] = $value[0];
if (!$this->config->isForceOneElementArray()) {
// if only one node of its kind, assign it directly instead if array($value);
foreach ($output as $key => $value) {
if (is_array($value) && count($value) === 1) {
$output[$key] = $value[0];
}
}
}

Expand Down
39 changes: 25 additions & 14 deletions src/ArrayToXml/XmlToArrayConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@

namespace AlexTartan\Array2Xml;

use function array_merge;

final class XmlToArrayConfig
{
private const DEFAULTS = [
'version' => '1.0',
'encoding' => 'UTF-8',
'attributesKey' => '@attributes',
'cdataKey' => '@cdata',
'valueKey' => '@value',
'useNamespaces' => false,
'version' => '1.0',
'encoding' => 'UTF-8',
'attributesKey' => '@attributes',
'cdataKey' => '@cdata',
'valueKey' => '@value',
'useNamespaces' => false,
'forceOneElementArray' => false,
];

/** @var string */
Expand All @@ -38,14 +41,16 @@ private function __construct(
string $attributesKey,
string $cdataKey,
string $valueKey,
bool $useNamespaces
bool $useNamespaces,
bool $forceOneElementArray
) {
$this->version = $version;
$this->encoding = $encoding;
$this->attributesKey = $attributesKey;
$this->cdataKey = $cdataKey;
$this->valueKey = $valueKey;
$this->useNamespaces = $useNamespaces;
$this->version = $version;
$this->encoding = $encoding;
$this->attributesKey = $attributesKey;
$this->cdataKey = $cdataKey;
$this->valueKey = $valueKey;
$this->useNamespaces = $useNamespaces;
$this->forceOneElementArray = $forceOneElementArray;
}

public static function fromArray(array $configData = []): self
Expand All @@ -58,7 +63,8 @@ public static function fromArray(array $configData = []): self
$config['attributesKey'],
$config['cdataKey'],
$config['valueKey'],
(bool)$config['useNamespaces']
(bool)$config['useNamespaces'],
(bool)$config['forceOneElementArray']
);
}

Expand Down Expand Up @@ -91,4 +97,9 @@ public function isUseNamespaces(): bool
{
return $this->useNamespaces;
}

public function isForceOneElementArray(): bool
{
return $this->forceOneElementArray;
}
}
31 changes: 31 additions & 0 deletions test/ArrayToXml/XmlToArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use AlexTartan\Array2Xml\XmlToArray;
use DOMDocument;
use PHPUnit\Framework\TestCase;
use function implode;

final class XmlToArrayTest extends TestCase
{
Expand Down Expand Up @@ -307,4 +308,34 @@ public function testXmlWithEmptyNodes(): void
$output
);
}

public function testXmlWithForceOneElementNodes(): void
{
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->loadXML(
implode(
'',
[
'<table>',
'<name>sd</name>',
'<width>80</width>',
'<length>120</length>',
'</table>',
]
)
);

$output = (new XmlToArray(['forceOneElementArray' => true]))->buildArrayFromDomDocument($doc);

self::assertSame(
[
'table' => [
'name' => ['sd'],
'width' => ['80'],
'length' => ['120'],
],
],
$output
);
}
}

0 comments on commit 1da822f

Please sign in to comment.