Report abuse


interface tableRenderer
{
    public  function Render($myThis, $asText = false);
}

class tableRendererDefault implements tableRenderer
{
    public  function Render($myThis, $asText = false)
    {
        return bufferedDump($myThis);
    }
}

class tableRendererXML implements tableRenderer
{
    public  function Render($myThis, $asText = false)
    {
        if (!isset($myThis->rootXPath))
        {
            $myRowName  = $myThis->tableName;
            $myRoot     = 'appData';
        } else
        {
            $xpathParts = explode('/', removeLeadingSlash($myThis->rootXPath));
            $myRoot     = $xpathParts[0];
            $myRowName  = $xpathParts[1];
        }
        $myXML      = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<${myRoot}>
</${myRoot}>
XML;
        
        $myXMLDoc       = new SimpleXMLElement($myXML);
        $nodeAttributes = array('id', 'uid');
        
        if ($myThis->numRows == 1) {
            self::appendRow($myXMLDoc, $myRowName, $myThis->myEntries, $myThis->myFieldsDef, $nodeAttributes);
        } else {
            foreach ($myThis->myEntries as $entryID => $entryData)
            {
                if (is_numeric($entryID)) {
                    self::appendRow($myXMLDoc, $myRowName, $entryData, $myThis->myFieldsDef, $nodeAttributes);
                }
            }
        }
        
        if (!$asText) {
            return $myXMLDoc;
        } else {
            $xmlDoc = new DOMDocument;
            $xmlDoc->formatOutput       = true;
            $xmlDoc->preserveWhiteSpace = false;
            $xmlDoc->loadXML($myXMLDoc->asXML());
            return $xmlDoc->saveXML();
        }
    }
    
    final   private static  function appendRow(&$myXMLDoc, $myRowName, $entryData, $myFieldsDef, $nodeAttributes)
    {
        $tableRow   = $myXMLDoc->addChild($myRowName);
        
        foreach ($entryData as $entryField => $entryValue) {
            self::appendRowNode($tableRow, $myRowName, $entryField, $entryValue, $myFieldsDef, $nodeAttributes);
        }
        
        return $myXMLDoc;
    }
    
    final   private static  function appendRowNode(&$tableRow, $myRowName, $entryField, $entryValue, $myFieldsDef, $nodeAttributes)
    {
        if (in_array(trim($entryField), $nodeAttributes) ||
            !empty($myFieldsDef[trim($entryField)]['key']) ||
            preg_match('/(enum\((.*)\))/', $myFieldsDef[$entryField]['type']))
        {
            $tableRow->addAttribute($entryField, $entryValue);
        } else
        {
            $tableRow->$entryField = $entryValue;
        }
        
        return $tableRow;
    }
}