Debian Codegraph @

vendor/mck89/peast/lib/Peast/Syntax/Node/Comment.php

raw
1 <?php
2 /**
3 * This file is part of the Peast package
4 *
5 * (c) Marco Marchiò <[email protected]>
6 *
7 * For the full copyright and license information refer to the LICENSE file
8 * distributed with this source code
9 */
10 namespace Peast\Syntax\Node;
11
12 /**
13 * A node that represents a comment.
14 *
15 * @author Marco Marchiò <[email protected]>
16 */
17 class Comment extends Node
18 {
19 //Comment kind constants
20 /**
21 * Inline comment
22 */
23 const KIND_INLINE = "inline";
24
25 /**
26 * Multiline comment
27 */
28 const KIND_MULTILINE = "multiline";
29
30 /**
31 * Html open comment
32 */
33 const KIND_HTML_OPEN = "html-open";
34
35 /**
36 * Html close comment
37 */
38 const KIND_HTML_CLOSE = "html-close";
39
40 /**
41 * Hashbang comment
42 */
43 const KIND_HASHBANG = "hashbang";
44
45 /**
46 * Map of node properties
47 *
48 * @var array
49 */
50 protected $propertiesMap = array(
51 "kind" => false,
52 "text" => false
53 );
54
55 /**
56 * The comment kind
57 *
58 * @var string
59 */
60 protected $kind;
61
62 /**
63 * The comment text
64 *
65 * @var string
66 */
67 protected $text;
68
69 /**
70 * Returns the comment kind
71 *
72 * @return string
73 */
74 public function getKind()
75 {
76 return $this->kind;
77 }
78
79 /**
80 * Sets the comment kind
81 *
82 * @param string $kind Comment kind
83 *
84 * @return $this
85 */
86 public function setKind($kind)
87 {
88 $this->kind = $kind;
89 return $this;
90 }
91
92 /**
93 * Returns the comment text
94 *
95 * @return string
96 */
97 public function getText()
98 {
99 return $this->text;
100 }
101
102 /**
103 * Sets the comment text
104 *
105 * @param string $text Comment text
106 *
107 * @return $this
108 */
109 public function setText($text)
110 {
111 $this->text = $text;
112 return $this;
113 }
114
115 /**
116 * Returns the comment raw text
117 *
118 * @return string
119 */
120 public function getRawText()
121 {
122 $text = $this->getText();
123 $kind = $this->getKind();
124
125 if ($kind === self::KIND_MULTILINE) {
126 $sanitize = "*/";
127 } else {
128 $sanitize = array("\n", "\r");
129 }
130 $text = str_replace($sanitize, "", $text);
131
132 if ($kind === self::KIND_INLINE) {
133 return "//" . $text;
134 } elseif ($kind === self::KIND_HASHBANG) {
135 return "#!" . $text;
136 } elseif ($kind === self::KIND_HTML_OPEN) {
137 return "<!--" . $text;
138 } elseif ($kind === self::KIND_HTML_CLOSE) {
139 return "-->" . $text;
140 } else {
141 return "/*" . $text . "*/";
142 }
143 }
144
145 /**
146 * Sets the comment raw text
147 *
148 * @param string $rawText Comment raw text
149 *
150 * @return $this
151 */
152 public function setRawText($rawText)
153 {
154 $start = substr($rawText, 0, 2);
155 if ($start === "//") {
156 $kind = self::KIND_INLINE;
157 $text = substr($rawText, 2);
158 } elseif ($start === "/*" && substr($rawText, -2) === "*/") {
159 $kind = self::KIND_MULTILINE;
160 $text = substr($rawText, 2, -2);
161 } elseif ($start === "#!") {
162 $kind = self::KIND_HASHBANG;
163 $text = substr($rawText, 2);
164 } elseif ($start === "<!" && substr($rawText, 2, 2) === "--") {
165 $kind = self::KIND_HTML_OPEN;
166 $text = substr($rawText, 4);
167 } elseif ($start === "--" && substr($rawText, 2, 1) === ">") {
168 $kind = self::KIND_HTML_CLOSE;
169 $text = substr($rawText, 3);
170 } else {
171 throw new \Exception("Invalid comment");
172 }
173 return $this->setKind($kind)->setText($text);
174 }
175
176 /**
177 * Sets leading comments array
178 *
179 * @param Comment[] $comments Comments array
180 *
181 * @return $this
182 */
183 public function setLeadingComments($comments)
184 {
185 //Comments cannot be attached to other comments
186 return $this;
187 }
188
189 /**
190 * Sets trailing comments array
191 *
192 * @param Comment[] $comments Comments array
193 *
194 * @return $this
195 */
196 public function setTrailingComments($comments)
197 {
198 //Comments cannot be attached to other comments
199 return $this;
200 }
201
202 /**
203 * Returns a serializable version of the node
204 *
205 * @return array
206 */
207 #[\ReturnTypeWillChange]
208 public function jsonSerialize()
209 {
210 $ret = parent::jsonSerialize();
211 unset($ret["leadingComments"]);
212 unset($ret["trailingComments"]);
213 return $ret;
214 }
215 }