From 2661b62929c118bd472728aa77d2ec8c1977c447 Mon Sep 17 00:00:00 2001 From: Michael Richey Date: Sun, 3 Dec 2017 13:12:56 -0600 Subject: [PATCH] Revenge of Pretty Print This time it's personal Also, documented new functions --- admin/helpers/compiler/c_Fields.php | 63 ++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/admin/helpers/compiler/c_Fields.php b/admin/helpers/compiler/c_Fields.php index 08cbb4367..9bceb6fa1 100644 --- a/admin/helpers/compiler/c_Fields.php +++ b/admin/helpers/compiler/c_Fields.php @@ -2286,6 +2286,15 @@ class Fields extends Structure return false; } + /** + * xmlComment + * + * @param SimpleXMLElement $xml The XML element reference in which to inject a comment + * @param string $comment The comment to inject + * + * @return null + * + */ public function xmlComment(&$xml, $comment) { $domXML = dom_import_simplexml($xml); @@ -2295,6 +2304,15 @@ class Fields extends Structure $xml = simplexml_import_dom($domXML); } + /** + * xmlAddAttributes + * + * @param SimpleXMLElement $xml The XML element reference in which to inject a comment + * @param array $attributes The attributes to apply to the XML element + * + * @return null + * + */ public function xmlAddAttributes(&$xml, $attributes = array()) { foreach ($attributes as $key => $value) @@ -2303,6 +2321,15 @@ class Fields extends Structure } } + /** + * xmlAppend + * + * @param SimpleXMLElement $xml The XML element reference in which to inject a comment + * @param mixed $node A SimpleXMLElement node to append to the XML element reference, or a stdClass object containing a comment attribute to be injected before the XML node and a fieldXML attribute containing a SimpleXMLElement + * + * @return null + * + */ public function xmlAppend(&$xml, $node) { if (!$node) @@ -2330,6 +2357,15 @@ class Fields extends Structure } } + /** + * xmlPrettyPrint + * + * @param SimpleXMLElement $xml The XML element containing a node to be output + * @param string $nodename node name of the input xml element to print out. this is done to omit the ownerDocument; @@ -2338,7 +2374,32 @@ class Fields extends Structure $tidy = new Tidy(); $tidy->parseString($xmlString,array('indent'=>true,'indent-spaces'=>8,'input-xml'=>true,'output-xml'=>true,'indent-attributes'=>true,'wrap-attributes'=>true,'wrap'=>false)); $tidy->cleanRepair(); - return $tidy; + return $this->xmlIndent((string)$tidy,' ',8,true,false); + } + + /** + * xmlIndent + * + * @param string $string The XML input + * @param string $char Character or characters to use as the repeated indent + * @param integer $depth number of times to repeat the indent character + * @param boolean $skipfirst Skip the first line of the input. + * @param boolean $skiplast Skip the last line of the input; + * + * @return string XML output + * + */ + public function xmlIndent($string,$char=' ',$depth=0,$skipfirst=false,$skiplast=false) { + $output = array(); + $lines = explode("\n",$string); + $first = true; + $last = count($lines)-1; + foreach($lines as $i=>$line) + { + $output[] = (($first&&$skipfirst)||$i===$last&&$skiplast)?$line:str_repeat($char,$depth).$line; + $first = false; + } + return implode("\n",$output); } }