|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* (c) Steve Nebes <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
declare(strict_types=1); |
|
10
|
|
|
|
|
11
|
|
|
namespace SN\DaisyDiff\Html\Ancestor; |
|
12
|
|
|
|
|
13
|
|
|
use SN\DaisyDiff\Html\Dom\TagNode; |
|
14
|
|
|
use SN\DaisyDiff\RangeDifferencer\RangeComparatorInterface; |
|
15
|
|
|
use SN\DaisyDiff\RangeDifferencer\RangeDifference; |
|
16
|
|
|
use SN\DaisyDiff\RangeDifferencer\RangeDifferencer; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* A comparator used when calculating the difference in ancestry of two Nodes. |
|
20
|
|
|
*/ |
|
21
|
|
|
class AncestorComparator implements RangeComparatorInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var TagNode[] */ |
|
24
|
|
|
private $ancestors = []; |
|
25
|
|
|
|
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
private $compareText = ''; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param TagNode[] $ancestors |
|
31
|
|
|
*/ |
|
32
|
52 |
|
public function __construct(array $ancestors) |
|
33
|
|
|
{ |
|
34
|
52 |
|
$this->ancestors = $ancestors; |
|
35
|
52 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @return int |
|
39
|
|
|
*/ |
|
40
|
46 |
|
public function getRangeCount(): int |
|
41
|
|
|
{ |
|
42
|
46 |
|
return \count($this->ancestors); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* {@inheritdoc} |
|
47
|
|
|
*/ |
|
48
|
46 |
|
public function rangesEqual(int $thisIndex, RangeComparatorInterface $other, int $otherIndex): bool |
|
49
|
|
|
{ |
|
50
|
46 |
|
if ($other instanceof AncestorComparator) { |
|
51
|
46 |
|
return $other->getAncestor($otherIndex)->isSameTag($this->getAncestor($thisIndex)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return false; // @codeCoverageIgnore |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* {@inheritdoc} |
|
59
|
|
|
*/ |
|
60
|
23 |
|
public function skipRangeComparison(int $length, int $maxLength, RangeComparatorInterface $other): bool |
|
61
|
|
|
{ |
|
62
|
23 |
|
return false; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param int $index |
|
67
|
|
|
* @return TagNode|null |
|
68
|
|
|
* |
|
69
|
|
|
* @throws \OutOfBoundsException |
|
70
|
|
|
*/ |
|
71
|
48 |
|
public function getAncestor(int $index): ?TagNode |
|
72
|
|
|
{ |
|
73
|
48 |
|
if (isset($this->ancestors[$index])) { |
|
74
|
47 |
|
return $this->ancestors[$index]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
throw new \OutOfBoundsException(\sprintf('Index: %d, Size: %d', $index, \count($this->ancestors))); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function getCompareTxt(): string |
|
84
|
|
|
{ |
|
85
|
1 |
|
return $this->compareText; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param AncestorComparator $other |
|
90
|
|
|
* @return AncestorComparatorResult |
|
91
|
|
|
*/ |
|
92
|
44 |
|
public function getResult(AncestorComparator $other): AncestorComparatorResult |
|
93
|
|
|
{ |
|
94
|
44 |
|
$result = new AncestorComparatorResult(); |
|
95
|
|
|
|
|
96
|
|
|
/** @var RangeDifference[] */ |
|
97
|
44 |
|
$differences = RangeDifferencer::findDifferences($other, $this); |
|
98
|
|
|
|
|
99
|
44 |
|
if (empty($differences)) { |
|
100
|
38 |
|
return $result; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
21 |
|
$changeText = new ChangeTextGenerator($this, $other); |
|
104
|
|
|
|
|
105
|
21 |
|
$result->setChanged(true); |
|
106
|
21 |
|
$result->setChanges($changeText->getChanged($differences)->getText()); |
|
107
|
21 |
|
$result->setHtmlLayoutChanges($changeText->getHtmlLayoutChanges()); |
|
108
|
|
|
|
|
109
|
21 |
|
return $result; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|