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
<?php
declare(strict_types=1);
namespace Laminas\Log\Formatter;
use DateTime;
use DOMDocument;
use DOMElement;
use DOMException;
use Laminas\Escaper\Escaper;
use Laminas\Stdlib\ArrayUtils;
use Traversable;
use function array_key_exists;
use function array_shift;
use function func_get_args;
use function get_class;
use function is_array;
use function is_numeric;
use function is_object;
use function is_scalar;
use function method_exists;
use function preg_replace;
use const PHP_EOL;
class Xml implements FormatterInterface
{
/** @var string Name of root element */
protected $rootElement;
/** @var array Relates XML elements to log data field keys. */
protected $elementMap;
/** @var string Encoding to use in XML */
protected $encoding;
/** @var Escaper instance */
protected $escaper;
/**
* Format specifier for DateTime objects in event data (default: ISO 8601)
*
* @see http://php.net/manual/en/function.date.php
*
* @var string
*/
protected $dateTimeFormat = self::DEFAULT_DATETIME_FORMAT;
/**
* (the default encoding is UTF-8)
*
* @param array|Traversable $options
*/
public function __construct($options = [])
{
if ($options instanceof Traversable) {
$options = ArrayUtils::iteratorToArray($options);
}
if (! is_array($options)) {
$args = func_get_args();
$options = [
'rootElement' => array_shift($args),
];
if (! empty($args)) {
$options['elementMap'] = array_shift($args);
}
if (! empty($args)) {
$options['encoding'] = array_shift($args);
}
if (! empty($args)) {
$options['dateTimeFormat'] = array_shift($args);
}
}
if (! array_key_exists('rootElement', $options)) {
$options['rootElement'] = 'logEntry';
}
if (! array_key_exists('encoding', $options)) {
$options['encoding'] = 'UTF-8';
}
$this->rootElement = $options['rootElement'];
$this->setEncoding($options['encoding']);
if (array_key_exists('elementMap', $options)) {
$this->elementMap = $options['elementMap'];
}
if (array_key_exists('dateTimeFormat', $options)) {
$this->setDateTimeFormat($options['dateTimeFormat']);
}
}
/**
* Get encoding
*
* @return string
*/
public function getEncoding()
{
return $this->encoding;
}
/**
* Set encoding
*
* @param string $value
* @return Xml
*/
public function setEncoding($value)
{
$this->encoding = (string) $value;
return $this;
}
/**
* Set Escaper instance
*
* @return Xml
*/
public function setEscaper(Escaper $escaper)
{
$this->escaper = $escaper;
return $this;
}
/**
* Get Escaper instance
*
* Lazy-loads an instance with the current encoding if none registered.
*
* @return Escaper
*/
public function getEscaper()
{
if (null === $this->escaper) {
$this->setEscaper(new Escaper($this->getEncoding()));
}
return $this->escaper;
}
/**
* Formats data into a single line to be written by the writer.
*
* @param array $event event data
* @return string formatted line to write to the log
*/
public function format($event)
{
if (isset($event['timestamp']) && $event['timestamp'] instanceof DateTime) {
$event['timestamp'] = $event['timestamp']->format($this->getDateTimeFormat());
}
$dataToInsert = $event;
if (null !== $this->elementMap) {
$dataToInsert = [];
foreach ($this->elementMap as $elementName => $fieldKey) {
$dataToInsert[$elementName] = $event[$fieldKey];
}
}
$enc = $this->getEncoding();
$escaper = $this->getEscaper();
$dom = new DOMDocument('1.0', $enc);
$elt = $dom->appendChild(new DOMElement($this->rootElement));
foreach ($dataToInsert as $key => $value) {
if (
empty($value)
|| is_scalar($value)
|| ((is_array($value) || $value instanceof Traversable) && $key === "extra")
|| (is_object($value) && method_exists($value, '__toString'))
) {
if ($key === "message") {
$value = $escaper->escapeHtml($value);
}
if ($key === "extra" && empty($value)) {
continue;
}
if ($key === "extra" && (is_array($value) || $value instanceof Traversable)) {
$elt->appendChild($this->buildElementTree($dom, $dom->createElement('extra'), $value));
continue;
}
$elt->appendChild(new DOMElement($key, (string) $value));
}
}
return preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $dom->saveXML()) . PHP_EOL;
}
/**
* Recursion function to create an xml tree structure out of array structure
*
* @param DOMDocument $doc - DOMDocument where the current nodes will be generated
* @param DOMElement $rootElement - root element the tree will be attached to
* @param array|Traversable $mixedData - mixed data
* @return DOMElement $domElement - DOM Element with appended child nodes
*/
protected function buildElementTree(DOMDocument $doc, DOMElement $rootElement, $mixedData)
{
if (! (is_array($mixedData) || $mixedData instanceof Traversable)) {
return $rootElement;
}
foreach ($mixedData as $key => $value) {
// key is numeric and switch is not possible, numeric values are not valid node names
if ((empty($value) || is_numeric($value)) && is_numeric($key)) {
continue;
}
if ($value instanceof Traversable || is_array($value)) {
// current value is an array, start recursion
$rootElement->appendChild($this->buildElementTree($doc, $doc->createElement($key), $value));
continue;
}
if (is_object($value) && ! method_exists($value, '__toString')) {
// object does not support __toString() method, manually convert the value
$value = $this->getEscaper()->escapeHtml(
'"Object" of type ' . get_class($value) . " does not support __toString() method"
);
} elseif ($value) {
$value = $this->getEscaper()->escapeHtml((string) $value);
}
if (is_numeric($key)) {
// xml does not allow numeric values, try to switch the value and the key
$key = (string) $value;
$value = null;
}
try {
$rootElement->appendChild(new DOMElement($key, empty($value) ? null : (string) $value));
} catch (DOMException $e) {
// the input name is not valid, go one.
continue;
}
}
return $rootElement;
}
/**
* {@inheritDoc}
*/
public function getDateTimeFormat()
{
return $this->dateTimeFormat;
}
/**
* {@inheritDoc}
*/
public function setDateTimeFormat($dateTimeFormat)
{
$this->dateTimeFormat = (string) $dateTimeFormat;
return $this;
}
}