Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
89 / 89
100.00% covered (success)
100.00%
14 / 14
CRAP
100.00% covered (success)
100.00%
1 / 1
Media
100.00% covered (success)
100.00%
89 / 89
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
1
 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%
64 / 64
100.00% covered (success)
100.00%
1 / 1
28
 __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-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
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-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    2.0.0
25 */
26class Media extends AbstractCss
27{
28
29    /**
30     * Media type
31     * @var ?string
32     */
33    protected ?string $type = null;
34
35    /**
36     * Media condition
37     * @var ?string
38     */
39    protected ?string $condition = null;
40
41    /**
42     * Media features
43     * @var array
44     */
45    protected array $features = [];
46
47    /**
48     * Tab size
49     * @var int
50     */
51    protected int $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(?string $type = null, ?array $features = null, ?string $condition = null, $tabSize = 4)
64    {
65        if ($type !== null) {
66            $this->setType($type);
67        }
68        if ($features !== null) {
69            $this->setFeatures($features);
70        }
71        if ($condition !== null) {
72            $this->setCondition($condition);
73        }
74        $this->setTabSize($tabSize);
75    }
76
77    /**
78     * Set type
79     *
80     * @param  string $type
81     * @return Media
82     */
83    public function setType(string $type): Media
84    {
85        $this->type = $type;
86        return $this;
87    }
88
89    /**
90     * Get type
91     *
92     * @return string|null
93     */
94    public function getType(): string|null
95    {
96        return $this->type;
97    }
98
99    /**
100     * Set condition
101     *
102     * @param  ?string $condition
103     * @return Media
104     */
105    public function setCondition(?string $condition = null): Media
106    {
107        $this->condition = $condition;
108        return $this;
109    }
110
111    /**
112     * Get condition
113     *
114     * @return string|null
115     */
116    public function getCondition(): string|null
117    {
118        return $this->condition;
119    }
120
121    /**
122     * Set tab size
123     *
124     * @param  int $tabSize
125     * @return Media
126     */
127    public function setTabSize(int $tabSize): Media
128    {
129        $this->tabSize = $tabSize;
130        return $this;
131    }
132
133    /**
134     * Get tab size
135     *
136     * @return int
137     */
138    public function getTabSize(): int
139    {
140        return $this->tabSize;
141    }
142
143    /**
144     * Set features
145     *
146     * @param  array $features
147     * @return Media
148     */
149    public function setFeatures(array $features): Media
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(): array
163    {
164        return $this->features;
165    }
166
167    /**
168     * Set feature
169     *
170     * @param  string $feature
171     * @param  string $value
172     * @return Media
173     */
174    public function setFeature(string $feature, string $value): Media
175    {
176        $this->features[$feature] = $value;
177        return $this;
178    }
179
180    /**
181     * Get feature
182     *
183     * @param  string $feature
184     * @return string|null
185     */
186    public function getFeature(string $feature): string|null
187    {
188        return $this->features[$feature] ?? null;
189    }
190
191    /**
192     * Does media query have feature
193     *
194     * @param  string $feature
195     * @return bool
196     */
197    public function hasFeature(string $feature): bool
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(): string
208    {
209        $css = '';
210
211        if (!$this->minify) {
212            foreach ($this->comments as $comment) {
213                $css .= $comment . PHP_EOL;
214            }
215        }
216
217        $css .= ($this->minify) ? ' @media': '@media';
218
219        if ($this->condition !== null) {
220            $css .= ' ' . $this->condition;
221        }
222        if ($this->type !== null) {
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        // Clean up end new lines
306        if (str_ends_with($css, PHP_EOL . str_repeat(' ', $this->tabSize) . PHP_EOL)) {
307            $css = substr($css, 0, 0 - ($this->tabSize + 1));
308        }
309
310        $css .= '}';
311
312        if (!$this->minify) {
313            $css .= PHP_EOL;
314        }
315
316        return $css;
317    }
318
319    /**
320     * To string method
321     *
322     * @return string
323     */
324    public function __toString(): string
325    {
326        return $this->render();
327    }
328
329}