- var_dump: documentation ( source)
<?php
const VALUE_FORMAT_RAW = "raw";
const VALUE_FORMAT_TAG_NAME_VALUE_ASSOC = "tagNameValueAssoc";
const VALUE_FORMAT_ARRAY = "array";
function formatArgumentValue(SimpleXMLElement $node, $options)
{
switch ($options["format"]) {
// Associative array from each children of the target node that have the tag name as key and
// the tag value as value
case VALUE_FORMAT_TAG_NAME_VALUE_ASSOC:
$assoc = [];
// Recursively get the associative array for the node
foreach ($node->children() as $nodePart) {
if($nodePart->count() > 0) {
$format = VALUE_FORMAT_TAG_NAME_VALUE_ASSOC;
if (!empty($nodePart->attributes()->type) && $nodePart->attributes()->type == "array") {
$format = VALUE_FORMAT_ARRAY;
}
$assoc[$nodePart->getName()] = $this->formatArgumentValue($nodePart, ["format" => $format]);
} else {
// If the value is already defined, we say that the value is an array and add the new value to it
if(!empty($assoc[$nodePart->getName()])) {
if(!is_array($assoc[$nodePart->getName()])) {
$assoc[$nodePart->getName()] = [$assoc[$nodePart->getName()]];
}
$assoc[$nodePart->getName()][] = $this->resolveValueString((string) $nodePart);
} else {
$assoc[$nodePart->getName()] = $this->resolveValueString((string) $nodePart);
}
}
}
$argumentValue = $assoc;
break;
case VALUE_FORMAT_ARRAY:
$children = [];
$i = 0;
foreach($node->children() as $child)
{
// Get the path for the key
$key = $i++; // Set the default key as a regular numbered autoincremented index
// NOTE: Here a post-increment is done, the value copied to $key is $i BEFORE
// it being incremented. The array will start at 0, even if the increment is done at
// the beginning of the string.
if(!empty($options['childrenKey'])) {
$key = (string) $this->walkNodePath($options['childrenKey'], $child);
}
$children[$key] = $this->formatArgumentValue($child, ["format" => $options["childrenFormat"]]);
}
$argumentValue = $children;
break;
// Raw string value of the tag as value. Default.
case VALUE_FORMAT_RAW:
default:
$argumentValue = $this->resolveValueString((string) $node);
break;
}
return $argumentValue;
}
$xml = <<<EOL
<config>
<formatters>
<fileformatter>
<class>Monolog\Formatter\LineFormatter</class>
<format>[%datetime%] %level_name%: %message%
</format>
</fileformatter>
<mongoformatter>
<class>Bettr\Service\Log\Formatter\MongoDBFormatter</class>
</mongoformatter>
</formatters>
<handlers>
<filelog>
<class>Monolog\Handler\StreamHandler</class>
<level>DEBUG</level>
<formatter>fileformatter</formatter>
<processors>psr_processor</processors>
<stream>../system/var/logs/debug.log</stream>
</filelog>
<mongolog>
<class>Bettr\Service\Log\Handler\MongoServiceHandler</class>
<level>ERROR</level>
<formatter>mongoformatter</formatter>
<mongoManager>{service:mongo}</mongoManager>
<processors type="array">psr_processor</processors>
<collection>logger</collection>
</mongolog>
</handlers>
<processors>
<psr_processor>
<class>Monolog\Processor\PsrLogMessageProcessor</class>
</psr_processor>
</processors>
<loggers>
<bettr>
<handlers type="array">
<handler>filelog</handler>
<handler>mongolog</handler>
</handlers>
</bettr>
</loggers>
</config>
EOL;
$simpleXMLElement = new SimpleXMLElement($xml);
var_dump(formatArgumentValue($simpleXMLElement, ["format" => VALUE_FORMAT_TAG_NAME_VALUE_ASSOC]));