Debian Codegraph @

vendor/sabberworm/php-css-parser/src/Settings.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace Sabberworm\CSS;
6
7 /**
8 * Parser settings class.
9 *
10 * Configure parser behaviour here.
11 */
12 class Settings
13 {
14 /**
15 * Multi-byte string support.
16 *
17 * If `true` (`mbstring` extension must be enabled), will use (slower) `mb_strlen`, `mb_convert_case`, `mb_substr`
18 * and `mb_strpos` functions. Otherwise, the normal (ASCII-Only) functions will be used.
19 *
20 * @var bool
21 */
22 private $multibyteSupport;
23
24 /**
25 * The default charset for the CSS if no `@charset` declaration is found. Defaults to utf-8.
26 *
27 * @var non-empty-string
28 */
29 private $defaultCharset = 'utf-8';
30
31 /**
32 * Whether the parser silently ignore invalid rules instead of choking on them.
33 *
34 * @var bool
35 */
36 private $lenientParsing = true;
37
38 private function __construct()
39 {
40 $this->multibyteSupport = \extension_loaded('mbstring');
41 }
42
43 public static function create(): self
44 {
45 return new Settings();
46 }
47
48 /**
49 * Enables/disables multi-byte string support.
50 *
51 * If `true` (`mbstring` extension must be enabled), will use (slower) `mb_strlen`, `mb_convert_case`, `mb_substr`
52 * and `mb_strpos` functions. Otherwise, the normal (ASCII-Only) functions will be used.
53 *
54 * @return $this fluent interface
55 */
56 public function withMultibyteSupport(bool $multibyteSupport = true): self
57 {
58 $this->multibyteSupport = $multibyteSupport;
59
60 return $this;
61 }
62
63 /**
64 * Sets the charset to be used if the CSS does not contain an `@charset` declaration.
65 *
66 * @param non-empty-string $defaultCharset
67 *
68 * @return $this fluent interface
69 */
70 public function withDefaultCharset(string $defaultCharset): self
71 {
72 $this->defaultCharset = $defaultCharset;
73
74 return $this;
75 }
76
77 /**
78 * Configures whether the parser should silently ignore invalid rules.
79 *
80 * @return $this fluent interface
81 */
82 public function withLenientParsing(bool $usesLenientParsing = true): self
83 {
84 $this->lenientParsing = $usesLenientParsing;
85
86 return $this;
87 }
88
89 /**
90 * Configures the parser to choke on invalid rules.
91 *
92 * @return $this fluent interface
93 */
94 public function beStrict(): self
95 {
96 return $this->withLenientParsing(false);
97 }
98
99 /**
100 * @internal
101 */
102 public function hasMultibyteSupport(): bool
103 {
104 return $this->multibyteSupport;
105 }
106
107 /**
108 * @return non-empty-string
109 *
110 * @internal
111 */
112 public function getDefaultCharset(): string
113 {
114 return $this->defaultCharset;
115 }
116
117 /**
118 * @internal
119 */
120 public function usesLenientParsing(): bool
121 {
122 return $this->lenientParsing;
123 }
124 }