Skip to content

Commit c44707b

Browse files
committed
added customize <?xml?>
1 parent 29a16de commit c44707b

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

readme.md

+88
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ echo \darkfriend\helpers\Xml::encode($array);
3535
#### Result encode
3636

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

src/Xml.php

+35-1
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 = array())
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 = array())
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 = array())
55+
{
56+
if(!$params) return '';
57+
if(!is_array($params)) {
58+
$params = array();
59+
}
60+
61+
$params = array_merge(
62+
array(
63+
'version' => '1.0',
64+
'encoding' => 'utf-8',
65+
),
66+
$params
67+
);
68+
69+
$attr = array();
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)