|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author tarjei huse |
|
4
|
|
|
* @package midcom.helper |
|
5
|
|
|
* @copyright The Midgard Project, http://www.midgard-project.org |
|
6
|
|
|
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Move midgard objects to and from XML |
|
11
|
|
|
* |
|
12
|
|
|
* @package midcom.helper |
|
13
|
|
|
*/ |
|
14
|
|
|
class midcom_helper_exporter_xml extends midcom_helper_exporter |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Make an array out of some xml. |
|
18
|
|
|
* |
|
19
|
|
|
* Note, the function expects xml like this: |
|
20
|
|
|
* <objecttype><attribute>attribute_value</attribute></objecttype> |
|
21
|
|
|
* But it will not return the objecttype. |
|
22
|
|
|
* |
|
23
|
|
|
* @param string $data xml |
|
24
|
|
|
* @return array with attribute => key values |
|
25
|
|
|
*/ |
|
26
|
5 |
|
public function data2array(string $data) : array |
|
27
|
|
|
{ |
|
28
|
5 |
|
return $this->_xml_to_array(new SimpleXMLIterator($data)); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
5 |
|
private function _xml_to_array(SimpleXMLIterator $sxi) : array |
|
32
|
|
|
{ |
|
33
|
5 |
|
$data = []; |
|
34
|
5 |
|
foreach ($sxi as $key => $val) { |
|
35
|
5 |
|
if ($sxi->hasChildren()) { |
|
36
|
5 |
|
$data[$key] = $this->_xml_to_array($val); |
|
37
|
|
|
} else { |
|
38
|
5 |
|
$val = trim($val); |
|
39
|
|
|
//TODO: This is mainly here for backward-compatibility. Its main effect |
|
40
|
|
|
// is that 0 is replaced by an empty string. The question is: Do we want/need this? |
|
41
|
5 |
|
if (!$val) { |
|
42
|
5 |
|
$val = ''; |
|
43
|
|
|
} |
|
44
|
5 |
|
$data[$key] = trim($val); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
5 |
|
return $data; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Make XML out of an array. |
|
52
|
|
|
* |
|
53
|
|
|
* @param array $array |
|
54
|
|
|
*/ |
|
55
|
76 |
|
public function array2data(array $array, $root_node = 'array', $prefix = '') : string |
|
56
|
|
|
{ |
|
57
|
76 |
|
$data = "{$prefix}<{$root_node}>\n"; |
|
58
|
|
|
|
|
59
|
76 |
|
foreach ($array as $key => $field) { |
|
60
|
76 |
|
if (is_numeric($key)) { |
|
61
|
|
|
$key = 'value'; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
76 |
|
if (is_object($field)) { |
|
65
|
|
|
$data .= $this->object2data($field, "{$prefix} "); |
|
66
|
76 |
|
} elseif (is_array($field)) { |
|
67
|
|
|
$data .= $this->array2data($field, $key, "{$prefix} ") . "\n"; |
|
68
|
76 |
|
} elseif (is_numeric($field) || $field === null || is_bool($field)) { |
|
69
|
76 |
|
$data .= "{$prefix} <{$key}>{$field}</{$key}>\n"; |
|
70
|
|
|
} else { |
|
71
|
|
|
// String |
|
72
|
76 |
|
$data .= "{$prefix} <{$key}><![CDATA[{$field}]]></{$key}>\n"; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
76 |
|
$data .= "{$prefix}</{$root_node}>\n"; |
|
77
|
|
|
|
|
78
|
76 |
|
return $data; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Make XML out of an object. |
|
83
|
|
|
* |
|
84
|
|
|
* @param midcom_core_dbaobject $object |
|
85
|
|
|
*/ |
|
86
|
76 |
|
public function object2data($object, $prefix = '') : string |
|
87
|
|
|
{ |
|
88
|
76 |
|
$arr = $this->object2array($object); |
|
89
|
76 |
|
if (!$arr) { |
|
90
|
|
|
return ''; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
76 |
|
$classname = $this->_get_classname($object); |
|
94
|
|
|
|
|
95
|
76 |
|
$data = "{$prefix}<{$classname}"; |
|
96
|
76 |
|
if (!empty($object->guid)) { |
|
97
|
75 |
|
$data .= " id=\"{$object->id}\" guid=\"{$object->guid}\""; |
|
98
|
|
|
} |
|
99
|
76 |
|
$data .= ">\n"; |
|
100
|
|
|
|
|
101
|
76 |
|
foreach ($arr as $key => $val) { |
|
102
|
76 |
|
if (is_array($val)) { |
|
103
|
76 |
|
$root_node = isset($object->{$key}) ? $this->_get_classname($object->{$key}) : "array"; |
|
104
|
76 |
|
$data .= $this->array2data($val, $root_node, " "); |
|
105
|
76 |
|
} elseif (is_numeric($val) || $val === null || is_bool($val)) { |
|
106
|
76 |
|
$data .= "{$prefix} <{$key}>{$val}</{$key}>\n"; |
|
107
|
|
|
} else { |
|
108
|
76 |
|
$data .= "{$prefix} <{$key}><![CDATA[{$val}]]></{$key}>\n"; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
76 |
|
$data .= "{$prefix}</{$classname}>"; |
|
113
|
|
|
|
|
114
|
76 |
|
return $data; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|