From 8a342adf382299aa1c4b63a37a6601ebd9738b13 Mon Sep 17 00:00:00 2001 From: dkemper Date: Mon, 14 Apr 2014 22:59:23 +0200 Subject: [PATCH 01/10] removed slash from Traversable --- src/Formatter/Xml.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Formatter/Xml.php b/src/Formatter/Xml.php index acd63681..6f00e142 100644 --- a/src/Formatter/Xml.php +++ b/src/Formatter/Xml.php @@ -177,14 +177,14 @@ public function format($event) foreach ($dataToInsert as $key => $value) { if (empty($value) || is_scalar($value) - || ((is_array($value) || $value instanceof \Traversable) && $key == "extra") + || ((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)) { continue; - } elseif ($key == "extra" && (is_array($value) || $value instanceof \Traversable)) { + } elseif ($key == "extra" && (is_array($value) || $value instanceof Traversable)) { $extraElement = $dom->appendChild(new DOMElement('extra')); foreach ($value as $extraKey => $extraValue) { $extraElement->appendChild(new DOMElement($extraKey, (string) $extraValue)); From 74991d81725f548afff166f5167b6ef0ce517ea7 Mon Sep 17 00:00:00 2001 From: dkemper Date: Mon, 14 Apr 2014 23:21:11 +0200 Subject: [PATCH 02/10] changed dom creation type --- src/Formatter/Xml.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Formatter/Xml.php b/src/Formatter/Xml.php index 6f00e142..056a784e 100644 --- a/src/Formatter/Xml.php +++ b/src/Formatter/Xml.php @@ -185,7 +185,7 @@ public function format($event) } elseif ($key == "extra" && empty($value)) { continue; } elseif ($key == "extra" && (is_array($value) || $value instanceof Traversable)) { - $extraElement = $dom->appendChild(new DOMElement('extra')); + $extraElement = $dom->createElement('extra'); foreach ($value as $extraKey => $extraValue) { $extraElement->appendChild(new DOMElement($extraKey, (string) $extraValue)); } From ae1298004a3706106e1a610cd8e09b33a4da30ee Mon Sep 17 00:00:00 2001 From: dkemper Date: Tue, 15 Apr 2014 02:38:42 +0200 Subject: [PATCH 03/10] added nested array handling for extra data --- src/Formatter/Xml.php | 53 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 5 deletions(-) diff --git a/src/Formatter/Xml.php b/src/Formatter/Xml.php index 056a784e..bde6cc93 100644 --- a/src/Formatter/Xml.php +++ b/src/Formatter/Xml.php @@ -185,11 +185,7 @@ public function format($event) } elseif ($key == "extra" && empty($value)) { continue; } elseif ($key == "extra" && (is_array($value) || $value instanceof Traversable)) { - $extraElement = $dom->createElement('extra'); - foreach ($value as $extraKey => $extraValue) { - $extraElement->appendChild(new DOMElement($extraKey, (string) $extraValue)); - } - $elt->appendChild($extraElement); + $elt->appendChild($this->_buildElementTree($dom, $dom->createElement('extra'), $value)); continue; } $elt->appendChild(new DOMElement($key, (string) $value)); @@ -197,11 +193,58 @@ public function format($event) } $xml = $dom->saveXML(); + $xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml); return $xml . PHP_EOL; } + /** + * Recursion function to crete a xml tree structure out of array structure + * @param DomDocument $doc - DomDocument where the current nodes will be generated + * @param DomElement $domElement - Element where there + * @param $mixedData array|Traversable - mixedData + * @return DomElement with appended child nodes + */ + protected function _buildElementTree(DOMDocument $doc, DOMElement $domElement, $mixedData) + { + if (is_array($mixedData) || $mixedData instanceof Traversable) { + + foreach ($mixedData as $key => $value) { + + // key is numeric and switch is not possible, numeric values are not valid node names + if (is_numeric($key) && (is_numeric($value) || empty($value))) { + continue; + } + + if (is_array($value) || $value instanceof Traversable) { + // current value is an array, start recursion + $domElement->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, switch the value and the key + $key = (string)$value; + $value = null; + } + + $domElement->appendChild(new DOMElement($key, (!empty($value)) ? (string) $value : null)); + + } + } + + return $domElement; + } + /** * {@inheritDoc} */ From a255e00569ad774fb64aac045c8415341d0f1680 Mon Sep 17 00:00:00 2001 From: dkemper Date: Thu, 17 Apr 2014 23:42:24 +0200 Subject: [PATCH 04/10] added tests, modified docs, improved error handling on wrong tag names --- src/Formatter/Xml.php | 25 ++++++++------- test/Formatter/XmlTest.php | 64 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 13 deletions(-) diff --git a/src/Formatter/Xml.php b/src/Formatter/Xml.php index bde6cc93..50be1829 100644 --- a/src/Formatter/Xml.php +++ b/src/Formatter/Xml.php @@ -185,7 +185,7 @@ public function format($event) } elseif ($key == "extra" && empty($value)) { continue; } elseif ($key == "extra" && (is_array($value) || $value instanceof Traversable)) { - $elt->appendChild($this->_buildElementTree($dom, $dom->createElement('extra'), $value)); + $elt->appendChild($this->buildElementTree($dom, $dom->createElement('extra'), $value)); continue; } $elt->appendChild(new DOMElement($key, (string) $value)); @@ -200,13 +200,13 @@ public function format($event) } /** - * Recursion function to crete a xml tree structure out of array structure + * 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 $domElement - Element where there + * @param DomElement $rootElement - root element the tree will be attached to * @param $mixedData array|Traversable - mixedData - * @return DomElement with appended child nodes + * @return DomElement $domElement - DOM Element with appended child nodes */ - protected function _buildElementTree(DOMDocument $doc, DOMElement $domElement, $mixedData) + protected function buildElementTree(DOMDocument $doc, DOMElement $rootElement, $mixedData) { if (is_array($mixedData) || $mixedData instanceof Traversable) { @@ -219,12 +219,11 @@ protected function _buildElementTree(DOMDocument $doc, DOMElement $domElement, $ if (is_array($value) || $value instanceof Traversable) { // current value is an array, start recursion - $domElement->appendChild($this->_buildElementTree($doc, $doc->createElement($key), $value)); + $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" @@ -232,17 +231,21 @@ protected function _buildElementTree(DOMDocument $doc, DOMElement $domElement, $ } if (is_numeric($key)) { - // xml does not allow numeric values, switch the value and the key + // xml does not allow numeric values, try to switch the value and the key $key = (string)$value; $value = null; } - $domElement->appendChild(new DOMElement($key, (!empty($value)) ? (string) $value : null)); - + try { + $rootElement->appendChild(new DOMElement($key, (!empty($value)) ? (string) $value : null)); + } catch (\DOMException $e) { + // the input name is not valid, go one. + continue; + } } } - return $domElement; + return $rootElement; } /** diff --git a/test/Formatter/XmlTest.php b/test/Formatter/XmlTest.php index 0170013c..bffbcfc7 100644 --- a/test/Formatter/XmlTest.php +++ b/test/Formatter/XmlTest.php @@ -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 = '2001-01-01T12:00:00-06:00test1CRIT'; $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 = '2001-01-01T12:00:00-06:00test1CRITonefoo'; + $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 = '2001-01-01T12:00:00-06:00test1CRITfour14"Object" of type stdClass does not support __toString() methodZendTest\Log\TestAsset\SerializableObjectfoo'; + $expected .= PHP_EOL . PHP_EOL; + $this->assertEquals($expected, $formatter->format($event)); + } + } From 072b295f09b1f1cd09ed4ed8a132f0b1ea0cb059 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 28 Jul 2014 01:21:05 +0200 Subject: [PATCH 05/10] zendframework/zf2#6137 - reducing nesting level --- src/Formatter/Xml.php | 57 ++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/src/Formatter/Xml.php b/src/Formatter/Xml.php index 50be1829..c56b51c4 100644 --- a/src/Formatter/Xml.php +++ b/src/Formatter/Xml.php @@ -208,40 +208,41 @@ public function format($event) */ protected function buildElementTree(DOMDocument $doc, DOMElement $rootElement, $mixedData) { - if (is_array($mixedData) || $mixedData instanceof Traversable) { + if (! (is_array($mixedData) || $mixedData instanceof Traversable)) { + return $rootElement; + } - foreach ($mixedData as $key => $value) { + foreach ($mixedData as $key => $value) { - // key is numeric and switch is not possible, numeric values are not valid node names - if (is_numeric($key) && (is_numeric($value) || empty($value))) { - continue; - } + // key is numeric and switch is not possible, numeric values are not valid node names + if (is_numeric($key) && (is_numeric($value) || empty($value))) { + continue; + } - if (is_array($value) || $value instanceof Traversable) { - // current value is an array, start recursion - $rootElement->appendChild($this->buildElementTree($doc, $doc->createElement($key), $value)); - continue; - } + if (is_array($value) || $value instanceof Traversable) { + // 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_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; - } + 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)) ? (string) $value : null)); - } catch (\DOMException $e) { - // the input name is not valid, go one. - continue; - } + try { + $rootElement->appendChild(new DOMElement($key, (!empty($value)) ? (string) $value : null)); + } catch (\DOMException $e) { + // the input name is not valid, go one. + continue; } } From 95a9cbd64fe0bfdfff3a3831c8dcadf81fc89245 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 28 Jul 2014 01:22:20 +0200 Subject: [PATCH 06/10] zendframework/zf2#6137 - minor cleanups, removing useless `else` block --- src/Formatter/Xml.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Formatter/Xml.php b/src/Formatter/Xml.php index c56b51c4..c8991407 100644 --- a/src/Formatter/Xml.php +++ b/src/Formatter/Xml.php @@ -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]; } From 16474b70a920374ffea1baeea9630cb00a26c0f2 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 28 Jul 2014 01:24:36 +0200 Subject: [PATCH 07/10] zendframework/zf2#6137 - avoiding `elseif` where not needed --- src/Formatter/Xml.php | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Formatter/Xml.php b/src/Formatter/Xml.php index c8991407..783fe3e6 100644 --- a/src/Formatter/Xml.php +++ b/src/Formatter/Xml.php @@ -183,12 +183,18 @@ public function format($event) ) { if ($key == "message") { $value = $escaper->escapeHtml($value); - } elseif ($key == "extra" && empty($value)) { + } + + if ($key == "extra" && empty($value)) { continue; - } elseif ($key == "extra" && (is_array($value) || $value instanceof Traversable)) { + } + + 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)); } } From 1874f01597647e6888450655408a1f23b1860eff Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 28 Jul 2014 01:25:19 +0200 Subject: [PATCH 08/10] zendframework/zf2#6137 - simplified return value for the generated XML, avoiding writing it to a variable --- src/Formatter/Xml.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Formatter/Xml.php b/src/Formatter/Xml.php index 783fe3e6..ed704aae 100644 --- a/src/Formatter/Xml.php +++ b/src/Formatter/Xml.php @@ -199,11 +199,7 @@ public function format($event) } } - $xml = $dom->saveXML(); - - $xml = preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $xml); - - return $xml . PHP_EOL; + return preg_replace('/<\?xml version="1.0"( encoding="[^\"]*")?\?>\n/u', '', $dom->saveXML()) . PHP_EOL; } /** From 4dc76978fa399ed2de0330cd6c726a4020aa2e8c Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 28 Jul 2014 01:27:16 +0200 Subject: [PATCH 09/10] zendframework/zf2#6137 - simplified ternary, alignment, spacing --- src/Formatter/Xml.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Formatter/Xml.php b/src/Formatter/Xml.php index ed704aae..b06bd589 100644 --- a/src/Formatter/Xml.php +++ b/src/Formatter/Xml.php @@ -225,6 +225,7 @@ protected function buildElementTree(DOMDocument $doc, DOMElement $rootElement, $ if (is_array($value) || $value instanceof Traversable) { // current value is an array, start recursion $rootElement->appendChild($this->buildElementTree($doc, $doc->createElement($key), $value)); + continue; } @@ -237,12 +238,12 @@ protected function buildElementTree(DOMDocument $doc, DOMElement $rootElement, $ if (is_numeric($key)) { // xml does not allow numeric values, try to switch the value and the key - $key = (string)$value; + $key = (string) $value; $value = null; } try { - $rootElement->appendChild(new DOMElement($key, (!empty($value)) ? (string) $value : null)); + $rootElement->appendChild(new DOMElement($key, empty($value) ? null : (string) $value)); } catch (\DOMException $e) { // the input name is not valid, go one. continue; From b9f8bdd23526daf87232049afe945919800b88f7 Mon Sep 17 00:00:00 2001 From: Marco Pivetta Date: Mon, 28 Jul 2014 01:30:24 +0200 Subject: [PATCH 10/10] zendframework/zf2#6137 - swapping type checks to reduce overhead --- src/Formatter/Xml.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/Formatter/Xml.php b/src/Formatter/Xml.php index b06bd589..8f916479 100644 --- a/src/Formatter/Xml.php +++ b/src/Formatter/Xml.php @@ -216,20 +216,19 @@ protected function buildElementTree(DOMDocument $doc, DOMElement $rootElement, $ } foreach ($mixedData as $key => $value) { - // key is numeric and switch is not possible, numeric values are not valid node names - if (is_numeric($key) && (is_numeric($value) || empty($value))) { + if ((empty($value) || is_numeric($value)) && is_numeric($key)) { continue; } - if (is_array($value) || $value instanceof Traversable) { + 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')) { + 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"