-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray2xml.php
461 lines (407 loc) · 9.61 KB
/
array2xml.php
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
<?php
/**
* Array -> XML Converter Class
* Convert array to clean XML
*
* @category Libraries
* @author Anton Vasylyev
* @link http://truecoder.name
* @version 1.3
*/
class Array2xml
{
private $writer;
private $version = '1.0';
private $encoding = 'UTF-8';
private $rootName = 'root';
private $rootAttrs = array(); //example: array('first_attr' => 'value_of_first_attr', 'second_atrr' => 'etc');
private $rootSelf = FALSE;
private $elementAttrs = array(); //example: $attrs['element_name'][] = array('attr_name' => 'attr_value');
private $CDataKeys = array();
private $newLine = "\n";
private $newTab = "\t";
private $numericTagPrefix = 'key';
private $skipNumeric = TRUE;
private $_tabulation = TRUE; //TODO
private $defaultTagName = FALSE; //Tag For Numeric Array Keys
private $rawKeys = array();
private $emptyElementSyntax = 1;
private $filterNumbers = FALSE;
private $tagsToFilter = array();
const EMPTY_SELF_CLOSING = 1;
const EMPTY_FULL = 2;
/**
* Constructor
* Load Standard PHP Class XMLWriter and path it to variable
*
* @access public
* @param array $params
*/
public function __construct($params = array())
{
if (is_array($params) and !empty($params))
{
foreach ($params as $key => $param)
{
$attr = '_' . $key;
if (property_exists($this, $attr))
{
$this->$attr = $param;
}
}
}
$this->writer = new \XMLWriter();
}
// --------------------------------------------------------------------
/**
* Converter
* Convert array data to XML. Last method to call
*
* @access public
* @param array
* @return string
*/
public function convert(array $data)
{
$this->writer->openMemory();
$this->writer->startDocument($this->version, $this->encoding);
$this->writer->startElement($this->rootName);
if (!empty($this->rootAttrs) and is_array($this->rootAttrs))
{
foreach ($this->rootAttrs as $rootAttrName => $rootAttrText)
{
$this->writer->writeAttribute($rootAttrName, $rootAttrText);
}
}
if ($this->rootSelf === FALSE)
{
$this->writer->text($this->newLine);
if (is_array($data) AND !empty($data))
{
$this->_getXML($data);
}
}
$this->writer->endElement();
return $this->writer->outputMemory();
}
// --------------------------------------------------------------------
/**
* Set XML Document Version
*
* @access public
* @param string
* @return void
*/
public function setVersion($version)
{
$this->version = (string)$version;
}
// --------------------------------------------------------------------
/**
* Set Encoding
*
* @access public
* @param string
* @return void
*/
public function setEncoding($encoding)
{
$this->encoding = (string)$encoding;
}
// --------------------------------------------------------------------
/**
* Set XML Root Element Name
*
* @access public
* @param string
* @return void
*/
public function setRootName($rootName)
{
$this->rootName = (string)$rootName;
}
// --------------------------------------------------------------------
/**
* Set XML Root Element Attributes
*
* @access public
* @param array
* @return void
*/
public function setRootAttrs(array $rootAttrs)
{
$this->rootAttrs = $rootAttrs;
}
// --------------------------------------------------------------------
/**
* Set XML Root Self close
*
* @access public
* @param bool
* @return void
*/
public function setRootSelf($rootSelf)
{
$this->rootSelf = (bool)$rootSelf;
}
// --------------------------------------------------------------------
/**
* Set Attributes of XML Elements
*
* @access public
* @param array
* @return void
*/
public function setElementsAttrs(array $elementAttrs)
{
$this->elementAttrs = $elementAttrs;
}
// --------------------------------------------------------------------
/**
* Set keys of array that needed to be as CData in XML document
*
* @access public
* @param array
* @return void
*/
public function setCDataKeys(array $CDataKeys)
{
$this->CDataKeys = $CDataKeys;
}
// --------------------------------------------------------------------
/**
* Set keys of array that needed to be as Raw XML in XML document
*
* @access public
* @param array
* @return void
*/
public function setRawKeys(array $rawKeys)
{
$this->rawKeys = $rawKeys;
}
// --------------------------------------------------------------------
/**
* Set New Line
*
* @access public
* @param string
* @return void
*/
public function setNewLine($newLine)
{
$this->newLine = (string)$newLine;
}
// --------------------------------------------------------------------
/**
* Set New Tab
*
* @access public
* @param string
* @return void
*/
public function setNewTab($newTab)
{
$this->newTab = (string)$newTab;
}
// --------------------------------------------------------------------
/**
* Set Default Numeric Tag Prefix
*
* @access public
* @param string
* @return void
*/
public function setNumericTagPrefix($numericTagPrefix)
{
$this->numericTagPrefix = (string)$numericTagPrefix;
}
// --------------------------------------------------------------------
/**
* On/Off Skip Numeric Array Keys
*
* @access public
* @param string
* @return void
*/
public function setSkipNumeric($skipNumeric)
{
$this->skipNumeric = (bool)$skipNumeric;
}
// --------------------------------------------------------------------
/**
* Tag For Numeric Array Keys
*
* @access public
* @param string
* @return void
*/
public function setDefaultTagName($defaultTagName)
{
$this->defaultTagName = (string)$defaultTagName;
}
// --------------------------------------------------------------------
/**
* Set preferred syntax of empty elements.
* <element></element> or self-closing <element/>
*
* @access public
* @param const Array2xml::EMPTY_SELF_CLOSING or Array2xml::EMPTY_FULL
* @return void
*/
public function setEmptyElementSyntax($syntax)
{
$this->emptyElementSyntax = $syntax;
}
// --------------------------------------------------------------------
/**
* Remove numbers from tag names.
* Useful if you need to have identically named elements in your XML
*
* You can pass either boolean TRUE to remove numbers from ALL tags
* or pass an ARRAY with element names in it(without numbers)
* to filter only specific elements.
*
* @access public
* @param bool|array
* @return void
*/
public function setFilterNumbersInTags($data)
{
if (is_bool($data))
{
$this->filterNumbers = $data;
}
elseif (is_array($data))
{
$this->tagsToFilter = $data;
}
else
{
throw new \InvalidArgumentException('$data must be a type of boolean or array');
}
}
// --------------------------------------------------------------------
/**
* Writing XML document by passing through array
*
* @access private
* @param array
* @param int
* @return void
*/
private function _getXML(&$data, $tabs_count = 0)
{
foreach ($data as $key => $val)
{
unset($data[$key]);
// Skip attribute param
if (substr($key, 0, 1) == '@')
{
continue;
}
if (is_numeric($key))
{
if ($this->defaultTagName !== FALSE)
{
$key = $this->defaultTagName;
}
elseif ($this->skipNumeric === TRUE)
{
if (!is_array($val))
{
$tabs_count = 0;
}
else
{
if ($tabs_count > 0)
{
$tabs_count--;
}
}
continue;
}
else
{
$key = $this->numericTagPrefix . $key;
}
}
if ($this->filterNumbers === TRUE || in_array(preg_replace('#[0-9]*#', '', $key), $this->tagsToFilter))
{
// Remove numbers
$key = preg_replace('#[0-9]*#', '', $key);
}
if ($key !== FALSE)
{
$this->writer->text(str_repeat($this->newTab, $tabs_count));
// Write element tag name
$this->writer->startElement($key);
// Check if there are some attributes
if (isset($this->elementAttrs[$key]) || isset($val['@attributes']))
{
if (isset($val['@attributes']) && is_array($val['@attributes']))
{
$attributes = $val['@attributes'];
}
else
{
$attributes = $this->elementAttrs[$key];
}
// Yeah, lets add them
foreach ($attributes as $elementAttrName => $elementAttrText)
{
$this->writer->startAttribute($elementAttrName);
$this->writer->text($elementAttrText);
$this->writer->endAttribute();
}
if ( ! empty($val['@content']) && is_string($val['@content']) && isset($val['@attributes']))
{
$val = $val['@content'];
}
}
}
if (is_array($val))
{
if ($key !== FALSE)
{
$this->writer->text($this->newLine);
}
$tabs_count++;
$this->_getXML($val, $tabs_count);
$tabs_count--;
if ($key !== FALSE)
{
$this->writer->text(str_repeat($this->newTab, $tabs_count));
}
}
else
{
if ($val != NULL || $val === 0)
{
if (isset($this->CDataKeys[$key]) || array_search($key, $this->CDataKeys) !== FALSE)
{
$this->writer->writeCData($val);
}
elseif (array_search($key, $this->rawKeys) !== FALSE)
{
$this->writer->writeRaw($val);
}
else
{
$this->writer->text($val);
}
}
elseif ($this->emptyElementSyntax === self::EMPTY_FULL)
{
$this->writer->text('');
}
}
if ($key !== FALSE)
{
$this->writer->endElement();
$this->writer->text($this->newLine);
}
}
}
}
//END Array to Xml Class