| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace AcyMailing\Libraries\Sabberworm\CSS; |
| 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 |
*/ |
| 13 |
class OutputFormatter |
| 14 |
{ |
| 15 |
/** |
| 16 |
* @var OutputFormat |
| 17 |
*/ |
| 18 |
private $outputFormat; |
| 19 |
|
| 20 |
public function __construct(OutputFormat $outputFormat) |
| 21 |
{ |
| 22 |
$this->outputFormat = $outputFormat; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* @param non-empty-string $name |
| 27 |
* |
| 28 |
* @throws \InvalidArgumentException |
| 29 |
*/ |
| 30 |
public function space(string $name): string |
| 31 |
{ |
| 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); |
| 71 |
} |
| 72 |
|
| 73 |
return $this->prepareSpace($spaceString); |
| 74 |
} |
| 75 |
|
| 76 |
public function spaceAfterRuleName(): string |
| 77 |
{ |
| 78 |
return $this->space('AfterRuleName'); |
| 79 |
} |
| 80 |
|
| 81 |
public function spaceBeforeRules(): string |
| 82 |
{ |
| 83 |
return $this->space('BeforeRules'); |
| 84 |
} |
| 85 |
|
| 86 |
public function spaceAfterRules(): string |
| 87 |
{ |
| 88 |
return $this->space('AfterRules'); |
| 89 |
} |
| 90 |
|
| 91 |
public function spaceBetweenRules(): string |
| 92 |
{ |
| 93 |
return $this->space('BetweenRules'); |
| 94 |
} |
| 95 |
|
| 96 |
public function spaceBeforeBlocks(): string |
| 97 |
{ |
| 98 |
return $this->space('BeforeBlocks'); |
| 99 |
} |
| 100 |
|
| 101 |
public function spaceAfterBlocks(): string |
| 102 |
{ |
| 103 |
return $this->space('AfterBlocks'); |
| 104 |
} |
| 105 |
|
| 106 |
public function spaceBetweenBlocks(): string |
| 107 |
{ |
| 108 |
return $this->space('BetweenBlocks'); |
| 109 |
} |
| 110 |
|
| 111 |
public function spaceBeforeSelectorSeparator(): string |
| 112 |
{ |
| 113 |
return $this->space('BeforeSelectorSeparator'); |
| 114 |
} |
| 115 |
|
| 116 |
public function spaceAfterSelectorSeparator(): string |
| 117 |
{ |
| 118 |
return $this->space('AfterSelectorSeparator'); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* @param non-empty-string $separator |
| 123 |
*/ |
| 124 |
public function spaceBeforeListArgumentSeparator(string $separator): string |
| 125 |
{ |
| 126 |
$spaceForSeparator = $this->outputFormat->getSpaceBeforeListArgumentSeparators(); |
| 127 |
|
| 128 |
return $spaceForSeparator[$separator] ?? $this->space('BeforeListArgumentSeparator'); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* @param non-empty-string $separator |
| 133 |
*/ |
| 134 |
public function spaceAfterListArgumentSeparator(string $separator): string |
| 135 |
{ |
| 136 |
$spaceForSeparator = $this->outputFormat->getSpaceAfterListArgumentSeparators(); |
| 137 |
|
| 138 |
return $spaceForSeparator[$separator] ?? $this->space('AfterListArgumentSeparator'); |
| 139 |
} |
| 140 |
|
| 141 |
public function spaceBeforeOpeningBrace(): string |
| 142 |
{ |
| 143 |
return $this->space('BeforeOpeningBrace'); |
| 144 |
} |
| 145 |
|
| 146 |
/** |
| 147 |
* Runs the given code, either swallowing or passing exceptions, depending on the `ignoreExceptions` setting. |
| 148 |
*/ |
| 149 |
public function safely(callable $callable): ?string |
| 150 |
{ |
| 151 |
if ($this->outputFormat->shouldIgnoreExceptions()) { |
| 152 |
// If output exceptions are ignored, run the code with exception guards |
| 153 |
try { |
| 154 |
return $callable(); |
| 155 |
} catch (OutputException $e) { |
| 156 |
return null; |
| 157 |
} // Do nothing |
| 158 |
} else { |
| 159 |
// Run the code as-is |
| 160 |
return $callable(); |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
/** |
| 165 |
* Clone of the `implode` function, but calls `render` with the current output format. |
| 166 |
* |
| 167 |
* @param array<array-key, Renderable|string> $values |
| 168 |
*/ |
| 169 |
public function implode(string $separator, array $values, bool $increaseLevel = false): string |
| 170 |
{ |
| 171 |
$result = ''; |
| 172 |
$outputFormat = $this->outputFormat; |
| 173 |
if ($increaseLevel) { |
| 174 |
$outputFormat = $outputFormat->nextLevel(); |
| 175 |
} |
| 176 |
$isFirst = true; |
| 177 |
foreach ($values as $value) { |
| 178 |
if ($isFirst) { |
| 179 |
$isFirst = false; |
| 180 |
} else { |
| 181 |
$result .= $separator; |
| 182 |
} |
| 183 |
if ($value instanceof Renderable) { |
| 184 |
$result .= $value->render($outputFormat); |
| 185 |
} else { |
| 186 |
$result .= $value; |
| 187 |
} |
| 188 |
} |
| 189 |
return $result; |
| 190 |
} |
| 191 |
|
| 192 |
public function removeLastSemicolon(string $string): string |
| 193 |
{ |
| 194 |
if ($this->outputFormat->shouldRenderSemicolonAfterLastRule()) { |
| 195 |
return $string; |
| 196 |
} |
| 197 |
|
| 198 |
$parts = \explode(';', $string); |
| 199 |
if (\count($parts) < 2) { |
| 200 |
return $parts[0]; |
| 201 |
} |
| 202 |
$lastPart = \array_pop($parts); |
| 203 |
$nextToLastPart = \array_pop($parts); |
| 204 |
\array_push($parts, $nextToLastPart . $lastPart); |
| 205 |
|
| 206 |
return \implode(';', $parts); |
| 207 |
} |
| 208 |
|
| 209 |
public function comments(Commentable $commentable): string |
| 210 |
{ |
| 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; |
| 224 |
} |
| 225 |
|
| 226 |
private function prepareSpace(string $spaceString): string |
| 227 |
{ |
| 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()); |
| 234 |
} |
| 235 |
} |
| 236 |
|