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