File tree 2 files changed +122
-1
lines changed
2 files changed +122
-1
lines changed Original file line number Diff line number Diff line change 92
92
)
93
93
)
94
94
)
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 >
95
182
```
Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ class Xml
18
18
* @param array $params = [
19
19
* 'root' => '<root/>',
20
20
* 'exception' => false,
21
+ * 'header' => false,
21
22
* ]
22
23
* @return string
23
24
* @throws XmlException
@@ -29,8 +30,13 @@ public static function encode($data, $params = [])
29
30
if (empty ($ params ['root ' ])) {
30
31
$ params ['root ' ] = self ::$ root ;
31
32
}
33
+ if (!isset ($ params ['header ' ])) {
34
+ $ params ['header ' ] = false ;
35
+ }
32
36
33
- $ xml = new SimpleXMLElement ($ params ['root ' ]);
37
+ $ xml = new SimpleXMLElement (
38
+ self ::getHeader ($ params ['header ' ]).$ params ['root ' ]
39
+ );
34
40
$ xml = self ::generateXml ($ xml , $ data );
35
41
36
42
if (!static ::checkException ($ params )) {
@@ -40,6 +46,34 @@ public static function encode($data, $params = [])
40
46
return $ xml ->asXML ();
41
47
}
42
48
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
+
43
77
/**
44
78
* @param SimpleXMLElement $xml
45
79
* @param mixed $data
You can’t perform that action at this time.
0 commit comments