21 $this->_indent = $indent;
30 public function format($xmlString)
32 $xmlDom = new \DOMDocument(
'1.0');
33 $xmlDom->formatOutput =
true;
34 $xmlDom->preserveWhiteSpace =
false;
35 $xmlDom->loadXML($xmlString);
39 $xmlXpath = new \DOMXPath($xmlDom);
41 foreach ($xmlXpath->query(
'//text() | //comment() | //@*') as $textNode) {
42 $placeholder = \spl_object_hash($textNode);
43 $placeholders[$placeholder] = $textNode->textContent;
44 $textNode->nodeValue = $placeholder;
51 $indent = $this->_indent;
52 $result = \preg_replace_callback(
54 function (array $matches) use ($indent) {
55 $indentCount = \strlen($matches[0]) >> 1;
56 return \str_repeat($indent, $indentCount);
62 $result = \str_replace(\array_keys($placeholders), \array_values($placeholders),
$result);