|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace a9f\FractorXml\Printer; |
| 6 | + |
| 7 | +/** |
| 8 | + * @source https://github.com/shanethehat/pretty-xml/blob/master/src/PrettyXml/Formatter.php |
| 9 | + */ |
| 10 | +class PrettyXMLPrinter |
| 11 | +{ |
| 12 | + private int $depth; |
| 13 | + |
| 14 | + private int $indent = 4; |
| 15 | + |
| 16 | + private string $padChar = ' '; |
| 17 | + |
| 18 | + private bool $preserveWhitespace = false; |
| 19 | + |
| 20 | + public function setIndentSize(int $indent): void |
| 21 | + { |
| 22 | + $this->indent = $indent; |
| 23 | + } |
| 24 | + |
| 25 | + public function setIndentCharacter(string $indentCharacter): void |
| 26 | + { |
| 27 | + $this->padChar = $indentCharacter; |
| 28 | + } |
| 29 | + |
| 30 | + public function format(string $xml): string |
| 31 | + { |
| 32 | + $output = ''; |
| 33 | + $this->depth = 0; |
| 34 | + |
| 35 | + $parts = $this->getXmlParts($xml); |
| 36 | + |
| 37 | + if (str_starts_with($parts[0], '<?xml')) { |
| 38 | + $output = array_shift($parts) . PHP_EOL; |
| 39 | + } |
| 40 | + |
| 41 | + foreach ($parts as $key => $part) { |
| 42 | + $element = preg_replace('/<([a-zA-Z0-9\-_]+).*/', '$1', $part); |
| 43 | + |
| 44 | + if ($element && isset($parts[$key + 1]) && preg_replace('~</(.*)>~', '$1', $parts[$key + 1]) === $element) { |
| 45 | + $output .= $this->getOutputForPart($part, ''); |
| 46 | + } else { |
| 47 | + $output .= $this->getOutputForPart($part); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return trim((string) preg_replace('~>' . $this->padChar . '+<~', '><', $output)); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @return string[] |
| 56 | + */ |
| 57 | + private function getXmlParts(string $xml): array |
| 58 | + { |
| 59 | + $withNewLines = preg_replace('/(>)(<)(\/*)/', "$1\n$2$3", trim($xml)); |
| 60 | + return explode("\n", (string) $withNewLines); |
| 61 | + } |
| 62 | + |
| 63 | + private function getOutputForPart(string $part, string $eol = PHP_EOL): string |
| 64 | + { |
| 65 | + $output = ''; |
| 66 | + $this->runPre($part); |
| 67 | + |
| 68 | + if ($this->preserveWhitespace) { |
| 69 | + $output .= $part . $eol; |
| 70 | + } else { |
| 71 | + $part = trim($part); |
| 72 | + $output .= $this->getPaddedString($part) . $eol; |
| 73 | + } |
| 74 | + |
| 75 | + $this->runPost($part); |
| 76 | + |
| 77 | + return $output; |
| 78 | + } |
| 79 | + |
| 80 | + private function runPre(string $part): void |
| 81 | + { |
| 82 | + if ($this->isClosingTag($part)) { |
| 83 | + $this->depth--; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private function runPost(string $part): void |
| 88 | + { |
| 89 | + if ($this->isOpeningCdataTag($part) && $this->isClosingCdataTag($part)) { |
| 90 | + return; |
| 91 | + } |
| 92 | + if ($this->isOpeningTag($part)) { |
| 93 | + $this->depth++; |
| 94 | + } |
| 95 | + if ($this->isClosingCdataTag($part)) { |
| 96 | + $this->preserveWhitespace = false; |
| 97 | + } |
| 98 | + if ($this->isOpeningCdataTag($part)) { |
| 99 | + $this->preserveWhitespace = true; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + private function getPaddedString(string $part): string |
| 104 | + { |
| 105 | + return str_pad($part, strlen($part) + ($this->depth * $this->indent), $this->padChar, STR_PAD_LEFT); |
| 106 | + } |
| 107 | + |
| 108 | + private function isOpeningTag(string $part): bool |
| 109 | + { |
| 110 | + return (bool) preg_match('/^<[^\/]*>$/', $part); |
| 111 | + } |
| 112 | + |
| 113 | + private function isClosingTag(string $part): bool |
| 114 | + { |
| 115 | + return (bool) preg_match('/^\s*<\//', $part); |
| 116 | + } |
| 117 | + |
| 118 | + private function isOpeningCdataTag(string $part): bool |
| 119 | + { |
| 120 | + return str_contains($part, '<![CDATA['); |
| 121 | + } |
| 122 | + |
| 123 | + private function isClosingCdataTag(string $part): bool |
| 124 | + { |
| 125 | + return str_contains($part, ']]>'); |
| 126 | + } |
| 127 | +} |
0 commit comments