Skip to content

Commit 0288467

Browse files
author
Manokaran Majushan
committed
Get real key updated
1 parent c3c4637 commit 0288467

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

src/ArrayToXml.php

+37-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
<?php
2+
23
namespace Spatie\ArrayToXml;
4+
35
use DOMElement;
46
use DOMDocument;
57
use DOMException;
68
use DOMImplementation;
9+
710
class ArrayToXml
811
{
912
/**
@@ -18,6 +21,7 @@ class ArrayToXml
1821
* @var bool
1922
*/
2023
protected $replaceSpacesByUnderScoresInKeyNames = true;
24+
2125
/**
2226
* Construct a new instance.
2327
*
@@ -34,10 +38,10 @@ public function __construct(array $array, $rootElement = '', $replaceSpacesByUnd
3438
{
3539
$this->document = new DOMDocument($xmlVersion, $xmlEncoding);
3640
$this->replaceSpacesByUnderScoresInKeyNames = $replaceSpacesByUnderScoresInKeyNames;
37-
if ($this->isArrayAllKeySequential($array) && ! empty($array)) {
41+
if ($this->isArrayAllKeySequential($array) && !empty($array)) {
3842
throw new DOMException('Invalid Character Error');
3943
}
40-
if (! empty($docTypeArray)) {
44+
if (!empty($docTypeArray)) {
4145
$docType = $this->createDocType($docTypeArray);
4246
$this->document->appendChild($docType);
4347
}
@@ -64,6 +68,7 @@ public static function convert(array $array, $rootElementName = '', $replaceSpac
6468
$converter = new static($array, $rootElementName, $replaceSpacesByUnderScoresInKeyNames, $xmlEncoding, $xmlVersion, $docTypeArray);
6569
return $converter->toXml();
6670
}
71+
6772
/**
6873
* Return as XML.
6974
*
@@ -73,6 +78,7 @@ public function toXml()
7378
{
7479
return $this->document->saveXML();
7580
}
81+
7682
/**
7783
* Return as DOM object.
7884
*
@@ -82,6 +88,7 @@ public function toDom()
8288
{
8389
return $this->document;
8490
}
91+
8592
/**
8693
* Parse individual element.
8794
*
@@ -91,12 +98,13 @@ public function toDom()
9198
private function convertElement(DOMElement $element, $value)
9299
{
93100
$sequential = $this->isArrayAllKeySequential($value);
94-
if (! is_array($value)) {
101+
if (!is_array($value)) {
95102
$element->nodeValue = htmlspecialchars($value);
96103
return;
97104
}
98105
foreach ($value as $key => $data) {
99-
if (! $sequential) {
106+
$key = $this->getRealKey($key);
107+
if (!$sequential) {
100108
if (($key === '_attributes') || ($key === '@attributes')) {
101109
$this->addAttributes($element, $data);
102110
} elseif ((($key === '_value') || ($key === '@value')) && is_string($data)) {
@@ -113,6 +121,23 @@ private function convertElement(DOMElement $element, $value)
113121
}
114122
}
115123
}
124+
125+
/**
126+
* Get Real key by $.
127+
*
128+
* @param string $key
129+
* @param string $key
130+
* @return bool|string
131+
*/
132+
private function getRealKey($key)
133+
{
134+
$pos = strpos($key, '$');
135+
if ($pos > 0) {
136+
$key = substr($key, 0, $pos);
137+
}
138+
return $key;
139+
}
140+
116141
/**
117142
* Add node.
118143
*
@@ -129,6 +154,7 @@ protected function addNode(DOMElement $element, $key, $value)
129154
$element->appendChild($child);
130155
$this->convertElement($child, $value);
131156
}
157+
132158
/**
133159
* Add collection node.
134160
*
@@ -147,6 +173,7 @@ protected function addCollectionNode(DOMElement $element, $value)
147173
$element->parentNode->appendChild($child);
148174
$this->convertElement($child, $value);
149175
}
176+
150177
/**
151178
* Add sequential node.
152179
*
@@ -165,6 +192,7 @@ protected function addSequentialNode(DOMElement $element, $value)
165192
$child->nodeValue = htmlspecialchars($value);
166193
$element->parentNode->appendChild($child);
167194
}
195+
168196
/**
169197
* Check if array are all sequential.
170198
*
@@ -174,14 +202,15 @@ protected function addSequentialNode(DOMElement $element, $value)
174202
*/
175203
protected function isArrayAllKeySequential($value)
176204
{
177-
if (! is_array($value)) {
205+
if (!is_array($value)) {
178206
return false;
179207
}
180208
if (count($value) <= 0) {
181209
return true;
182210
}
183211
return array_unique(array_map('is_int', array_keys($value))) === [true];
184212
}
213+
185214
/**
186215
* Add attributes.
187216
*
@@ -194,10 +223,11 @@ protected function addAttributes($element, $data)
194223
$element->setAttribute($attrKey, $attrVal);
195224
}
196225
}
226+
197227
/**
198228
* Create the root element.
199229
*
200-
* @param string|array $rootElement
230+
* @param string|array $rootElement
201231
* @return DOMElement
202232
*/
203233
protected function createRootElement($rootElement)
@@ -216,6 +246,7 @@ protected function createRootElement($rootElement)
216246
}
217247
return $element;
218248
}
249+
219250
/**
220251
* Pass in an array of elements to set the doctype of the XML.
221252
* @param $docTypeArray

0 commit comments

Comments
 (0)