1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of SebastianFeldmann\Git. |
5
|
|
|
* |
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace SebastianFeldmann\Git\Diff; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class Change. |
16
|
|
|
* |
17
|
|
|
* @author Sebastian Feldmann <[email protected]> |
18
|
|
|
* @link https://github.com/sebastianfeldmann/git |
19
|
|
|
* @since Class available since Release 1.2.0 |
20
|
|
|
*/ |
21
|
|
|
class Change |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Optional file header. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $header; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Pre range ['from' => x, 'to' => y] |
32
|
|
|
* |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $pre; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Post range ['from' => x, 'to' => y] |
39
|
|
|
* |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $post; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* List of changed lines. |
46
|
|
|
* |
47
|
|
|
* @var \SebastianFeldmann\Git\Diff\Line[] |
48
|
|
|
*/ |
49
|
|
|
private $lines = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Chan |
53
|
|
|
* ge constructor. |
54
|
|
|
* |
55
|
|
|
* @param string $ranges |
56
|
12 |
|
* @param string $header |
57
|
|
|
*/ |
58
|
12 |
|
public function __construct(string $ranges, string $header = '') |
59
|
12 |
|
{ |
60
|
11 |
|
$this->header = $header; |
61
|
|
|
$this->splitRanges($ranges); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Header getter. |
66
|
|
|
* |
67
|
3 |
|
* @return string |
68
|
|
|
*/ |
69
|
3 |
|
public function getHeader(): string |
70
|
|
|
{ |
71
|
|
|
return $this->header; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Pre range getter. |
76
|
|
|
* |
77
|
1 |
|
* @return array |
78
|
|
|
*/ |
79
|
1 |
|
public function getPre(): array |
80
|
|
|
{ |
81
|
|
|
return $this->pre; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Post range getter. |
86
|
|
|
* |
87
|
1 |
|
* @return array |
88
|
|
|
*/ |
89
|
1 |
|
public function getPost(): array |
90
|
|
|
{ |
91
|
|
|
return $this->post; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Return list of changed lines. |
96
|
|
|
* |
97
|
9 |
|
* @return \SebastianFeldmann\Git\Diff\Line[] |
98
|
|
|
*/ |
99
|
9 |
|
public function getLines(): array |
100
|
|
|
{ |
101
|
|
|
return $this->lines; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Return list of added content |
106
|
|
|
* |
107
|
|
|
* @return string[] |
108
|
9 |
|
*/ |
109
|
|
|
public function getAddedContent(): array |
110
|
9 |
|
{ |
111
|
9 |
|
$added = []; |
112
|
|
|
foreach ($this->lines as $line) { |
113
|
|
|
if ($line->getOperation() === Line::ADDED) { |
114
|
|
|
$added[] = $line->getContent(); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
return $added; |
118
|
|
|
} |
119
|
12 |
|
|
120
|
|
|
/** |
121
|
12 |
|
* Add a line to the change. |
122
|
12 |
|
* |
123
|
1 |
|
* @param \SebastianFeldmann\Git\Diff\Line $line |
124
|
|
|
* @return void |
125
|
11 |
|
*/ |
126
|
11 |
|
public function addLine(Line $line): void |
127
|
11 |
|
{ |
128
|
|
|
$this->lines[] = $line; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Parse ranges and split them into pre and post range. |
133
|
|
|
* |
134
|
|
|
* @param string $ranges |
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
|
|
private function splitRanges(string $ranges): void |
138
|
|
|
{ |
139
|
|
|
$matches = []; |
140
|
|
|
if (!preg_match('#^[\-|\+]{1}([0-9]+),([0-9]+) [\-\+]{1}([0-9]+),([0-9]+)$#', $ranges, $matches)) { |
141
|
|
|
throw new \RuntimeException('invalid ranges: ' . $ranges); |
142
|
|
|
} |
143
|
|
|
$this->pre = ['from' => (int)$matches[1], 'to' => (int)$matches[2]]; |
144
|
|
|
$this->post = ['from' => (int)$matches[3], 'to' => (int)$matches[4]]; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|