Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
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; } }
This paste will be private.
From the Design Piracy series on my blog: