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