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
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 comment 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 Comment
27{
28
29    /**
30     * Comment string
31     * @var ?string
32     */
33    protected ?string $comment = null;
34
35    /**
36     * Comment wrap
37     * @var int
38     */
39    protected int $wrap = 80;
40
41    /**
42     * Trailing new line
43     * @var bool
44     */
45    protected bool $trailingNewLine = true;
46
47    /**
48     * Constructor
49     *
50     * Instantiate the CSS comment object
51     *
52     * @param string $comment
53     * @param int    $wrap
54     * @param bool   $trailingNewLine
55     */
56    public function __construct(string $comment, int $wrap = 80, bool $trailingNewLine = false)
57    {
58        $this->setComment($comment);
59        $this->setWrap($wrap);
60        $this->setTrailingNewLine($trailingNewLine);
61    }
62
63    /**
64     * Set comment
65     *
66     * @param  string $comment
67     * @return Comment
68     */
69    public function setComment(string $comment): Comment
70    {
71        $this->comment = $comment;
72        return $this;
73    }
74
75    /**
76     * Get comment
77     *
78     * @return string|null
79     */
80    public function getComment(): string|null
81    {
82        return $this->comment;
83    }
84
85    /**
86     * Set wrap
87     *
88     * @param  int $wrap
89     * @return Comment
90     */
91    public function setWrap(int $wrap): Comment
92    {
93        $this->wrap = $wrap;
94        return $this;
95    }
96
97    /**
98     * Get wrap
99     *
100     * @return int
101     */
102    public function getWrap(): int
103    {
104        return $this->wrap;
105    }
106
107    /**
108     * Set trailing new line
109     *
110     * @param  bool $trailingNewLine
111     * @return Comment
112     */
113    public function setTrailingNewLine(bool $trailingNewLine): Comment
114    {
115        $this->trailingNewLine = $trailingNewLine;
116        return $this;
117    }
118
119    /**
120     * Has trailing new line
121     *
122     * @return bool
123     */
124    public function hasTrailingNewLine(): bool
125    {
126        return $this->trailingNewLine;
127    }
128
129    /**
130     * Method to render the selector CSS
131     *
132     * @return string
133     */
134    public function render(): string
135    {
136        $comment = '/*';
137
138        if ($this->wrap == 0) {
139            $comment .= ' ' . $this->comment;
140        } else {
141            $comment .= PHP_EOL;
142            if (str_contains($this->comment, PHP_EOL)) {
143                $commentAry = explode(PHP_EOL, $this->comment);
144            } else {
145                $commentAry = (strlen($this->comment) > $this->wrap) ?
146                    explode(PHP_EOL, wordwrap($this->comment, $this->wrap, PHP_EOL)) : [$this->comment];
147            }
148
149            foreach ($commentAry as $line) {
150                $comment .= ' * ' . $line . PHP_EOL;
151            }
152        }
153
154        $comment .= ' */';
155
156        if ($this->trailingNewLine) {
157            $comment .= PHP_EOL;
158        }
159
160        return $comment;
161    }
162
163    /**
164     * To string method
165     *
166     * @return string
167     */
168    public function __toString(): string
169    {
170        return $this->render();
171    }
172
173}