CSSList
3 years ago
Comment
3 years ago
Parsing
3 years ago
Property
3 years ago
Rule
3 years ago
RuleSet
3 years ago
Value
3 years ago
OutputFormat.php
3 years ago
OutputFormatter.php
3 years ago
Parser.php
3 years ago
Renderable.php
3 years ago
Settings.php
3 years ago
OutputFormatter.php
213 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Sabberworm\CSS; |
| 4 | |
| 5 | use IAWP\Sabberworm\CSS\Parsing\OutputException; |
| 6 | class OutputFormatter |
| 7 | { |
| 8 | /** |
| 9 | * @var OutputFormat |
| 10 | */ |
| 11 | private $oFormat; |
| 12 | public function __construct(OutputFormat $oFormat) |
| 13 | { |
| 14 | $this->oFormat = $oFormat; |
| 15 | } |
| 16 | /** |
| 17 | * @param string $sName |
| 18 | * @param string|null $sType |
| 19 | * |
| 20 | * @return string |
| 21 | */ |
| 22 | public function space($sName, $sType = null) |
| 23 | { |
| 24 | $sSpaceString = $this->oFormat->get("Space{$sName}"); |
| 25 | // If $sSpaceString is an array, we have multiple values configured |
| 26 | // depending on the type of object the space applies to |
| 27 | if (\is_array($sSpaceString)) { |
| 28 | if ($sType !== null && isset($sSpaceString[$sType])) { |
| 29 | $sSpaceString = $sSpaceString[$sType]; |
| 30 | } else { |
| 31 | $sSpaceString = \reset($sSpaceString); |
| 32 | } |
| 33 | } |
| 34 | return $this->prepareSpace($sSpaceString); |
| 35 | } |
| 36 | /** |
| 37 | * @return string |
| 38 | */ |
| 39 | public function spaceAfterRuleName() |
| 40 | { |
| 41 | return $this->space('AfterRuleName'); |
| 42 | } |
| 43 | /** |
| 44 | * @return string |
| 45 | */ |
| 46 | public function spaceBeforeRules() |
| 47 | { |
| 48 | return $this->space('BeforeRules'); |
| 49 | } |
| 50 | /** |
| 51 | * @return string |
| 52 | */ |
| 53 | public function spaceAfterRules() |
| 54 | { |
| 55 | return $this->space('AfterRules'); |
| 56 | } |
| 57 | /** |
| 58 | * @return string |
| 59 | */ |
| 60 | public function spaceBetweenRules() |
| 61 | { |
| 62 | return $this->space('BetweenRules'); |
| 63 | } |
| 64 | /** |
| 65 | * @return string |
| 66 | */ |
| 67 | public function spaceBeforeBlocks() |
| 68 | { |
| 69 | return $this->space('BeforeBlocks'); |
| 70 | } |
| 71 | /** |
| 72 | * @return string |
| 73 | */ |
| 74 | public function spaceAfterBlocks() |
| 75 | { |
| 76 | return $this->space('AfterBlocks'); |
| 77 | } |
| 78 | /** |
| 79 | * @return string |
| 80 | */ |
| 81 | public function spaceBetweenBlocks() |
| 82 | { |
| 83 | return $this->space('BetweenBlocks'); |
| 84 | } |
| 85 | /** |
| 86 | * @return string |
| 87 | */ |
| 88 | public function spaceBeforeSelectorSeparator() |
| 89 | { |
| 90 | return $this->space('BeforeSelectorSeparator'); |
| 91 | } |
| 92 | /** |
| 93 | * @return string |
| 94 | */ |
| 95 | public function spaceAfterSelectorSeparator() |
| 96 | { |
| 97 | return $this->space('AfterSelectorSeparator'); |
| 98 | } |
| 99 | /** |
| 100 | * @param string $sSeparator |
| 101 | * |
| 102 | * @return string |
| 103 | */ |
| 104 | public function spaceBeforeListArgumentSeparator($sSeparator) |
| 105 | { |
| 106 | return $this->space('BeforeListArgumentSeparator', $sSeparator); |
| 107 | } |
| 108 | /** |
| 109 | * @param string $sSeparator |
| 110 | * |
| 111 | * @return string |
| 112 | */ |
| 113 | public function spaceAfterListArgumentSeparator($sSeparator) |
| 114 | { |
| 115 | return $this->space('AfterListArgumentSeparator', $sSeparator); |
| 116 | } |
| 117 | /** |
| 118 | * @return string |
| 119 | */ |
| 120 | public function spaceBeforeOpeningBrace() |
| 121 | { |
| 122 | return $this->space('BeforeOpeningBrace'); |
| 123 | } |
| 124 | /** |
| 125 | * Runs the given code, either swallowing or passing exceptions, depending on the `bIgnoreExceptions` setting. |
| 126 | * |
| 127 | * @param string $cCode the name of the function to call |
| 128 | * |
| 129 | * @return string|null |
| 130 | */ |
| 131 | public function safely($cCode) |
| 132 | { |
| 133 | if ($this->oFormat->get('IgnoreExceptions')) { |
| 134 | // If output exceptions are ignored, run the code with exception guards |
| 135 | try { |
| 136 | return $cCode(); |
| 137 | } catch (OutputException $e) { |
| 138 | return null; |
| 139 | } |
| 140 | // Do nothing |
| 141 | } else { |
| 142 | // Run the code as-is |
| 143 | return $cCode(); |
| 144 | } |
| 145 | } |
| 146 | /** |
| 147 | * Clone of the `implode` function, but calls `render` with the current output format instead of `__toString()`. |
| 148 | * |
| 149 | * @param string $sSeparator |
| 150 | * @param array<array-key, Renderable|string> $aValues |
| 151 | * @param bool $bIncreaseLevel |
| 152 | * |
| 153 | * @return string |
| 154 | */ |
| 155 | public function implode($sSeparator, array $aValues, $bIncreaseLevel = \false) |
| 156 | { |
| 157 | $sResult = ''; |
| 158 | $oFormat = $this->oFormat; |
| 159 | if ($bIncreaseLevel) { |
| 160 | $oFormat = $oFormat->nextLevel(); |
| 161 | } |
| 162 | $bIsFirst = \true; |
| 163 | foreach ($aValues as $mValue) { |
| 164 | if ($bIsFirst) { |
| 165 | $bIsFirst = \false; |
| 166 | } else { |
| 167 | $sResult .= $sSeparator; |
| 168 | } |
| 169 | if ($mValue instanceof Renderable) { |
| 170 | $sResult .= $mValue->render($oFormat); |
| 171 | } else { |
| 172 | $sResult .= $mValue; |
| 173 | } |
| 174 | } |
| 175 | return $sResult; |
| 176 | } |
| 177 | /** |
| 178 | * @param string $sString |
| 179 | * |
| 180 | * @return string |
| 181 | */ |
| 182 | public function removeLastSemicolon($sString) |
| 183 | { |
| 184 | if ($this->oFormat->get('SemicolonAfterLastRule')) { |
| 185 | return $sString; |
| 186 | } |
| 187 | $sString = \explode(';', $sString); |
| 188 | if (\count($sString) < 2) { |
| 189 | return $sString[0]; |
| 190 | } |
| 191 | $sLast = \array_pop($sString); |
| 192 | $sNextToLast = \array_pop($sString); |
| 193 | \array_push($sString, $sNextToLast . $sLast); |
| 194 | return \implode(';', $sString); |
| 195 | } |
| 196 | /** |
| 197 | * @param string $sSpaceString |
| 198 | * |
| 199 | * @return string |
| 200 | */ |
| 201 | private function prepareSpace($sSpaceString) |
| 202 | { |
| 203 | return \str_replace("\n", "\n" . $this->indent(), $sSpaceString); |
| 204 | } |
| 205 | /** |
| 206 | * @return string |
| 207 | */ |
| 208 | private function indent() |
| 209 | { |
| 210 | return \str_repeat($this->oFormat->sIndentation, $this->oFormat->level()); |
| 211 | } |
| 212 | } |
| 213 |