indentString() public static method

public static indentString ( $string, $size = 2, $char = ' ' )
Esempio n. 1
0
 /**
  * Appends the xm structure with the given values.
  *
  * @param string $key
  * @param mixed $value
  * @param \DOMNode $node
  * @param boolean $arrayType
  * @param boolean $printDefaults
  *
  * @param bool $printComments
  * @return \DOMNode
  * @throws \Exception
  */
 public function appendXmlValue($key, $value, \DOMNode $node, $arrayType = false, $printDefaults = false, $printComments = false)
 {
     $doc = $node instanceof \DOMDocument ? $node : $node->ownerDocument;
     $append = function ($el) use($node) {
         return $node->appendChild($el);
     };
     if ($printComments && property_exists(get_called_class(), $key) && ($comment = $this->getPropertyDescription($key))) {
         if (in_array($key, $this->attributes)) {
             $comment = Tools::indentString($comment, 4);
             $comment = sprintf("\n\n  Attribute %s:\n\n%s", $key, $comment);
             $this->lastRootElementComment->appendData($comment);
         } else {
             $comment = "\n" . Tools::indentString($comment, 2);
             $comment = $doc->createComment($comment);
             $append($comment);
         }
     }
     if (null !== $this->nodeValueVar && $key == $this->nodeValueVar) {
         $textNode = $doc->createTextNode($value);
         $result = $append($textNode);
     } else {
         if (is_scalar($value) || null === $value) {
             $value = is_bool($value) ? $value ? 'true' : 'false' : (string) $value;
             if ($arrayType) {
                 $element = $doc->createElement(@$this->arrayIndexNames[$arrayType] ?: 'item');
                 if (!is_integer($key)) {
                     $element->setAttribute('key', (string) $key);
                 }
                 $element->nodeValue = $value;
                 $result = $append($element);
             } else {
                 if (in_array($key, $this->attributes)) {
                     //                    $key = Tools::camelcase2Char($key, '-');
                     $result = $node->setAttribute($key, $value);
                 } else {
                     $elementName = is_integer($key) ? $this->arrayIndexNames[$arrayType] ?: 'item' : $key;
                     //                    $elementName = Tools::camelcase2Char($elementName, '-');
                     $element = $doc->createElement($elementName);
                     $element->nodeValue = $value;
                     $result = $append($element);
                 }
             }
         } else {
             if (is_array($value)) {
                 if ($arrayName = $this->getElementArrayName($key)) {
                     $element = $node;
                 } else {
                     $elementName = is_integer($key) ? $this->arrayIndexNames[$arrayType] ?: 'item' : $key;
                     //                $elementName = Tools::camelcase2Char($elementName, '-');
                     $element = $doc->createElement($elementName);
                 }
                 foreach ($value as $k => $v) {
                     $this->appendXmlValue($k, $v, $element, $key, $printDefaults, $printComments);
                 }
                 if (!$arrayName) {
                     $result = $append($element);
                 } else {
                     $result = $element;
                 }
             } else {
                 if ($value instanceof Model) {
                     $result = $value->appendXml($node, $printDefaults, $printComments);
                 }
             }
         }
     }
     return $result;
 }