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