Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
82 / 82
100.00% covered (success)
100.00%
32 / 32
CRAP
100.00% covered (success)
100.00%
1 / 1
Selector
100.00% covered (success)
100.00%
82 / 82
100.00% covered (success)
100.00%
32 / 32
62
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 setName
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
3
 getName
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isElementSelector
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 isIdSelector
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isClassSelector
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isMultipleSelector
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 hasDescendant
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTabSize
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getTabSize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setProperty
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 setProperties
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 hasProperty
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getProperties
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getProperty
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getColorProperty
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
 removeProperty
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 minify
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isMinified
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIterator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 count
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
6
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __set
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 __get
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
5
 resolveShorthandValue
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
 __isset
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
6
 __unset
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 offsetExists
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 offsetGet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 offsetSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 offsetUnset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Css;
16
17use ArrayIterator;
18use Pop\Color\Color;
19use Pop\Color\Color\ColorInterface;
20
21/**
22 * Pop CSS selector class
23 *
24 * @category   Pop
25 * @package    Pop\Css
26 * @author     Nick Sagona, III <nick@popphp.org>
27 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
28 * @license    https://www.popphp.org/license     New BSD License
29 * @version    3.0.0
30 */
31class Selector implements \ArrayAccess, \Countable, \IteratorAggregate
32{
33
34    /**
35     * Trait declaration
36     */
37    use CommentTrait;
38
39    /**
40     * Selector name
41     * @var ?string
42     */
43    protected ?string $name = null;
44
45    /**
46     * Properties
47     * @var array
48     */
49    protected array $properties = [];
50
51    /**
52     * Tab size
53     * @var int
54     */
55    protected int $tabSize = 4;
56
57    /**
58     * Is ID selector flag
59     * @var bool
60     */
61    protected bool $isId = false;
62
63    /**
64     * Is class selector flag
65     * @var bool
66     */
67    protected bool $isClass = false;
68
69    /**
70     * Minify flag
71     * @var bool
72     */
73    protected bool $minify = false;
74
75    /**
76     * Constructor
77     *
78     * Instantiate the CSS selector object
79     *
80     * @param ?string $name
81     * @param int    $tabSize
82     */
83    public function __construct(?string $name = null, int $tabSize = 4)
84    {
85        if ($name !== null) {
86            $this->setName($name);
87        }
88        $this->setTabSize($tabSize);
89    }
90
91    /**
92     * Set name
93     *
94     * @param  string $name
95     * @return Selector
96     */
97    public function setName(string $name): Selector
98    {
99        if (str_contains($name, '.')) {
100            $this->isClass = true;
101        }
102        if (str_contains($name, '#')) {
103            $this->isId = true;
104        }
105        $this->name = $name;
106        return $this;
107    }
108
109    /**
110     * Get name
111     *
112     * @return string|null
113     */
114    public function getName(): string|null
115    {
116        return $this->name;
117    }
118
119    /**
120     * Check if is element selector
121     *
122     * @return bool
123     */
124    public function isElementSelector(): bool
125    {
126        return (!($this->isId) && !($this->isClass));
127    }
128
129    /**
130     * Check if is ID selector
131     *
132     * @return bool
133     */
134    public function isIdSelector(): bool
135    {
136        return $this->isId;
137    }
138
139    /**
140     * Check if is class selector
141     *
142     * @return bool
143     */
144    public function isClassSelector(): bool
145    {
146        return $this->isClass;
147    }
148
149    /**
150     * Check if is multiple selector
151     *
152     * @return bool
153     */
154    public function isMultipleSelector(): bool
155    {
156        return (str_contains($this->name, ','));
157    }
158
159    /**
160     * Check if selector has a descendant
161     *
162     * @return bool
163     */
164    public function hasDescendant(): bool
165    {
166        return (str_contains($this->name, '>'));
167    }
168
169    /**
170     * Set tab size
171     *
172     * @param  int $tabSize
173     * @return Selector
174     */
175    public function setTabSize(int $tabSize): Selector
176    {
177        $this->tabSize = $tabSize;
178        return $this;
179    }
180
181    /**
182     * Get tab size
183     *
184     * @return int
185     */
186    public function getTabSize(): int
187    {
188        return $this->tabSize;
189    }
190
191    /**
192     * Set property
193     *
194     * @param  string                          $property
195     * @param  string|int|float|ColorInterface $value
196     * @return Selector
197     */
198    public function setProperty(string $property, string|int|float|ColorInterface $value): Selector
199    {
200        $this->properties[$property] = ($value instanceof ColorInterface) ? $value->render('CSS') : (string)$value;
201        return $this;
202    }
203
204    /**
205     * Set properties
206     *
207     * @param  array $properties
208     * @return Selector
209     */
210    public function setProperties(array $properties): Selector
211    {
212        foreach ($properties as $property => $value) {
213            $this->setProperty($property, $value);
214        }
215        return $this;
216    }
217
218    /**
219     * Check if selector has property
220     *
221     * @param  string $property
222     * @return bool
223     */
224    public function hasProperty(string $property): bool
225    {
226        return isset($this->properties[$property]);
227    }
228
229    /**
230     * Get properties
231     *
232     * @return array
233     */
234    public function getProperties(): array
235    {
236        return $this->properties;
237    }
238
239    /**
240     * Get property
241     *
242     * @param  string $property
243     * @return string|null
244     */
245    public function getProperty(string $property): string|null
246    {
247        return $this->properties[$property] ?? null;
248    }
249
250    /**
251     * Get property as a color object
252     *
253     * @param  string $property
254     * @return ColorInterface|null
255     */
256    public function getColorProperty(string $property): ColorInterface|null
257    {
258        if (!isset($this->properties[$property])) {
259            return null;
260        }
261
262        try {
263            return Color::parse($this->properties[$property]);
264        } catch (\Throwable) {
265            return null;
266        }
267    }
268
269    /**
270     * Remove property
271     *
272     * @param  string $property
273     * @return Selector
274     */
275    public function removeProperty(string $property): Selector
276    {
277        if (isset($this->properties[$property])) {
278            unset($this->properties[$property]);
279        }
280        return $this;
281    }
282
283    /**
284     * Set minify flag
285     *
286     * @param  bool $minify
287     * @return Selector
288     */
289    public function minify(bool $minify = true): Selector
290    {
291        $this->minify = $minify;
292        return $this;
293    }
294
295    /**
296     * Check if minify flag is set
297     *
298     * @return bool
299     */
300    public function isMinified(): bool
301    {
302        return $this->minify;
303    }
304
305    /**
306     * Method to iterate over the properties
307     *
308     * @return ArrayIterator
309     */
310    public function getIterator(): ArrayIterator
311    {
312        return new ArrayIterator($this->properties);
313    }
314
315    /**
316     * Method to get the count of properties
317     *
318     * @return int
319     */
320    public function count(): int
321    {
322        return count($this->properties);
323    }
324
325    /**
326     * Method to render the selector CSS
327     *
328     * @return string
329     */
330    public function render(): string
331    {
332        $css = '';
333
334        if (!$this->minify) {
335            foreach ($this->comments as $comment) {
336                $css .= (string)$comment . PHP_EOL;
337            }
338        }
339
340        if (!$this->minify) {
341            $css .= $this->name . ' {' . PHP_EOL;
342
343            foreach ($this->properties as $property => $value) {
344                $css .= str_repeat(' ', $this->tabSize) . $property . ': ' . $value . ';' . PHP_EOL;
345            }
346
347            $css .= '}' . PHP_EOL;
348        } else {
349            $css .= $this->name . '{';
350            foreach ($this->properties as $property => $value) {
351                $css .= $property . ':' . $value . ';';
352            }
353
354            $css .= '}';
355        }
356
357        return $css;
358    }
359
360    /**
361     * To string method
362     *
363     * @return string
364     */
365    public function __toString(): string
366    {
367        return $this->render();
368    }
369
370    /**
371     * Magic method to set the property to the value of $this->properties[$name]
372     *
373     * @param  string $name
374     * @param  mixed  $value
375     * @return void
376     */
377    public function __set(string $name, mixed $value): void
378    {
379        $this->properties[$name] = ($value instanceof ColorInterface) ? $value->render('CSS') : (string)$value;
380    }
381
382    /**
383     * Magic method to return the value of $this->properties[$name]
384     *
385     * @param  string $name
386     * @return mixed
387     */
388    public function __get(string $name): mixed
389    {
390        foreach (['margin', 'padding'] as $shorthand) {
391            if (str_contains($name, $shorthand . '-') && !isset($this->properties[$name]) && isset($this->properties[$shorthand])) {
392                return $this->resolveShorthandValue($shorthand, substr($name, strpos($name, '-') + 1));
393            }
394        }
395
396        return $this->properties[$name] ?? null;
397    }
398
399    /**
400     * Resolve a longhand value (e.g. margin-top) from a margin/padding shorthand property,
401     * per the 1/2/3/4-value CSS shorthand rules
402     *
403     * @param  string $shorthand
404     * @param  string $position
405     * @return string|null
406     */
407    protected function resolveShorthandValue(string $shorthand, string $position): string|null
408    {
409        $values = explode(' ', $this->properties[$shorthand]);
410
411        $positionValues = match (count($values)) {
412            4       => ['top' => $values[0], 'right' => $values[1], 'bottom' => $values[2], 'left' => $values[3]],
413            3       => ['top' => $values[0], 'right' => $values[1], 'bottom' => $values[2], 'left' => $values[1]],
414            2       => ['top' => $values[0], 'right' => $values[1], 'bottom' => $values[0], 'left' => $values[1]],
415            1       => ['top' => $values[0], 'right' => $values[0], 'bottom' => $values[0], 'left' => $values[0]],
416            default => ['top' => null, 'right' => null, 'bottom' => null, 'left' => null],
417        };
418
419        return $positionValues[$position] ?? null;
420    }
421
422    /**
423     * Magic method to return the isset value of $this->properties[$name]
424     *
425     * @param  string $name
426     * @return bool
427     */
428    public function __isset(string $name): bool
429    {
430        if (isset($this->properties[$name])) {
431            return true;
432        }
433
434        foreach (['margin', 'padding'] as $shorthand) {
435            if (str_starts_with($name, $shorthand . '-') && isset($this->properties[$shorthand])) {
436                $position = substr($name, strlen($shorthand) + 1);
437                if (in_array($position, ['top', 'right', 'bottom', 'left'], true)) {
438                    return true;
439                }
440            }
441        }
442
443        return false;
444    }
445
446    /**
447     * Magic method to unset $this->properties[$name]
448     *
449     * @param  string $name
450     * @return void
451     */
452    public function __unset(string $name): void
453    {
454        if (isset($this->properties[$name])) {
455            unset($this->properties[$name]);
456        }
457    }
458
459    /**
460     * ArrayAccess offsetExists
461     *
462     * @param  mixed $offset
463     * @return bool
464     */
465    public function offsetExists(mixed $offset): bool
466    {
467        return $this->__isset($offset);
468    }
469
470    /**
471     * ArrayAccess offsetGet
472     *
473     * @param  mixed $offset
474     * @return mixed
475     */
476    public function offsetGet(mixed $offset): mixed
477    {
478        return $this->__get($offset);
479    }
480
481    /**
482     * ArrayAccess offsetSet
483     *
484     * @param  mixed $offset
485     * @param  mixed $value
486     * @return void
487     */
488    public function offsetSet(mixed $offset, mixed $value): void
489    {
490        $this->__set($offset, $value);
491    }
492
493    /**
494     * ArrayAccess offsetUnset
495     *
496     * @param  mixed $offset
497     * @return void
498     */
499    public function offsetUnset(mixed $offset): void
500    {
501        $this->__unset($offset);
502    }
503
504}