1 | <?php |
||
20 | class CommitMessage |
||
21 | { |
||
22 | /** |
||
23 | * Commit Message content |
||
24 | * |
||
25 | * This includes lines that are comments. |
||
26 | * |
||
27 | * @var string |
||
28 | */ |
||
29 | private $rawContent; |
||
30 | |||
31 | /** |
||
32 | * Content split lines |
||
33 | * |
||
34 | * This includes lines that are comments. |
||
35 | * |
||
36 | * @var string[] |
||
37 | */ |
||
38 | private $rawLines; |
||
39 | |||
40 | /** |
||
41 | * Amount of lines |
||
42 | * |
||
43 | * This includes lines that are comments |
||
44 | * |
||
45 | * @var int |
||
46 | */ |
||
47 | private $rawLineCount; |
||
48 | |||
49 | /** |
||
50 | * The comment character |
||
51 | * |
||
52 | * Null would indicate the comment character is not set for this commit. A comment character might be null if this |
||
53 | * commit came from a message stored in the repository, it would be populated if it came from a commit-msg hook, |
||
54 | * where the commit message can still contain comments. |
||
55 | * |
||
56 | * @var string|null |
||
57 | */ |
||
58 | private $commentCharacter; |
||
59 | |||
60 | /** |
||
61 | * Get the lines |
||
62 | * |
||
63 | * This excludes the lines which are comments. |
||
64 | * |
||
65 | * @var string[] |
||
66 | */ |
||
67 | private $contentLines; |
||
68 | |||
69 | /** |
||
70 | * Get the number of lines |
||
71 | * |
||
72 | * This excludes lines which are comments. |
||
73 | * |
||
74 | * @var int |
||
75 | */ |
||
76 | private $contentLineCount; |
||
77 | |||
78 | /** |
||
79 | * Commit Message content |
||
80 | * |
||
81 | * This excludes lines that are comments. |
||
82 | * |
||
83 | * @var string |
||
84 | */ |
||
85 | private $content; |
||
86 | |||
87 | /** |
||
88 | * CommitMessage constructor |
||
89 | * |
||
90 | * @param string $content |
||
91 | * @param string|null $commentCharacter |
||
92 | */ |
||
93 | 22 | public function __construct(string $content, string $commentCharacter = null) |
|
94 | { |
||
95 | 22 | $this->rawContent = $content; |
|
96 | 22 | $this->rawLines = empty($content) ? [] : preg_split("/\\r\\n|\\r|\\n/", $content); |
|
97 | 22 | $this->rawLineCount = count($this->rawLines); |
|
98 | 22 | $this->commentCharacter = $commentCharacter; |
|
99 | 22 | $this->contentLines = $this->getContentLines($this->rawLines, $commentCharacter); |
|
100 | 22 | $this->contentLineCount = count($this->contentLines); |
|
101 | 22 | $this->content = implode(PHP_EOL, $this->contentLines); |
|
102 | 22 | } |
|
103 | |||
104 | /** |
||
105 | * Is message empty. |
||
106 | * |
||
107 | * @return bool |
||
108 | */ |
||
109 | 2 | public function isEmpty() : bool |
|
110 | { |
||
111 | 2 | return empty($this->content); |
|
112 | } |
||
113 | |||
114 | |||
115 | /** |
||
116 | * Get commit message content |
||
117 | * |
||
118 | * This excludes lines that are comments. |
||
119 | * |
||
120 | * @return string |
||
121 | */ |
||
122 | 2 | public function getContent() : string |
|
126 | |||
127 | /** |
||
128 | * Get complete commit message content |
||
129 | * |
||
130 | * This includes lines that are comments. |
||
131 | * |
||
132 | * @return string |
||
133 | */ |
||
134 | 1 | public function getRawContent() : string |
|
138 | |||
139 | /** |
||
140 | * Return all lines |
||
141 | * |
||
142 | * This includes lines that are comments. |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | 2 | public function getLines() : array |
|
150 | |||
151 | /** |
||
152 | * Return line count |
||
153 | * |
||
154 | * This includes lines that are comments. |
||
155 | * |
||
156 | * @return int |
||
157 | */ |
||
158 | 3 | public function getLineCount() : int |
|
162 | |||
163 | /** |
||
164 | * Get a specific line |
||
165 | * |
||
166 | * @param int $index |
||
167 | * @return string |
||
168 | */ |
||
169 | 2 | public function getLine(int $index) : string |
|
173 | |||
174 | /** |
||
175 | * Return first line |
||
176 | * |
||
177 | * @return string |
||
178 | */ |
||
179 | 3 | public function getSubject() : string |
|
183 | |||
184 | /** |
||
185 | * Return content from line nr. 3 to the last line |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | 3 | public function getBody() : string |
|
193 | |||
194 | /** |
||
195 | * Return lines from line nr. 3 to the last line |
||
196 | * |
||
197 | * @return array |
||
198 | */ |
||
199 | 5 | public function getBodyLines() : array |
|
203 | |||
204 | /** |
||
205 | * Get the comment character |
||
206 | * |
||
207 | * Null would indicate the comment character is not set for this commit. A comment character might be null if this |
||
208 | * commit came from a message stored in the repository, it would be populated if it came from a commit-msg hook. |
||
209 | * |
||
210 | * @return null|string |
||
211 | */ |
||
212 | 1 | public function getCommentCharacter() |
|
216 | |||
217 | /** |
||
218 | * Get the lines that are not comments |
||
219 | * |
||
220 | * Null comment character indicates no comment character. |
||
221 | * |
||
222 | * @param array $rawLines |
||
223 | * @param string|null $commentCharacter |
||
224 | * @return string[] |
||
225 | */ |
||
226 | 22 | private function getContentLines(array $rawLines, string $commentCharacter = null) : array |
|
238 | |||
239 | /** |
||
240 | * Create CommitMessage from file |
||
241 | * |
||
242 | * @param string $path |
||
243 | * @param string|null $commentCharacter |
||
244 | * @return \SebastianFeldmann\Git\CommitMessage |
||
245 | */ |
||
246 | 3 | public static function createFromFile(string $path, $commentCharacter = '#') : CommitMessage |
|
254 | } |
||
255 |