Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
Comment
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
9 / 9
14
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 setComment
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getComment
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setWrap
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getWrap
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setTrailingNewLine
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 hasTrailingNewLine
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
6
 __toString
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
17/**
18 * Pop CSS comment class
19 *
20 * @category   Pop
21 * @package    Pop\Css
22 * @author     Nick Sagona, III <nick@popphp.org>
23 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    3.0.0
26 */
27class Comment
28{
29
30    /**
31     * Comment string
32     * @var ?string
33     */
34    protected ?string $comment = null;
35
36    /**
37     * Comment wrap
38     * @var int
39     */
40    protected int $wrap = 80;
41
42    /**
43     * Trailing new line
44     * @var bool
45     */
46    protected bool $trailingNewLine = true;
47
48    /**
49     * Constructor
50     *
51     * Instantiate the CSS comment object
52     *
53     * @param string $comment
54     * @param int    $wrap
55     * @param bool   $trailingNewLine
56     */
57    public function __construct(string $comment, int $wrap = 80, bool $trailingNewLine = false)
58    {
59        $this->setComment($comment);
60        $this->setWrap($wrap);
61        $this->setTrailingNewLine($trailingNewLine);
62    }
63
64    /**
65     * Set comment
66     *
67     * @param  string $comment
68     * @return Comment
69     */
70    public function setComment(string $comment): Comment
71    {
72        $this->comment = $comment;
73        return $this;
74    }
75
76    /**
77     * Get comment
78     *
79     * @return string|null
80     */
81    public function getComment(): string|null
82    {
83        return $this->comment;
84    }
85
86    /**
87     * Set wrap
88     *
89     * @param  int $wrap
90     * @return Comment
91     */
92    public function setWrap(int $wrap): Comment
93    {
94        $this->wrap = $wrap;
95        return $this;
96    }
97
98    /**
99     * Get wrap
100     *
101     * @return int
102     */
103    public function getWrap(): int
104    {
105        return $this->wrap;
106    }
107
108    /**
109     * Set trailing new line
110     *
111     * @param  bool $trailingNewLine
112     * @return Comment
113     */
114    public function setTrailingNewLine(bool $trailingNewLine): Comment
115    {
116        $this->trailingNewLine = $trailingNewLine;
117        return $this;
118    }
119
120    /**
121     * Has trailing new line
122     *
123     * @return bool
124     */
125    public function hasTrailingNewLine(): bool
126    {
127        return $this->trailingNewLine;
128    }
129
130    /**
131     * Method to render the selector CSS
132     *
133     * @return string
134     */
135    public function render(): string
136    {
137        $comment = '/*';
138
139        if ($this->wrap == 0) {
140            $comment .= ' ' . $this->comment;
141        } else {
142            $comment .= PHP_EOL;
143            if (str_contains($this->comment, PHP_EOL)) {
144                $commentAry = explode(PHP_EOL, $this->comment);
145            } else {
146                $commentAry = (strlen($this->comment) > $this->wrap) ?
147                    explode(PHP_EOL, wordwrap($this->comment, $this->wrap, PHP_EOL)) : [$this->comment];
148            }
149
150            foreach ($commentAry as $line) {
151                $comment .= ' * ' . $line . PHP_EOL;
152            }
153        }
154
155        $comment .= ' */';
156
157        if ($this->trailingNewLine) {
158            $comment .= PHP_EOL;
159        }
160
161        return $comment;
162    }
163
164    /**
165     * To string method
166     *
167     * @return string
168     */
169    public function __toString(): string
170    {
171        return $this->render();
172    }
173
174}