-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAdvancedXmlWriter.php
More file actions
158 lines (135 loc) · 3.31 KB
/
AdvancedXmlWriter.php
File metadata and controls
158 lines (135 loc) · 3.31 KB
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
<?php
namespace Port\Xml;
use Port\Writer;
use Port\Writer\FlushableWriter;
/**
* This writer allows you to write more complex XML from arrays than the default XmlWriter.
*
* The main differences are that nested arrays can be proceed and XML attributes are supported.
* If the key of an array value starts with '@' the value will be used as a XML attribute.
*/
class AdvancedXmlWriter implements Writer, FlushableWriter
{
/**
* @var \XmlWriter
*/
protected $xmlWriter;
/**
* @var string
*/
protected $file;
/**
* @var string
*/
protected $rootElement;
/**
* @var array<string, string>
*/
protected $rootElementAttributes;
/**
* @var string
*/
protected $itemElement;
/**
* @var string
*/
protected $version;
/**
* @var string|null
*/
protected $encoding;
/**
* @param string $file
*/
public function __construct(
\XmlWriter $xmlWriter,
$file,
$rootElement = 'items',
$rootElementAttributes = [],
$itemElement = 'item',
$version = '1.0',
$encoding = null
) {
$this->xmlWriter = $xmlWriter;
$this->file = $file;
$this->rootElement = $rootElement;
$this->rootElementAttributes = $rootElementAttributes;
$this->itemElement = $itemElement;
$this->version = $version;
$this->encoding = $encoding;
}
/**
* {@inheritdoc}
*/
public function prepare()
{
$this->xmlWriter->openUri($this->file);
$this->xmlWriter->startDocument($this->version, $this->encoding);
$this->xmlWriter->startElement($this->rootElement);
foreach($this->rootElementAttributes as $key => $value) {
$this->xmlWriter->writeAttribute($key, $value);
}
}
/**
* {@inheritdoc}
*/
public function writeItem(array $item)
{
$this->writeXmlItem($item, $this->itemElement);
}
/**
* Handle XML write with attributes support.
*
* @param array $item
* @param $elementName
*/
protected function writeXmlItem(array $item, $elementName)
{
$this->xmlWriter->startElement($elementName);
$this->writeAttributes($item);
foreach ($item as $key => $value) {
if (substr($key, 0, 1) === '@') {
continue;
}
if (is_array($value)) {
$this->writeXmlItem($value, $key);
} else {
$this->xmlWriter->writeElement($key, $value);
}
}
$this->xmlWriter->endElement();
}
/**
* Write attributes of item.
*
* @param array $item
*/
protected function writeAttributes(array $item)
{
foreach ($item as $key => $value) {
if (substr($key, 0, 1) !== '@') {
continue;
}
$this->xmlWriter->writeAttribute(
substr($key, 1),
$value
);
}
}
/**
* {@inheritdoc}
*/
public function finish()
{
$this->xmlWriter->endElement();
$this->xmlWriter->endDocument();
$this->flush();
}
/**
* {@inheritdoc}
*/
public function flush()
{
$this->xmlWriter->flush();
}
}