Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
13 / 13
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractCss
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
13 / 13
15
100.00% covered (success)
100.00%
1 / 1
 addSelector
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addSelectors
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 hasSelector
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSelector
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 removeSelector
100.00% covered (success)
100.00%
2 / 2
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
 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%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
 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;
18
19/**
20 * Abstract CSS class
21 *
22 * @category   Pop
23 * @package    Pop\Css
24 * @author     Nick Sagona, III <nick@popphp.org>
25 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    3.0.0
28 */
29abstract class AbstractCss implements \ArrayAccess, \Countable, \IteratorAggregate
30{
31
32    /**
33     * Trait declaration
34     */
35    use CommentTrait;
36
37    /**
38     * Selectors
39     * @var array
40     */
41    protected array $selectors = [];
42
43    /**
44     * Minify flag
45     * @var bool
46     */
47    protected bool $minify = false;
48
49    /**
50     * Add CSS selector
51     *
52     * @param  Selector $selector
53     * @return AbstractCss
54     */
55    public function addSelector(Selector $selector): AbstractCss
56    {
57        $this->selectors[$selector->getName()] = $selector;
58        return $this;
59    }
60
61    /**
62     * Add CSS selectors
63     *
64     * @param  array $selectors
65     * @return AbstractCss
66     */
67    public function addSelectors(array $selectors): AbstractCss
68    {
69        foreach ($selectors as $selector) {
70            $this->addSelector($selector);
71        }
72        return $this;
73    }
74
75    /**
76     * Check if the object has CSS selector
77     *
78     * @param  string $selector
79     * @return bool
80     */
81    public function hasSelector(string $selector): bool
82    {
83        return (isset($this->selectors[$selector]));
84    }
85
86    /**
87     * Get CSS selector
88     *
89     * @param  string $selector
90     * @return Selector|null
91     */
92    public function getSelector(string $selector): Selector|null
93    {
94        return $this->selectors[$selector] ?? null;
95    }
96
97    /**
98     * Remove CSS selector
99     *
100     * @param  string $selector
101     * @return AbstractCss
102     */
103    public function removeSelector(string $selector): AbstractCss
104    {
105        unset($this->selectors[$selector]);
106        return $this;
107    }
108
109    /**
110     * Set minify flag
111     *
112     * @param  bool $minify
113     * @return AbstractCss
114     */
115    public function minify(bool $minify = true): AbstractCss
116    {
117        $this->minify = $minify;
118        return $this;
119    }
120
121    /**
122     * Check if minify flag is set
123     *
124     * @return bool
125     */
126    public function isMinified(): bool
127    {
128        return $this->minify;
129    }
130
131   /**
132     * Method to iterate over the properties
133     *
134     * @return ArrayIterator
135     */
136    public function getIterator(): ArrayIterator
137    {
138        return new ArrayIterator($this->selectors);
139    }
140
141    /**
142     * Method to get the count of properties
143     *
144     * @return int
145     */
146    public function count(): int
147    {
148        return count($this->selectors);
149    }
150
151    /**
152     * ArrayAccess offsetExists
153     *
154     * @param  mixed $offset
155     * @return bool
156     */
157    public function offsetExists(mixed $offset): bool
158    {
159        return $this->hasSelector($offset);
160    }
161
162    /**
163     * ArrayAccess offsetGet
164     *
165     * @param  mixed $offset
166     * @return mixed
167     */
168    public function offsetGet(mixed $offset): mixed
169    {
170        return $this->getSelector($offset);
171    }
172
173    /**
174     * ArrayAccess offsetSet
175     *
176     * @param  mixed $offset
177     * @param  mixed $value
178     * @throws Exception
179     * @return void
180     */
181    public function offsetSet(mixed $offset, mixed $value): void
182    {
183        if (!($value instanceof Selector)) {
184            throw new Exception('Error: The value passed must be an instance of Pop\Css\Selector');
185        }
186        $value->setName($offset);
187        $this->addSelector($value);
188    }
189
190    /**
191     * ArrayAccess offsetUnset
192     *
193     * @param  mixed $offset
194     * @return void
195     */
196    public function offsetUnset(mixed $offset): void
197    {
198        $this->removeSelector($offset);
199    }
200
201}