OutputFormat.php
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\Sabberworm\CSS; |
| 4 | |
| 5 | /** |
| 6 | * Class OutputFormat |
| 7 | * |
| 8 | * @method OutputFormat setSemicolonAfterLastRule(bool $bSemicolonAfterLastRule) Set whether semicolons are added after |
| 9 | * last rule. |
| 10 | */ |
| 11 | class OutputFormat |
| 12 | { |
| 13 | /** |
| 14 | * Value format: `"` means double-quote, `'` means single-quote |
| 15 | * |
| 16 | * @var string |
| 17 | */ |
| 18 | public $sStringQuotingType = '"'; |
| 19 | /** |
| 20 | * Output RGB colors in hash notation if possible |
| 21 | * |
| 22 | * @var string |
| 23 | */ |
| 24 | public $bRGBHashNotation = \true; |
| 25 | /** |
| 26 | * Declaration format |
| 27 | * |
| 28 | * Semicolon after the last rule of a declaration block can be omitted. To do that, set this false. |
| 29 | * |
| 30 | * @var bool |
| 31 | */ |
| 32 | public $bSemicolonAfterLastRule = \true; |
| 33 | /** |
| 34 | * Spacing |
| 35 | * Note that these strings are not sanity-checked: the value should only consist of whitespace |
| 36 | * Any newline character will be indented according to the current level. |
| 37 | * The triples (After, Before, Between) can be set using a wildcard (e.g. `$oFormat->set('Space*Rules', "\n");`) |
| 38 | */ |
| 39 | public $sSpaceAfterRuleName = ' '; |
| 40 | /** |
| 41 | * @var string |
| 42 | */ |
| 43 | public $sSpaceBeforeRules = ''; |
| 44 | /** |
| 45 | * @var string |
| 46 | */ |
| 47 | public $sSpaceAfterRules = ''; |
| 48 | /** |
| 49 | * @var string |
| 50 | */ |
| 51 | public $sSpaceBetweenRules = ''; |
| 52 | /** |
| 53 | * @var string |
| 54 | */ |
| 55 | public $sSpaceBeforeBlocks = ''; |
| 56 | /** |
| 57 | * @var string |
| 58 | */ |
| 59 | public $sSpaceAfterBlocks = ''; |
| 60 | /** |
| 61 | * @var string |
| 62 | */ |
| 63 | public $sSpaceBetweenBlocks = "\n"; |
| 64 | /** |
| 65 | * Content injected in and around at-rule blocks. |
| 66 | * |
| 67 | * @var string |
| 68 | */ |
| 69 | public $sBeforeAtRuleBlock = ''; |
| 70 | /** |
| 71 | * @var string |
| 72 | */ |
| 73 | public $sAfterAtRuleBlock = ''; |
| 74 | /** |
| 75 | * This is what’s printed before and after the comma if a declaration block contains multiple selectors. |
| 76 | * |
| 77 | * @var string |
| 78 | */ |
| 79 | public $sSpaceBeforeSelectorSeparator = ''; |
| 80 | /** |
| 81 | * @var string |
| 82 | */ |
| 83 | public $sSpaceAfterSelectorSeparator = ' '; |
| 84 | /** |
| 85 | * This is what’s printed after the comma of value lists |
| 86 | * |
| 87 | * @var string |
| 88 | */ |
| 89 | public $sSpaceBeforeListArgumentSeparator = ''; |
| 90 | /** |
| 91 | * @var string |
| 92 | */ |
| 93 | public $sSpaceAfterListArgumentSeparator = ''; |
| 94 | /** |
| 95 | * @var string |
| 96 | */ |
| 97 | public $sSpaceBeforeOpeningBrace = ' '; |
| 98 | /** |
| 99 | * Content injected in and around declaration blocks. |
| 100 | * |
| 101 | * @var string |
| 102 | */ |
| 103 | public $sBeforeDeclarationBlock = ''; |
| 104 | /** |
| 105 | * @var string |
| 106 | */ |
| 107 | public $sAfterDeclarationBlockSelectors = ''; |
| 108 | /** |
| 109 | * @var string |
| 110 | */ |
| 111 | public $sAfterDeclarationBlock = ''; |
| 112 | /** |
| 113 | * Indentation character(s) per level. Only applicable if newlines are used in any of the spacing settings. |
| 114 | * |
| 115 | * @var string |
| 116 | */ |
| 117 | public $sIndentation = "\t"; |
| 118 | /** |
| 119 | * Output exceptions. |
| 120 | * |
| 121 | * @var bool |
| 122 | */ |
| 123 | public $bIgnoreExceptions = \false; |
| 124 | /** |
| 125 | * @var OutputFormatter|null |
| 126 | */ |
| 127 | private $oFormatter = null; |
| 128 | /** |
| 129 | * @var OutputFormat|null |
| 130 | */ |
| 131 | private $oNextLevelFormat = null; |
| 132 | /** |
| 133 | * @var int |
| 134 | */ |
| 135 | private $iIndentationLevel = 0; |
| 136 | public function __construct() |
| 137 | { |
| 138 | } |
| 139 | /** |
| 140 | * @param string $sName |
| 141 | * |
| 142 | * @return string|null |
| 143 | */ |
| 144 | public function get($sName) |
| 145 | { |
| 146 | $aVarPrefixes = ['a', 's', 'm', 'b', 'f', 'o', 'c', 'i']; |
| 147 | foreach ($aVarPrefixes as $sPrefix) { |
| 148 | $sFieldName = $sPrefix . \ucfirst($sName); |
| 149 | if (isset($this->{$sFieldName})) { |
| 150 | return $this->{$sFieldName}; |
| 151 | } |
| 152 | } |
| 153 | return null; |
| 154 | } |
| 155 | /** |
| 156 | * @param array<array-key, string>|string $aNames |
| 157 | * @param mixed $mValue |
| 158 | * |
| 159 | * @return self|false |
| 160 | */ |
| 161 | public function set($aNames, $mValue) |
| 162 | { |
| 163 | $aVarPrefixes = ['a', 's', 'm', 'b', 'f', 'o', 'c', 'i']; |
| 164 | if (\is_string($aNames) && \strpos($aNames, '*') !== \false) { |
| 165 | $aNames = [\str_replace('*', 'Before', $aNames), \str_replace('*', 'Between', $aNames), \str_replace('*', 'After', $aNames)]; |
| 166 | } elseif (!\is_array($aNames)) { |
| 167 | $aNames = [$aNames]; |
| 168 | } |
| 169 | foreach ($aVarPrefixes as $sPrefix) { |
| 170 | $bDidReplace = \false; |
| 171 | foreach ($aNames as $sName) { |
| 172 | $sFieldName = $sPrefix . \ucfirst($sName); |
| 173 | if (isset($this->{$sFieldName})) { |
| 174 | $this->{$sFieldName} = $mValue; |
| 175 | $bDidReplace = \true; |
| 176 | } |
| 177 | } |
| 178 | if ($bDidReplace) { |
| 179 | return $this; |
| 180 | } |
| 181 | } |
| 182 | // Break the chain so the user knows this option is invalid |
| 183 | return \false; |
| 184 | } |
| 185 | /** |
| 186 | * @param string $sMethodName |
| 187 | * @param array<array-key, mixed> $aArguments |
| 188 | * |
| 189 | * @return mixed |
| 190 | * |
| 191 | * @throws \Exception |
| 192 | */ |
| 193 | public function __call($sMethodName, array $aArguments) |
| 194 | { |
| 195 | if (\strpos($sMethodName, 'set') === 0) { |
| 196 | return $this->set(\substr($sMethodName, 3), $aArguments[0]); |
| 197 | } elseif (\strpos($sMethodName, 'get') === 0) { |
| 198 | return $this->get(\substr($sMethodName, 3)); |
| 199 | } elseif (\method_exists(OutputFormatter::class, $sMethodName)) { |
| 200 | return \call_user_func_array([$this->getFormatter(), $sMethodName], $aArguments); |
| 201 | } else { |
| 202 | throw new \Exception('Unknown OutputFormat method called: ' . $sMethodName); |
| 203 | } |
| 204 | } |
| 205 | /** |
| 206 | * @param int $iNumber |
| 207 | * |
| 208 | * @return self |
| 209 | */ |
| 210 | public function indentWithTabs($iNumber = 1) |
| 211 | { |
| 212 | return $this->setIndentation(\str_repeat("\t", $iNumber)); |
| 213 | } |
| 214 | /** |
| 215 | * @param int $iNumber |
| 216 | * |
| 217 | * @return self |
| 218 | */ |
| 219 | public function indentWithSpaces($iNumber = 2) |
| 220 | { |
| 221 | return $this->setIndentation(\str_repeat(" ", $iNumber)); |
| 222 | } |
| 223 | /** |
| 224 | * @return OutputFormat |
| 225 | */ |
| 226 | public function nextLevel() |
| 227 | { |
| 228 | if ($this->oNextLevelFormat === null) { |
| 229 | $this->oNextLevelFormat = clone $this; |
| 230 | $this->oNextLevelFormat->iIndentationLevel++; |
| 231 | $this->oNextLevelFormat->oFormatter = null; |
| 232 | } |
| 233 | return $this->oNextLevelFormat; |
| 234 | } |
| 235 | /** |
| 236 | * @return void |
| 237 | */ |
| 238 | public function beLenient() |
| 239 | { |
| 240 | $this->bIgnoreExceptions = \true; |
| 241 | } |
| 242 | /** |
| 243 | * @return OutputFormatter |
| 244 | */ |
| 245 | public function getFormatter() |
| 246 | { |
| 247 | if ($this->oFormatter === null) { |
| 248 | $this->oFormatter = new OutputFormatter($this); |
| 249 | } |
| 250 | return $this->oFormatter; |
| 251 | } |
| 252 | /** |
| 253 | * @return int |
| 254 | */ |
| 255 | public function level() |
| 256 | { |
| 257 | return $this->iIndentationLevel; |
| 258 | } |
| 259 | /** |
| 260 | * Creates an instance of this class without any particular formatting settings. |
| 261 | * |
| 262 | * @return self |
| 263 | */ |
| 264 | public static function create() |
| 265 | { |
| 266 | return new OutputFormat(); |
| 267 | } |
| 268 | /** |
| 269 | * Creates an instance of this class with a preset for compact formatting. |
| 270 | * |
| 271 | * @return self |
| 272 | */ |
| 273 | public static function createCompact() |
| 274 | { |
| 275 | $format = self::create(); |
| 276 | $format->set('Space*Rules', "")->set('Space*Blocks', "")->setSpaceAfterRuleName('')->setSpaceBeforeOpeningBrace('')->setSpaceAfterSelectorSeparator(''); |
| 277 | return $format; |
| 278 | } |
| 279 | /** |
| 280 | * Creates an instance of this class with a preset for pretty formatting. |
| 281 | * |
| 282 | * @return self |
| 283 | */ |
| 284 | public static function createPretty() |
| 285 | { |
| 286 | $format = self::create(); |
| 287 | $format->set('Space*Rules', "\n")->set('Space*Blocks', "\n")->setSpaceBetweenBlocks("\n\n")->set('SpaceAfterListArgumentSeparator', ['default' => '', ',' => ' ']); |
| 288 | return $format; |
| 289 | } |
| 290 | } |
| 291 |