Merge branch 'MDL-81457-main' of https://github.com/andrewnicols/moodle
[moodle.git] / lib / php-css-parser / OutputFormatter.php
blob535feca7ff9d1383fd82c8a319415dd9bbc9fe5b
1 <?php
3 namespace Sabberworm\CSS;
5 use Sabberworm\CSS\Parsing\OutputException;
7 class OutputFormatter
9 /**
10 * @var OutputFormat
12 private $oFormat;
14 public function __construct(OutputFormat $oFormat)
16 $this->oFormat = $oFormat;
19 /**
20 * @param string $sName
21 * @param string|null $sType
23 * @return string
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];
33 } else {
34 $sSpaceString = reset($sSpaceString);
37 return $this->prepareSpace($sSpaceString);
40 /**
41 * @return string
43 public function spaceAfterRuleName()
45 return $this->space('AfterRuleName');
48 /**
49 * @return string
51 public function spaceBeforeRules()
53 return $this->space('BeforeRules');
56 /**
57 * @return string
59 public function spaceAfterRules()
61 return $this->space('AfterRules');
64 /**
65 * @return string
67 public function spaceBetweenRules()
69 return $this->space('BetweenRules');
72 /**
73 * @return string
75 public function spaceBeforeBlocks()
77 return $this->space('BeforeBlocks');
80 /**
81 * @return string
83 public function spaceAfterBlocks()
85 return $this->space('AfterBlocks');
88 /**
89 * @return string
91 public function spaceBetweenBlocks()
93 return $this->space('BetweenBlocks');
96 /**
97 * @return string
99 public function spaceBeforeSelectorSeparator()
101 return $this->space('BeforeSelectorSeparator');
105 * @return string
107 public function spaceAfterSelectorSeparator()
109 return $this->space('AfterSelectorSeparator');
113 * @param string $sSeparator
115 * @return string
117 public function spaceBeforeListArgumentSeparator($sSeparator)
119 return $this->space('BeforeListArgumentSeparator', $sSeparator);
123 * @param string $sSeparator
125 * @return string
127 public function spaceAfterListArgumentSeparator($sSeparator)
129 return $this->space('AfterListArgumentSeparator', $sSeparator);
133 * @return string
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
151 try {
152 return $cCode();
153 } catch (OutputException $e) {
154 return null;
155 } // Do nothing
156 } else {
157 // Run the code as-is
158 return $cCode();
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
169 * @return string
171 public function implode($sSeparator, array $aValues, $bIncreaseLevel = false)
173 $sResult = '';
174 $oFormat = $this->oFormat;
175 if ($bIncreaseLevel) {
176 $oFormat = $oFormat->nextLevel();
178 $bIsFirst = true;
179 foreach ($aValues as $mValue) {
180 if ($bIsFirst) {
181 $bIsFirst = false;
182 } else {
183 $sResult .= $sSeparator;
185 if ($mValue instanceof Renderable) {
186 $sResult .= $mValue->render($oFormat);
187 } else {
188 $sResult .= $mValue;
191 return $sResult;
195 * @param string $sString
197 * @return string
199 public function removeLastSemicolon($sString)
201 if ($this->oFormat->get('SemicolonAfterLastRule')) {
202 return $sString;
204 $sString = explode(';', $sString);
205 if (count($sString) < 2) {
206 return $sString[0];
208 $sLast = array_pop($sString);
209 $sNextToLast = array_pop($sString);
210 array_push($sString, $sNextToLast . $sLast);
211 return implode(';', $sString);
215 * @param string $sSpaceString
217 * @return string
219 private function prepareSpace($sSpaceString)
221 return str_replace("\n", "\n" . $this->indent(), $sSpaceString);
225 * @return string
227 private function indent()
229 return str_repeat($this->oFormat->sIndentation, $this->oFormat->level());