Debian Codegraph @

vendor/ipl/web/src/Less/LessRuleset.php

raw
1 <?php
2
3 namespace ipl\Web\Less;
4
5 use ArrayObject;
6 use Less_Parser;
7 use OutOfBoundsException;
8
9 /**
10 * @extends ArrayObject<string, string>
11 */
12 class LessRuleset extends ArrayObject
13 {
14 /** @var ?string */
15 protected $selector;
16
17 /** @var array<LessRuleset> */
18 protected $children = [];
19
20 /**
21 * Create a new LessRuleset
22 *
23 * @param string $selector Selector to use
24 * @param array<string, string> $properties CSS properties
25 *
26 * @return self
27 */
28 public static function create(string $selector, array $properties): self
29 {
30 $ruleset = new static();
31 $ruleset->selector = $selector;
32 $ruleset->exchangeArray($properties);
33
34 return $ruleset;
35 }
36
37 /**
38 * Get the selector
39 *
40 * @return ?string
41 */
42 public function getSelector(): ?string
43 {
44 return $this->selector;
45 }
46
47 /**
48 * Set the selector
49 *
50 * @param string $selector
51 *
52 * @return $this
53 */
54 public function setSelector(string $selector): self
55 {
56 $this->selector = $selector;
57
58 return $this;
59 }
60
61 /**
62 * Get a property value
63 *
64 * @param string $property Name of the property
65 *
66 * @return string
67 *
68 * @throws OutOfBoundsException If the property does not exist
69 */
70 public function getProperty(string $property): string
71 {
72 return (string) $this[$property];
73 }
74
75 /**
76 * Get a property value
77 *
78 * @param string $key Name of the property
79 *
80 * @return string
81 *
82 * @throws OutOfBoundsException If the property does not exist
83 */
84 public function offsetGet(mixed $key): mixed
85 {
86 if (! $this->offsetExists($key)) {
87 throw new OutOfBoundsException("Property '$key' not found");
88 }
89
90 return parent::offsetGet($key);
91 }
92
93 /**
94 * Set a property
95 *
96 * @param string $property Name to use
97 * @param string $value Value to set
98 *
99 * @return $this
100 */
101 public function setProperty(string $property, string $value): self
102 {
103 $this[$property] = $value;
104
105 return $this;
106 }
107
108 /**
109 * Get all properties
110 *
111 * @return array<string, string>
112 */
113 public function getProperties(): array
114 {
115 return $this->getArrayCopy();
116 }
117
118 /**
119 * Set properties
120 *
121 * @param array<string, string> $properties
122 *
123 * @return $this
124 */
125 public function setProperties(array $properties): self
126 {
127 $this->exchangeArray($properties);
128
129 return $this;
130 }
131
132 /**
133 * Create and add a ruleset
134 *
135 * @param string $selector Selector to use
136 * @param array<string, string> $properties CSS properties
137 *
138 * @return $this
139 */
140 public function add(string $selector, array $properties): self
141 {
142 $this->children[] = static::create($selector, $properties);
143
144 return $this;
145 }
146
147 /**
148 * Add a ruleset
149 *
150 * @param LessRuleset $ruleset
151 *
152 * @return $this
153 */
154 public function addRuleset(LessRuleset $ruleset): self
155 {
156 $this->children[] = $ruleset;
157
158 return $this;
159 }
160
161 /**
162 * Compile the ruleset to CSS
163 *
164 * @return string
165 */
166 public function renderCss(): string
167 {
168 $parser = new Less_Parser(['compress' => true]);
169 $parser->parse($this->renderLess());
170
171 return $parser->getCss();
172 }
173
174 /**
175 * Render the ruleset to LESS
176 *
177 * @return string
178 */
179 protected function renderLess(): string
180 {
181 $less = [];
182
183 foreach ($this as $property => $value) {
184 if ($value === null || trim($value) === '') {
185 continue;
186 }
187
188 $less[] = "$property: $value;";
189 }
190
191 foreach ($this->children as $ruleset) {
192 $rendered = $ruleset->renderLess();
193 if ($rendered !== '') {
194 $less[] = $rendered;
195 }
196 }
197
198 if ($this->selector !== null && ! empty($less)) {
199 array_unshift($less, "$this->selector {");
200 $less[] = '}';
201 }
202
203 return implode("\n", $less);
204 }
205 }