← All changes
|
back/Libraries/Sabberworm/CSS/src/OutputFormatter.php
+129
-125
10.2.2
→
11.1.0
View file →
| @@ -1,231 +1,235 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace Sabberworm\CSS; | |
| 3 | +declare(strict_types=1); | |
| 4 | 4 | |
| 5 | -use Sabberworm\CSS\Parsing\OutputException; | |
| 5 | +namespace AcyMailing\Libraries\Sabberworm\CSS; | |
| 6 | 6 | |
| 7 | +use AcyMailing\Libraries\Sabberworm\CSS\Comment\Commentable; | |
| 8 | +use AcyMailing\Libraries\Sabberworm\CSS\Parsing\OutputException; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * @internal since 8.8.0 | |
| 12 | + */ | |
| 7 | 13 | class OutputFormatter |
| 8 | 14 | { |
| 9 | 15 | /** |
| 10 | 16 | * @var OutputFormat |
| 11 | 17 | */ |
| 12 | - private $oFormat; | |
| 18 | + private $outputFormat; | |
| 13 | 19 | |
| 14 | - public function __construct(OutputFormat $oFormat) | |
| 20 | + public function __construct(OutputFormat $outputFormat) | |
| 15 | 21 | { |
| 16 | - $this->oFormat = $oFormat; | |
| 22 | + $this->outputFormat = $outputFormat; | |
| 17 | 23 | } |
| 18 | 24 | |
| 19 | 25 | /** |
| 20 | - * @param string $sName | |
| 21 | - * @param string|null $sType | |
| 26 | + * @param non-empty-string $name | |
| 22 | 27 | * |
| 23 | - * @return string | |
| 28 | + * @throws \InvalidArgumentException | |
| 24 | 29 | */ |
| 25 | - public function space($sName, $sType = null) | |
| 30 | + public function space(string $name): string | |
| 26 | 31 | { |
| 27 | - $sSpaceString = $this->oFormat->get("Space$sName"); | |
| 28 | - // If $sSpaceString is an array, we have multiple values configured | |
| 29 | - // depending on the type of object the space applies to | |
| 30 | - if (is_array($sSpaceString)) { | |
| 31 | - if ($sType !== null && isset($sSpaceString[$sType])) { | |
| 32 | - $sSpaceString = $sSpaceString[$sType]; | |
| 33 | - } else { | |
| 34 | - $sSpaceString = reset($sSpaceString); | |
| 35 | - } | |
| 32 | + switch ($name) { | |
| 33 | + case 'AfterRuleName': | |
| 34 | + $spaceString = $this->outputFormat->getSpaceAfterRuleName(); | |
| 35 | + break; | |
| 36 | + case 'BeforeRules': | |
| 37 | + $spaceString = $this->outputFormat->getSpaceBeforeRules(); | |
| 38 | + break; | |
| 39 | + case 'AfterRules': | |
| 40 | + $spaceString = $this->outputFormat->getSpaceAfterRules(); | |
| 41 | + break; | |
| 42 | + case 'BetweenRules': | |
| 43 | + $spaceString = $this->outputFormat->getSpaceBetweenRules(); | |
| 44 | + break; | |
| 45 | + case 'BeforeBlocks': | |
| 46 | + $spaceString = $this->outputFormat->getSpaceBeforeBlocks(); | |
| 47 | + break; | |
| 48 | + case 'AfterBlocks': | |
| 49 | + $spaceString = $this->outputFormat->getSpaceAfterBlocks(); | |
| 50 | + break; | |
| 51 | + case 'BetweenBlocks': | |
| 52 | + $spaceString = $this->outputFormat->getSpaceBetweenBlocks(); | |
| 53 | + break; | |
| 54 | + case 'BeforeSelectorSeparator': | |
| 55 | + $spaceString = $this->outputFormat->getSpaceBeforeSelectorSeparator(); | |
| 56 | + break; | |
| 57 | + case 'AfterSelectorSeparator': | |
| 58 | + $spaceString = $this->outputFormat->getSpaceAfterSelectorSeparator(); | |
| 59 | + break; | |
| 60 | + case 'BeforeOpeningBrace': | |
| 61 | + $spaceString = $this->outputFormat->getSpaceBeforeOpeningBrace(); | |
| 62 | + break; | |
| 63 | + case 'BeforeListArgumentSeparator': | |
| 64 | + $spaceString = $this->outputFormat->getSpaceBeforeListArgumentSeparator(); | |
| 65 | + break; | |
| 66 | + case 'AfterListArgumentSeparator': | |
| 67 | + $spaceString = $this->outputFormat->getSpaceAfterListArgumentSeparator(); | |
| 68 | + break; | |
| 69 | + default: | |
| 70 | + throw new \InvalidArgumentException("Unknown space type: $name", 1740049248); | |
| 36 | 71 | } |
| 37 | - return $this->prepareSpace($sSpaceString); | |
| 72 | + | |
| 73 | + return $this->prepareSpace($spaceString); | |
| 38 | 74 | } |
| 39 | 75 | |
| 40 | - /** | |
| 41 | - * @return string | |
| 42 | - */ | |
| 43 | - public function spaceAfterRuleName() | |
| 76 | + public function spaceAfterRuleName(): string | |
| 44 | 77 | { |
| 45 | 78 | return $this->space('AfterRuleName'); |
| 46 | 79 | } |
| 47 | 80 | |
| 48 | - /** | |
| 49 | - * @return string | |
| 50 | - */ | |
| 51 | - public function spaceBeforeRules() | |
| 81 | + public function spaceBeforeRules(): string | |
| 52 | 82 | { |
| 53 | 83 | return $this->space('BeforeRules'); |
| 54 | 84 | } |
| 55 | 85 | |
| 56 | - /** | |
| 57 | - * @return string | |
| 58 | - */ | |
| 59 | - public function spaceAfterRules() | |
| 86 | + public function spaceAfterRules(): string | |
| 60 | 87 | { |
| 61 | 88 | return $this->space('AfterRules'); |
| 62 | 89 | } |
| 63 | 90 | |
| 64 | - /** | |
| 65 | - * @return string | |
| 66 | - */ | |
| 67 | - public function spaceBetweenRules() | |
| 91 | + public function spaceBetweenRules(): string | |
| 68 | 92 | { |
| 69 | 93 | return $this->space('BetweenRules'); |
| 70 | 94 | } |
| 71 | 95 | |
| 72 | - /** | |
| 73 | - * @return string | |
| 74 | - */ | |
| 75 | - public function spaceBeforeBlocks() | |
| 96 | + public function spaceBeforeBlocks(): string | |
| 76 | 97 | { |
| 77 | 98 | return $this->space('BeforeBlocks'); |
| 78 | 99 | } |
| 79 | 100 | |
| 80 | - /** | |
| 81 | - * @return string | |
| 82 | - */ | |
| 83 | - public function spaceAfterBlocks() | |
| 101 | + public function spaceAfterBlocks(): string | |
| 84 | 102 | { |
| 85 | 103 | return $this->space('AfterBlocks'); |
| 86 | 104 | } |
| 87 | 105 | |
| 88 | - /** | |
| 89 | - * @return string | |
| 90 | - */ | |
| 91 | - public function spaceBetweenBlocks() | |
| 106 | + public function spaceBetweenBlocks(): string | |
| 92 | 107 | { |
| 93 | 108 | return $this->space('BetweenBlocks'); |
| 94 | 109 | } |
| 95 | 110 | |
| 96 | - /** | |
| 97 | - * @return string | |
| 98 | - */ | |
| 99 | - public function spaceBeforeSelectorSeparator() | |
| 111 | + public function spaceBeforeSelectorSeparator(): string | |
| 100 | 112 | { |
| 101 | 113 | return $this->space('BeforeSelectorSeparator'); |
| 102 | 114 | } |
| 103 | 115 | |
| 104 | - /** | |
| 105 | - * @return string | |
| 106 | - */ | |
| 107 | - public function spaceAfterSelectorSeparator() | |
| 116 | + public function spaceAfterSelectorSeparator(): string | |
| 108 | 117 | { |
| 109 | 118 | return $this->space('AfterSelectorSeparator'); |
| 110 | 119 | } |
| 111 | 120 | |
| 112 | 121 | /** |
| 113 | - * @param string $sSeparator | |
| 114 | - * | |
| 115 | - * @return string | |
| 122 | + * @param non-empty-string $separator | |
| 116 | 123 | */ |
| 117 | - public function spaceBeforeListArgumentSeparator($sSeparator) | |
| 124 | + public function spaceBeforeListArgumentSeparator(string $separator): string | |
| 118 | 125 | { |
| 119 | - return $this->space('BeforeListArgumentSeparator', $sSeparator); | |
| 126 | + $spaceForSeparator = $this->outputFormat->getSpaceBeforeListArgumentSeparators(); | |
| 127 | + | |
| 128 | + return $spaceForSeparator[$separator] ?? $this->space('BeforeListArgumentSeparator'); | |
| 120 | 129 | } |
| 121 | 130 | |
| 122 | 131 | /** |
| 123 | - * @param string $sSeparator | |
| 124 | - * | |
| 125 | - * @return string | |
| 132 | + * @param non-empty-string $separator | |
| 126 | 133 | */ |
| 127 | - public function spaceAfterListArgumentSeparator($sSeparator) | |
| 134 | + public function spaceAfterListArgumentSeparator(string $separator): string | |
| 128 | 135 | { |
| 129 | - return $this->space('AfterListArgumentSeparator', $sSeparator); | |
| 136 | + $spaceForSeparator = $this->outputFormat->getSpaceAfterListArgumentSeparators(); | |
| 137 | + | |
| 138 | + return $spaceForSeparator[$separator] ?? $this->space('AfterListArgumentSeparator'); | |
| 130 | 139 | } |
| 131 | 140 | |
| 132 | - /** | |
| 133 | - * @return string | |
| 134 | - */ | |
| 135 | - public function spaceBeforeOpeningBrace() | |
| 141 | + public function spaceBeforeOpeningBrace(): string | |
| 136 | 142 | { |
| 137 | 143 | return $this->space('BeforeOpeningBrace'); |
| 138 | 144 | } |
| 139 | 145 | |
| 140 | 146 | /** |
| 141 | - * Runs the given code, either swallowing or passing exceptions, depending on the `bIgnoreExceptions` setting. | |
| 142 | - * | |
| 143 | - * @param string $cCode the name of the function to call | |
| 144 | - * | |
| 145 | - * @return string|null | |
| 147 | + * Runs the given code, either swallowing or passing exceptions, depending on the `ignoreExceptions` setting. | |
| 146 | 148 | */ |
| 147 | - public function safely($cCode) | |
| 149 | + public function safely(callable $callable): ?string | |
| 148 | 150 | { |
| 149 | - if ($this->oFormat->get('IgnoreExceptions')) { | |
| 151 | + if ($this->outputFormat->shouldIgnoreExceptions()) { | |
| 150 | 152 | // If output exceptions are ignored, run the code with exception guards |
| 151 | 153 | try { |
| 152 | - return $cCode(); | |
| 154 | + return $callable(); | |
| 153 | 155 | } catch (OutputException $e) { |
| 154 | 156 | return null; |
| 155 | 157 | } // Do nothing |
| 156 | 158 | } else { |
| 157 | 159 | // Run the code as-is |
| 158 | - return $cCode(); | |
| 160 | + return $callable(); | |
| 159 | 161 | } |
| 160 | 162 | } |
| 161 | 163 | |
| 162 | 164 | /** |
| 163 | - * Clone of the `implode` function, but calls `render` with the current output format instead of `__toString()`. | |
| 165 | + * Clone of the `implode` function, but calls `render` with the current output format. | |
| 164 | 166 | * |
| 165 | - * @param string $sSeparator | |
| 166 | - * @param array<array-key, Renderable|string> $aValues | |
| 167 | - * @param bool $bIncreaseLevel | |
| 168 | - * | |
| 169 | - * @return string | |
| 167 | + * @param array<array-key, Renderable|string> $values | |
| 170 | 168 | */ |
| 171 | - public function implode($sSeparator, array $aValues, $bIncreaseLevel = false) | |
| 169 | + public function implode(string $separator, array $values, bool $increaseLevel = false): string | |
| 172 | 170 | { |
| 173 | - $sResult = ''; | |
| 174 | - $oFormat = $this->oFormat; | |
| 175 | - if ($bIncreaseLevel) { | |
| 176 | - $oFormat = $oFormat->nextLevel(); | |
| 171 | + $result = ''; | |
| 172 | + $outputFormat = $this->outputFormat; | |
| 173 | + if ($increaseLevel) { | |
| 174 | + $outputFormat = $outputFormat->nextLevel(); | |
| 177 | 175 | } |
| 178 | - $bIsFirst = true; | |
| 179 | - foreach ($aValues as $mValue) { | |
| 180 | - if ($bIsFirst) { | |
| 181 | - $bIsFirst = false; | |
| 176 | + $isFirst = true; | |
| 177 | + foreach ($values as $value) { | |
| 178 | + if ($isFirst) { | |
| 179 | + $isFirst = false; | |
| 182 | 180 | } else { |
| 183 | - $sResult .= $sSeparator; | |
| 181 | + $result .= $separator; | |
| 184 | 182 | } |
| 185 | - if ($mValue instanceof Renderable) { | |
| 186 | - $sResult .= $mValue->render($oFormat); | |
| 183 | + if ($value instanceof Renderable) { | |
| 184 | + $result .= $value->render($outputFormat); | |
| 187 | 185 | } else { |
| 188 | - $sResult .= $mValue; | |
| 186 | + $result .= $value; | |
| 189 | 187 | } |
| 190 | 188 | } |
| 191 | - return $sResult; | |
| 189 | + return $result; | |
| 192 | 190 | } |
| 193 | 191 | |
| 194 | - /** | |
| 195 | - * @param string $sString | |
| 196 | - * | |
| 197 | - * @return string | |
| 198 | - */ | |
| 199 | - public function removeLastSemicolon($sString) | |
| 192 | + public function removeLastSemicolon(string $string): string | |
| 200 | 193 | { |
| 201 | - if ($this->oFormat->get('SemicolonAfterLastRule')) { | |
| 202 | - return $sString; | |
| 194 | + if ($this->outputFormat->shouldRenderSemicolonAfterLastRule()) { | |
| 195 | + return $string; | |
| 203 | 196 | } |
| 204 | - $sString = explode(';', $sString); | |
| 205 | - if (count($sString) < 2) { | |
| 206 | - return $sString[0]; | |
| 197 | + | |
| 198 | + $parts = \explode(';', $string); | |
| 199 | + if (\count($parts) < 2) { | |
| 200 | + return $parts[0]; | |
| 207 | 201 | } |
| 208 | - $sLast = array_pop($sString); | |
| 209 | - $sNextToLast = array_pop($sString); | |
| 210 | - array_push($sString, $sNextToLast . $sLast); | |
| 211 | - return implode(';', $sString); | |
| 202 | + $lastPart = \array_pop($parts); | |
| 203 | + $nextToLastPart = \array_pop($parts); | |
| 204 | + \array_push($parts, $nextToLastPart . $lastPart); | |
| 205 | + | |
| 206 | + return \implode(';', $parts); | |
| 212 | 207 | } |
| 213 | 208 | |
| 214 | - /** | |
| 215 | - * @param string $sSpaceString | |
| 216 | - * | |
| 217 | - * @return string | |
| 218 | - */ | |
| 219 | - private function prepareSpace($sSpaceString) | |
| 209 | + public function comments(Commentable $commentable): string | |
| 220 | 210 | { |
| 221 | - return str_replace("\n", "\n" . $this->indent(), $sSpaceString); | |
| 211 | + if (!$this->outputFormat->shouldRenderComments()) { | |
| 212 | + return ''; | |
| 213 | + } | |
| 214 | + | |
| 215 | + $result = ''; | |
| 216 | + $comments = $commentable->getComments(); | |
| 217 | + $lastCommentIndex = \count($comments) - 1; | |
| 218 | + | |
| 219 | + foreach ($comments as $i => $comment) { | |
| 220 | + $result .= $comment->render($this->outputFormat); | |
| 221 | + $result .= $i === $lastCommentIndex ? $this->spaceAfterBlocks() : $this->spaceBetweenBlocks(); | |
| 222 | + } | |
| 223 | + return $result; | |
| 222 | 224 | } |
| 223 | 225 | |
| 224 | - /** | |
| 225 | - * @return string | |
| 226 | - */ | |
| 227 | - private function indent() | |
| 226 | + private function prepareSpace(string $spaceString): string | |
| 228 | 227 | { |
| 229 | - return str_repeat($this->oFormat->sIndentation, $this->oFormat->level()); | |
| 228 | + return \str_replace("\n", "\n" . $this->indent(), $spaceString); | |
| 229 | + } | |
| 230 | + | |
| 231 | + private function indent(): string | |
| 232 | + { | |
| 233 | + return \str_repeat($this->outputFormat->getIndentation(), $this->outputFormat->getIndentationLevel()); | |
| 230 | 234 | } |
| 231 | 235 | } |