Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/zendframework/zendframework#6137-formatter-xml-…
Browse files Browse the repository at this point in the history
…extra-improvement' into develop

Close zendframework/zendframework#6137
  • Loading branch information
Ocramius committed Jul 27, 2014
148 parents eda5e97 + b652dfb + db2794e + 6fca41f + 317429f + 6753576 + 4112f92 + 6faf2bb + df3a849 + ee5efcb + 2ee5d16 + bcb4d03 + a5c61e2 + 57ffceb + a6fa26b + 9d36595 + bf0040c + 71b1a95 + 5619ba3 + c093ab0 + de13946 + 04b025f + 0933607 + 34ed3ed + 83f6472 + db74e8c + 7b75da1 + bb4c48f + 9fa3c05 + 139c0b1 + a587eef + 7a782f4 + 39cb2e2 + 31f8c80 + a9b7779 + de058f6 + 67b97f6 + 40d86d0 + b5234cc + 17b4e13 + 8af24aa + 1a9df55 + 2c85c99 + 5557fac + fe78a35 + f6430c3 + 6041a1e + 86b9031 + 674bd75 + 67a1bfb + 4d4dd8b + c655082 + dbb18b2 + 97a9134 + 80367df + 98d5255 + 7946c9f + ea67c13 + 44df0b2 + 38e090d + 7af74e7 + 8b311b1 + e2c2b94 + 51601cb + dcc1eaf + 6f4b805 + a30a64f + ee7347d + 8b04bfe + b487f00 + ecdfdf6 + d8cde75 + b7a33bb + 65ecb58 + 571d938 + 625a786 + 6fe4efc + f94838d + 76607e3 + 492076c + 17b56b8 + c5509fe + e0fa433 + 7d74e99 + 309136e + f1dd38a + 7be533f + 6a618c2 + 3a454e3 + f66ca80 + f1bebf2 + 850249d + 674d7ba + 597ff82 + ae87c49 + fb156fb + 9c474de + 1a75c72 + 47d1cd2 + a937ea3 + 4219922 + 49e380f + 5903df6 + 7a9d632 + 2ef7274 + b606220 + a09c5c7 + 21afa5c + 8faa67f + 1731915 + efa1bb3 + 5576455 + 86d3ae8 + de3fd70 + 398a3c4 + 5c56554 + 7ad896f + fa91c9b + 1372a45 + a0c8737 + e368d53 + 462b1f7 + 79675fe + a8104b9 + 91ed9d0 + b1d1358 + 6a0c90c + 5e4258f + 239c0d0 + b457c76 + a2533a4 + beb6598 + 79d0f46 + 1519723 + 5084e3b + ce75f80 + e5fd689 + 6744e82 + 6cb64b8 + 03c6da8 + 7dcfd8f + a18fe5d + 5e5ada1 + 439fdac + e6addba + 0272b1d + 0df04d6 + b9f8bdd commit de49a1f
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 9 deletions.
72 changes: 65 additions & 7 deletions src/Formatter/Xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,11 @@ public function format($event)
$event['timestamp'] = $event['timestamp']->format($this->getDateTimeFormat());
}

if ($this->elementMap === null) {
$dataToInsert = $event;
} else {
$dataToInsert = $event;

if (null !== $this->elementMap) {
$dataToInsert = array();

foreach ($this->elementMap as $elementName => $fieldKey) {
$dataToInsert[$elementName] = $event[$fieldKey];
}
Expand All @@ -177,21 +178,78 @@ public function format($event)
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);
} elseif ($key == "extra" && empty($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));
}
}

$xml = $dom->saveXML();
$xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml);
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 $mixedData array|Traversable - mixedData
* @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"
);
}

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 $xml . PHP_EOL;
return $rootElement;
}

/**
Expand Down
64 changes: 62 additions & 2 deletions test/Formatter/XmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,74 @@ public function testFormatWillRemoveExtraEmptyArrayFromEvent()
$formatter = new XmlFormatter;
$d = new DateTime('2001-01-01T12:00:00-06:00');
$event = array(
'timestamp' => $d,
'timestamp' => $d,
'message' => 'test',
'priority' => 1,
'priorityName' => 'CRIT',
'extra' => array()
'extra' => array()
);
$expected = '<logEntry><timestamp>2001-01-01T12:00:00-06:00</timestamp><message>test</message><priority>1</priority><priorityName>CRIT</priorityName></logEntry>';
$expected .= PHP_EOL . PHP_EOL;
$this->assertEquals($expected, $formatter->format($event));
}

public function testFormatWillAcceptSimpleArrayFromExtra()
{
$formatter = new XmlFormatter;
$d = new DateTime('2001-01-01T12:00:00-06:00');
$event = array(
'timestamp' => $d,
'message' => 'test',
'priority' => 1,
'priorityName' => 'CRIT',
'extra' => array(
'test' => 'one',
'bar' => 'foo',
'wrong message' => 'dasdasd'
)
);

$expected = '<logEntry><timestamp>2001-01-01T12:00:00-06:00</timestamp><message>test</message><priority>1</priority><priorityName>CRIT</priorityName><extra><test>one</test><bar>foo</bar></extra></logEntry>';
$expected .= PHP_EOL . PHP_EOL;
$this->assertEquals($expected, $formatter->format($event));
}

public function testFormatWillAcceptNestedArrayFromExtraEvent()
{
$formatter = new XmlFormatter;

$d = new DateTime('2001-01-01T12:00:00-06:00');

$event = array(
'timestamp' => $d,
'message' => 'test',
'priority' => 1,
'priorityName' => 'CRIT',
'extra' => array(
'test' => array(
'one',
'two' => array(
'three' => array(
'four' => 'four'
),
'five' => array('')
)
),
'1111' => '2222',
'test_null' => null,
'test_int' => 14,
'test_object' => new \stdClass(),
new SerializableObject(),
'serializable_object' => new SerializableObject(),
null,
'test_empty_array' => array(),
'bar' => 'foo',
'foobar'
)
);
$expected = '<logEntry><timestamp>2001-01-01T12:00:00-06:00</timestamp><message>test</message><priority>1</priority><priorityName>CRIT</priorityName><extra><test><one/><two><three><four>four</four></three><five/></two></test><test_null/><test_int>14</test_int><test_object>"Object" of type stdClass does not support __toString() method</test_object><serializable_object>ZendTest\Log\TestAsset\SerializableObject</serializable_object><test_empty_array/><bar>foo</bar><foobar/></extra></logEntry>';
$expected .= PHP_EOL . PHP_EOL;
$this->assertEquals($expected, $formatter->format($event));
}

}

0 comments on commit de49a1f

Please sign in to comment.