Skip to content

Commit 82ff074

Browse files
committed
added customize <?xml?>
1 parent 3d0a1eb commit 82ff074

File tree

2 files changed

+122
-1
lines changed

2 files changed

+122
-1
lines changed

readme.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,91 @@ Array
9292
)
9393
)
9494
)
95+
```
96+
97+
### Custom \<?xml \?> attributes
98+
99+
```php
100+
$array = array(
101+
'bar' => 'value bar',
102+
'foo' => 'value foo',
103+
'der' => array(
104+
'@cdata' => 'this is long text',
105+
'@attributes' => array(
106+
'at1' => 'at1val',
107+
'at2' => 'at2val',
108+
),
109+
),
110+
'qpo' => array(
111+
'sub1' => array('sub2'=>'val')
112+
)
113+
);
114+
115+
echo \darkfriend\helpers\Xml::encode(
116+
$array,
117+
[
118+
'header' => [
119+
'version' => 1.0,
120+
'encoding' => 'utf-8',
121+
],
122+
]
123+
);
124+
```
125+
126+
```xml
127+
<?xml version="1.0" encoding="utf-8"?>
128+
<root>
129+
<bar>value bar</bar>
130+
<foo>value foo</foo>
131+
<der at1="at1val" at2="at2val"><![CDATA[this is long text]]></der>
132+
<qpo>
133+
<sub1>
134+
<sub2>val</sub2>
135+
</sub1>
136+
</qpo>
137+
</root>
138+
```
139+
140+
### Custom root element
141+
142+
```php
143+
$array = array(
144+
'bar' => 'value bar',
145+
'foo' => 'value foo',
146+
'der' => array(
147+
'@cdata' => 'this is long text',
148+
'@attributes' => array(
149+
'at1' => 'at1val',
150+
'at2' => 'at2val',
151+
),
152+
),
153+
'qpo' => array(
154+
'sub1' => array('sub2'=>'val')
155+
)
156+
);
157+
158+
echo \darkfriend\helpers\Xml::encode(
159+
$array,
160+
[
161+
'root' => '<response/>',
162+
'header' => [
163+
'version' => 1.0,
164+
'encoding' => 'utf-8',
165+
],
166+
]
167+
);
168+
```
169+
170+
```xml
171+
<?xml version="1.0" encoding="utf-8"?>
172+
<response>
173+
<bar>value bar</bar>
174+
<foo>value foo</foo>
175+
<der at1="at1val" at2="at2val"><![CDATA[this is long text]]></der>
176+
<qpo>
177+
<sub1>
178+
<sub2>val</sub2>
179+
</sub1>
180+
</qpo>
181+
</response>
95182
```

src/Xml.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class Xml
1818
* @param array $params = [
1919
* 'root' => '<root/>',
2020
* 'exception' => false,
21+
* 'header' => false,
2122
* ]
2223
* @return string
2324
* @throws XmlException
@@ -29,8 +30,13 @@ public static function encode($data, $params = [])
2930
if(empty($params['root'])) {
3031
$params['root'] = self::$root;
3132
}
33+
if(!isset($params['header'])) {
34+
$params['header'] = false;
35+
}
3236

33-
$xml = new SimpleXMLElement($params['root']);
37+
$xml = new SimpleXMLElement(
38+
self::getHeader($params['header']).$params['root']
39+
);
3440
$xml = self::generateXml($xml, $data);
3541

3642
if(!static::checkException($params)) {
@@ -40,6 +46,34 @@ public static function encode($data, $params = [])
4046
return $xml->asXML();
4147
}
4248

49+
/**
50+
* @param array $params header attributes
51+
* @return string
52+
* @since 1.0.1
53+
*/
54+
public static function getHeader($params = [])
55+
{
56+
if(!$params) return '';
57+
if(!is_array($params)) {
58+
$params = [];
59+
}
60+
61+
$params = array_merge(
62+
[
63+
'version' => '1.0',
64+
'encoding' => 'utf-8',
65+
],
66+
$params
67+
);
68+
69+
$attr = [];
70+
foreach ($params as $attrKey=>$attrVal) {
71+
$attr[] = "$attrKey=\"$attrVal\"";
72+
}
73+
74+
return '<?xml '.implode(' ', $attr).'?>';
75+
}
76+
4377
/**
4478
* @param SimpleXMLElement $xml
4579
* @param mixed $data

0 commit comments

Comments
 (0)