1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of SebastianFeldmann\Git. |
4
|
|
|
* |
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace SebastianFeldmann\Git; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class CommitMessage |
14
|
|
|
* |
15
|
|
|
* @package SebastianFeldmann\Git |
16
|
|
|
* @author Sebastian Feldmann <[email protected]> |
17
|
|
|
* @link https://github.com/sebastianfeldmann/git |
18
|
|
|
* @since Class available since Release 0.9.0 |
19
|
|
|
*/ |
20
|
|
|
class CommitMessage |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Commit Message content |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $content; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Content split lines |
31
|
|
|
* |
32
|
|
|
* @var string[] |
33
|
|
|
*/ |
34
|
|
|
private $lines; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Amount of lines |
38
|
|
|
* |
39
|
|
|
* @var int |
40
|
|
|
*/ |
41
|
|
|
private $lineCount; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* CommitMessage constructor. |
45
|
|
|
* |
46
|
|
|
* @param string $content |
47
|
|
|
*/ |
48
|
11 |
|
public function __construct(string $content) |
49
|
|
|
{ |
50
|
11 |
|
$this->content = $content; |
51
|
11 |
|
$this->lines = empty($content) ? [] : preg_split("/\\r\\n|\\r|\\n/", $content); |
52
|
11 |
|
$this->lineCount = count($this->lines); |
53
|
11 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Is message empty. |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
1 |
|
public function isEmpty() : bool |
61
|
|
|
{ |
62
|
1 |
|
return empty($this->content); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get complete commit message content. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
1 |
|
public function getContent() : string |
71
|
|
|
{ |
72
|
1 |
|
return $this->content; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Return all lines. |
77
|
|
|
* |
78
|
|
|
* @return array |
79
|
|
|
*/ |
80
|
1 |
|
public function getLines() : array |
81
|
|
|
{ |
82
|
1 |
|
return $this->lines; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return line count. |
87
|
|
|
* |
88
|
|
|
* @return int |
89
|
|
|
*/ |
90
|
2 |
|
public function getLineCount() : int |
91
|
|
|
{ |
92
|
2 |
|
return $this->lineCount; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get a specific line. |
97
|
|
|
* |
98
|
|
|
* @param int $index |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
1 |
|
public function getLine(int $index) : string |
102
|
|
|
{ |
103
|
1 |
|
return isset($this->lines[$index]) ? $this->lines[$index] : ''; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return first line. |
108
|
|
|
* |
109
|
|
|
* @return string |
110
|
|
|
*/ |
111
|
2 |
|
public function getSubject() : string |
112
|
|
|
{ |
113
|
2 |
|
return $this->lines[0]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Return content from line nr. 3 to the last line. |
118
|
|
|
* |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
1 |
|
public function getBody() : string |
122
|
|
|
{ |
123
|
1 |
|
return implode(PHP_EOL, $this->getBodyLines()); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Return lines from line nr. 3 to the last line. |
128
|
|
|
* |
129
|
|
|
* @return array |
130
|
|
|
*/ |
131
|
2 |
|
public function getBodyLines() : array |
132
|
|
|
{ |
133
|
2 |
|
return $this->lineCount < 3 ? [] : array_slice($this->lines, 2); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Create CommitMessage from file. |
138
|
|
|
* |
139
|
|
|
* @param string $path |
140
|
|
|
* @return \SebastianFeldmann\Git\CommitMessage |
141
|
|
|
*/ |
142
|
2 |
|
public static function createFromFile(string $path) : CommitMessage |
143
|
|
|
{ |
144
|
2 |
|
if (!file_exists($path)) { |
145
|
1 |
|
throw new \RuntimeException('Commit message file not found'); |
146
|
|
|
} |
147
|
1 |
|
return new CommitMessage(file_get_contents($path)); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|