function padString($str, $length, $spacer = " ", $align = "left") { switch ($align) { case "right": return $str . General::repeatStr($spacer, $length - strlen($str)); break; case "center": return General::repeatStr($spacer, ($length - strlen($str)) * 0.5) . $str . General::repeatStr($spacer, ($length - strlen($str)) * 0.5); break; case "left": default: return General::repeatStr($spacer, $length - strlen($str)) . $str; break; } }
public function generate($indent = false, $tab_depth = 0, $hasParent = false) { $result = NULL; $newline = $indent ? self::CRLF : NULL; if (!$hasParent) { if ($this->_includeHeader) { $result .= sprintf('<?xml version="%s" encoding="%s" ?>', $this->_version, $this->_encoding) . $newline; } if ($this->_dtd) { $result .= $this->_dtd . $newline; } if (is_array($this->_processingInstructions) && !empty($this->_processingInstructions)) { $result .= implode(self::CRLF, $this->_processingInstructions); } } $result .= ($indent ? General::repeatStr("\t", $tab_depth) : NULL) . '<' . $this->_name; if (count($this->_attributes) > 0) { foreach ($this->_attributes as $attribute => $value) { if (strlen($value) != 0 || strlen($value) == 0 && $this->_allowEmptyAttributes) { $result .= sprintf(' %s="%s"', $attribute, $value); } } } $numberOfchildren = $this->getNumberOfChildren(); if ($numberOfchildren > 0 || strlen($this->_value) != 0 || !$this->_selfclosing) { $result .= '>'; if ($this->_value != NULL && !$this->_placeValueAfterChildElements) { $result .= $this->_value; } if ($numberOfchildren > 0) { $result .= $newline; foreach ($this->_children as $child) { if (!$child instanceof self) { throw new Exception('Child is not of type XMLElement'); } $child->setElementStyle($this->_elementStyle); $result .= $child->generate($indent, $tab_depth + 1, true); } if ($indent) { $result .= str_repeat("\t", $tab_depth); } } if ($this->_value != NULL && $this->_placeValueAfterChildElements) { if ($indent) { $result .= str_repeat("\t", max(1, $tab_depth)); } $result .= $this->_value . $newline; } $result .= "</{$this->_name}>{$newline}"; // Empty elements: } else { if ($this->_elementStyle == 'xml') { $result .= ' />'; } else { if (in_array($this->_name, $this->_no_end_tags) || substr($this->_name, 0, 3) == '!--') { $result .= '>'; } else { $result .= "></{$this->_name}>"; } } $result .= $newline; } return $result; }
function generate($indent = false, $tab_depth = 0) { $result = ""; $newline = $indent ? "\n" : ""; if ($this->_includeHeader) { $result .= '<?xml version="' . $this->_version . '" encoding="' . $this->_encoding . '" ?>' . $newline; } $result .= ($indent ? General::repeatStr("\t", $tab_depth) : "") . "<" . $this->_name; if (count($this->_attributes) > 0) { foreach ($this->_attributes as $attribute => $value) { if ($value != NULL) { $result .= " {$attribute}=\"{$value}\""; } } } $numberOfchildren = $this->getNumberOfChildren(); if ($numberOfchildren > 0 || $this->_value != NULL) { $result .= ">"; if ($this->_value != NULL) { $result .= $this->_value; } if ($numberOfchildren > 0) { $result .= $newline; foreach ($this->_children as $child) { $result .= $child->generate($indent, $tab_depth + 1); } if ($indent) { $result .= General::repeatStr("\t", $tab_depth); } } $result .= "</" . $this->_name . ">" . $newline; } else { $result .= " />" . $newline; } return $result; }