Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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