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