XML.php
2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
namespace Lackoxygen\GzCbec\Traits;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection as BaseCollection;
use Lackoxygen\GzCbec\Contract\XMLInterface;
use Lackoxygen\GzCbec\Exception\Exception;
use Lackoxygen\GzCbec\Constant\XML as xmlConstant;
trait XML
{
/**
* @throws Exception
*/
public function toXML(string $head): string
{
if (!$this instanceof BaseCollection) {
throw new Exception('not support');
}
$simpleXML = new \SimpleXMLElement(xmlConstant::FIRST_LINE . $head);
$this->arrayToXML($this->toArray(), $simpleXML);
return $simpleXML->asXML();
}
/**
* @param \SimpleXMLElement $xmlObj
* @param $key
* @param null $value
*/
protected function appendChildToXMl(\SimpleXMLElement $xmlObj, $key, $value = null): \SimpleXMLElement
{
if ($this instanceof XMLInterface) {
$key = $this->getFirstLabelPrefix() . ':' . $key;
}
if (is_null($value)) {
//$xmlObj->addChild($key);
} else {
$xmlObj->addChild($key, $value);
}
return $xmlObj;
}
/**
* @param $array
* @param \SimpleXMLElement $xmlObj
*/
protected function arrayToXML($array, \SimpleXMLElement &$xmlObj)
{
foreach ($array as $key => $value) {
if (is_numeric($key)) {
$key = Arr::get($value, '__last_key');
unset($value['__last_key']);
}
if (is_array($value) || is_object($value)) {
if (Arr::exists($value, 0)) {
foreach ($value as &$item) {
Arr::set($item, '__last_key', $key);
}
$this->appendChildToXMl($xmlObj, $key);
$this->arrayToXML($value, $xmlObj);
} else {
$subNode = $xmlObj->addChild($key);
$this->arrayToXML($value, $subNode);
}
} else {
$this->appendChildToXMl($xmlObj, $key, $value);
}
}
}
}