3 namespace Sabberworm\CSS
;
5 use Sabberworm\CSS\Parsing\OutputException
;
14 public function __construct(OutputFormat
$oFormat)
16 $this->oFormat
= $oFormat;
20 * @param string $sName
21 * @param string|null $sType
25 public function space($sName, $sType = null)
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];
34 $sSpaceString = reset($sSpaceString);
37 return $this->prepareSpace($sSpaceString);
43 public function spaceAfterRuleName()
45 return $this->space('AfterRuleName');
51 public function spaceBeforeRules()
53 return $this->space('BeforeRules');
59 public function spaceAfterRules()
61 return $this->space('AfterRules');
67 public function spaceBetweenRules()
69 return $this->space('BetweenRules');
75 public function spaceBeforeBlocks()
77 return $this->space('BeforeBlocks');
83 public function spaceAfterBlocks()
85 return $this->space('AfterBlocks');
91 public function spaceBetweenBlocks()
93 return $this->space('BetweenBlocks');
99 public function spaceBeforeSelectorSeparator()
101 return $this->space('BeforeSelectorSeparator');
107 public function spaceAfterSelectorSeparator()
109 return $this->space('AfterSelectorSeparator');
113 * @param string $sSeparator
117 public function spaceBeforeListArgumentSeparator($sSeparator)
119 return $this->space('BeforeListArgumentSeparator', $sSeparator);
123 * @param string $sSeparator
127 public function spaceAfterListArgumentSeparator($sSeparator)
129 return $this->space('AfterListArgumentSeparator', $sSeparator);
135 public function spaceBeforeOpeningBrace()
137 return $this->space('BeforeOpeningBrace');
141 * Runs the given code, either swallowing or passing exceptions, depending on the `bIgnoreExceptions` setting.
143 * @param string $cCode the name of the function to call
145 * @return string|null
147 public function safely($cCode)
149 if ($this->oFormat
->get('IgnoreExceptions')) {
150 // If output exceptions are ignored, run the code with exception guards
153 } catch (OutputException
$e) {
157 // Run the code as-is
163 * Clone of the `implode` function, but calls `render` with the current output format instead of `__toString()`.
165 * @param string $sSeparator
166 * @param array<array-key, Renderable|string> $aValues
167 * @param bool $bIncreaseLevel
171 public function implode($sSeparator, array $aValues, $bIncreaseLevel = false)
174 $oFormat = $this->oFormat
;
175 if ($bIncreaseLevel) {
176 $oFormat = $oFormat->nextLevel();
179 foreach ($aValues as $mValue) {
183 $sResult .= $sSeparator;
185 if ($mValue instanceof Renderable
) {
186 $sResult .= $mValue->render($oFormat);
195 * @param string $sString
199 public function removeLastSemicolon($sString)
201 if ($this->oFormat
->get('SemicolonAfterLastRule')) {
204 $sString = explode(';', $sString);
205 if (count($sString) < 2) {
208 $sLast = array_pop($sString);
209 $sNextToLast = array_pop($sString);
210 array_push($sString, $sNextToLast . $sLast);
211 return implode(';', $sString);
215 * @param string $sSpaceString
219 private function prepareSpace($sSpaceString)
221 return str_replace("\n", "\n" . $this->indent(), $sSpaceString);
227 private function indent()
229 return str_repeat($this->oFormat
->sIndentation
, $this->oFormat
->level());