Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
87 / 87
100.00% covered (success)
100.00%
14 / 14
CRAP
100.00% covered (success)
100.00%
1 / 1
Media
100.00% covered (success)
100.00%
87 / 87
100.00% covered (success)
100.00%
14 / 14
45
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
4
 setType
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setCondition
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getCondition
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
 setFeatures
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 getFeatures
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setFeature
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getFeature
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 hasFeature
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
62 / 62
100.00% covered (success)
100.00%
1 / 1
27
 __toString
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
16/**
17 * Pop CSS media class
18 *
19 * @category   Pop
20 * @package    Pop\Css
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2023 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    1.1.0
25 */
26class Media extends AbstractCss
27{
28
29    /**
30     * Media type
31     * @var string
32     */
33    protected $type = null;
34
35    /**
36     * Media condition
37     * @var string
38     */
39    protected $condition = null;
40
41    /**
42     * Media features
43     * @var array
44     */
45    protected $features = [];
46
47    /**
48     * Tab size
49     * @var int
50     */
51    protected $tabSize = 4;
52
53    /**
54     * Constructor
55     *
56     * Instantiate the CSS media object
57     *
58     * @param string $type
59     * @param array  $features
60     * @param string $condition
61     * @param int    $tabSize
62     */
63    public function __construct($type = null, array $features = null, $condition = null, $tabSize = 4)
64    {
65        if (null !== $type) {
66            $this->setType($type);
67        }
68        if (null !== $features) {
69            $this->setFeatures($features);
70        }
71        if (null !== $condition) {
72            $this->setCondition($condition);
73        }
74        $this->setTabSize($tabSize);
75    }
76
77    /**
78     * Set type
79     *
80     * @param  string $type
81     * @return self
82     */
83    public function setType($type)
84    {
85        $this->type = $type;
86        return $this;
87    }
88
89    /**
90     * Get type
91     *
92     * @return string
93     */
94    public function getType()
95    {
96        return $this->type;
97    }
98
99    /**
100     * Set condition
101     *
102     * @param  string $condition
103     * @return self
104     */
105    public function setCondition($condition = null)
106    {
107        $this->condition = $condition;
108        return $this;
109    }
110
111    /**
112     * Get condition
113     *
114     * @return string
115     */
116    public function getCondition()
117    {
118        return $this->condition;
119    }
120
121    /**
122     * Set tab size
123     *
124     * @param  int $tabSize
125     * @return self
126     */
127    public function setTabSize($tabSize)
128    {
129        $this->tabSize = (int)$tabSize;
130        return $this;
131    }
132
133    /**
134     * Get tab size
135     *
136     * @return int
137     */
138    public function getTabSize()
139    {
140        return $this->tabSize;
141    }
142
143    /**
144     * Set features
145     *
146     * @param  array $features
147     * @return self
148     */
149    public function setFeatures(array $features)
150    {
151        foreach ($features as $feature => $value) {
152            $this->setFeature($feature, $value);
153        }
154        return $this;
155    }
156
157    /**
158     * Get features
159     *
160     * @return array
161     */
162    public function getFeatures()
163    {
164        return $this->features;
165    }
166
167    /**
168     * Set feature
169     *
170     * @param  string $feature
171     * @param  string $value
172     * @return self
173     */
174    public function setFeature($feature, $value)
175    {
176        $this->features[$feature] = $value;
177        return $this;
178    }
179
180    /**
181     * Get feature
182     *
183     * @param  string $feature
184     * @return string
185     */
186    public function getFeature($feature)
187    {
188        return (isset($this->features[$feature])) ? $this->features[$feature] : null;
189    }
190
191    /**
192     * Does media query have feature
193     *
194     * @param  string $feature
195     * @return boolean
196     */
197    public function hasFeature($feature)
198    {
199        return isset($this->features[$feature]);
200    }
201
202    /**
203     * Method to render the selector CSS
204     *
205     * @return string
206     */
207    public function render()
208    {
209        $css = '';
210
211        if (!$this->minify) {
212            foreach ($this->comments as $comment) {
213                $css .= (string)$comment . PHP_EOL;
214            }
215        }
216
217        $css .= ($this->minify) ? ' @media': '@media';
218
219        if (null !== $this->condition) {
220            $css .= ' ' . $this->condition;
221        }
222        if (null !== $this->type) {
223            $css .= ' ' . $this->type;
224        }
225
226        if (!empty($this->condition) || !empty($this->type)) {
227            $css .= ' and';
228        }
229
230        if (count($this->features) > 0) {
231            $features = [];
232            foreach ($this->features as $feature => $value) {
233                $features[] = '(' . $feature . ': ' . $value .')';
234            }
235            $css .= ' ' . implode(' and ', $features);
236        }
237
238        $css .= ' {';
239        if (!$this->minify) {
240            $css .= PHP_EOL;
241        }
242
243        foreach ($this->elements as $element) {
244
245            if (isset($this->selectors[$element])) {
246                $selector = $this->selectors[$element];
247                $selector->minify($this->minify);
248                $elementCss = (string)$selector;
249
250                if (!$this->minify) {
251                    $elementCssAry = explode(PHP_EOL, $elementCss);
252                    foreach ($elementCssAry as $key => $value) {
253                        $elementCssAry[$key] = str_repeat(' ', $this->tabSize) . $value;
254                    }
255                    $elementCss = implode(PHP_EOL, $elementCssAry);
256                }
257
258                $css .= $elementCss;
259                if (!$this->minify) {
260                    $css .= PHP_EOL;
261                }
262            }
263        }
264        foreach ($this->ids as $id) {
265            if (isset($this->selectors[$id])) {
266                $selector = $this->selectors[$id];
267                $selector->minify($this->minify);
268                $idCss = (string)$selector;
269
270                if (!$this->minify) {
271                    $idCssAry = explode(PHP_EOL, $idCss);
272                    foreach ($idCssAry as $key => $value) {
273                        $idCssAry[$key] = str_repeat(' ', $this->tabSize) . $value;
274                    }
275                    $idCss = implode(PHP_EOL, $idCssAry);
276                }
277
278                $css .= $idCss;
279                if (!$this->minify) {
280                    $css .= PHP_EOL;
281                }
282            }
283        }
284        foreach ($this->classes as $class) {
285            if (isset($this->selectors[$class])) {
286                $selector = $this->selectors[$class];
287                $selector->minify($this->minify);
288                $classCss = (string)$selector;
289
290                if (!$this->minify) {
291                    $classCssAry = explode(PHP_EOL, $classCss);
292                    foreach ($classCssAry as $key => $value) {
293                        $classCssAry[$key] = str_repeat(' ', $this->tabSize) . $value;
294                    }
295                    $classCss = implode(PHP_EOL, $classCssAry);
296                }
297
298                $css .= $classCss;
299                if (!$this->minify) {
300                    $css .= PHP_EOL;
301                }
302            }
303        }
304
305        $css .= '}';
306        if (!$this->minify) {
307            $css .= PHP_EOL;
308        }
309
310        return $css;
311    }
312
313    /**
314     * To string method
315     *
316     * @return string
317     */
318    public function __toString()
319    {
320        return $this->render();
321    }
322
323}