|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the php-merge package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Fabian Bircher <[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
|
|
|
|
|
11
|
|
|
namespace PhpMerge\internal; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Class Line |
|
15
|
|
|
* |
|
16
|
|
|
* @package PhpMerge |
|
17
|
|
|
* @author Fabian Bircher <[email protected]> |
|
18
|
|
|
* @copyright Fabian Bircher <[email protected]> |
|
19
|
|
|
* @license https://opensource.org/licenses/MIT |
|
20
|
|
|
* @version Release: @package_version@ |
|
21
|
|
|
* @link http://github.com/bircher/php-merge |
|
22
|
|
|
* @internal This class is not part of the public api. |
|
23
|
|
|
*/ |
|
24
|
|
|
final class Line |
|
25
|
|
|
{ |
|
26
|
|
|
public const ADDED = 1; |
|
27
|
|
|
public const REMOVED = 2; |
|
28
|
|
|
public const UNCHANGED = 3; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var int |
|
32
|
|
|
*/ |
|
33
|
|
|
private $type; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
private $content; |
|
39
|
|
|
|
|
40
|
56 |
|
/** |
|
41
|
|
|
* @var int |
|
42
|
56 |
|
*/ |
|
43
|
|
|
protected $index; |
|
44
|
56 |
|
|
|
45
|
56 |
|
/** |
|
46
|
|
|
* Line constructor. |
|
47
|
|
|
* @param int $type |
|
48
|
|
|
* @param string $content |
|
49
|
|
|
* @param int $index |
|
50
|
52 |
|
*/ |
|
51
|
|
|
public function __construct($type = self::UNCHANGED, $content = '', $index = null) |
|
52
|
52 |
|
{ |
|
53
|
|
|
$this->type = $type; |
|
54
|
|
|
$this->content = $content; |
|
55
|
|
|
$this->index = $index; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
52 |
|
* @return int |
|
60
|
|
|
*/ |
|
61
|
52 |
|
public function getIndex() |
|
62
|
52 |
|
{ |
|
63
|
52 |
|
return $this->index; |
|
64
|
52 |
|
} |
|
65
|
52 |
|
|
|
66
|
52 |
|
/** |
|
67
|
52 |
|
* @param array $diff |
|
68
|
52 |
|
* @return Line[] |
|
69
|
|
|
*/ |
|
70
|
52 |
|
public static function createArray($diff) |
|
71
|
52 |
|
{ |
|
72
|
52 |
|
$index = -1; |
|
73
|
|
|
$lines = []; |
|
74
|
52 |
|
foreach ($diff as $key => $value) { |
|
75
|
52 |
|
switch ($value[1]) { |
|
76
|
52 |
|
case 0: |
|
77
|
52 |
|
$index++; |
|
78
|
|
|
$line = new Line(Line::UNCHANGED, $value[0], $index); |
|
79
|
|
|
break; |
|
80
|
4 |
|
|
|
81
|
|
|
case 1: |
|
82
|
52 |
|
$line = new Line(Line::ADDED, $value[0], $index); |
|
83
|
|
|
break; |
|
84
|
52 |
|
|
|
85
|
|
|
case 2: |
|
86
|
|
|
$index++; |
|
87
|
|
|
$line = new Line(Line::REMOVED, $value[0], $index); |
|
88
|
|
|
break; |
|
89
|
|
|
|
|
90
|
|
|
default: |
|
91
|
|
|
throw new \RuntimeException('Unsupported diff line type.'); |
|
92
|
|
|
} |
|
93
|
|
|
$lines[] = $line; |
|
94
|
|
|
} |
|
95
|
|
|
return $lines; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getContent(): string |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->content; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return int |
|
108
|
|
|
*/ |
|
109
|
|
|
public function getType(): int |
|
110
|
|
|
{ |
|
111
|
|
|
return $this->type; |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|