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
|
|
|
/** |
41
|
|
|
* @var int |
42
|
|
|
*/ |
43
|
|
|
protected $index; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Line constructor. |
47
|
|
|
* @param int $type |
48
|
|
|
* @param string $content |
49
|
|
|
* @param int $index |
50
|
|
|
*/ |
51
|
14 |
|
public function __construct($type = self::UNCHANGED, $content = '', $index = null) |
52
|
|
|
{ |
53
|
14 |
|
$this->type = $type; |
54
|
14 |
|
$this->content = $content; |
55
|
14 |
|
$this->index = $index; |
56
|
14 |
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return int |
60
|
|
|
*/ |
61
|
13 |
|
public function getIndex() |
62
|
|
|
{ |
63
|
13 |
|
return $this->index; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array $diff |
68
|
|
|
* @return Line[] |
69
|
|
|
*/ |
70
|
13 |
|
public static function createArray($diff) |
71
|
|
|
{ |
72
|
13 |
|
$index = -1; |
73
|
13 |
|
$lines = []; |
74
|
13 |
|
foreach ($diff as $key => $value) { |
75
|
13 |
|
switch ($value[1]) { |
76
|
13 |
|
case 0: |
77
|
13 |
|
$index++; |
78
|
13 |
|
$line = new Line(Line::UNCHANGED, $value[0], $index); |
79
|
13 |
|
break; |
80
|
|
|
|
81
|
13 |
|
case 1: |
82
|
13 |
|
$line = new Line(Line::ADDED, $value[0], $index); |
83
|
13 |
|
break; |
84
|
|
|
|
85
|
13 |
|
case 2: |
86
|
13 |
|
$index++; |
87
|
13 |
|
$line = new Line(Line::REMOVED, $value[0], $index); |
88
|
13 |
|
break; |
89
|
|
|
|
90
|
|
|
default: |
91
|
1 |
|
throw new \RuntimeException('Unsupported diff line type.'); |
92
|
|
|
} |
93
|
13 |
|
$lines[] = $line; |
94
|
|
|
} |
95
|
13 |
|
return $lines; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
8 |
|
public function getContent(): string |
102
|
|
|
{ |
103
|
8 |
|
return $this->content; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return int |
108
|
|
|
*/ |
109
|
13 |
|
public function getType(): int |
110
|
|
|
{ |
111
|
13 |
|
return $this->type; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|