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 DaisyDiff\Html\Ancestor; |
12
|
|
|
|
13
|
|
|
use DaisyDiff\Html\Dom\TagNode; |
14
|
|
|
use DaisyDiff\RangeDifferencer\RangeComparatorInterface; |
15
|
|
|
use DaisyDiff\RangeDifferencer\RangeDifference; |
16
|
|
|
use 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
|
11 |
|
public function __construct(?array $ancestors) |
33
|
|
|
{ |
34
|
11 |
|
$this->ancestors = $ancestors ?? []; |
35
|
11 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return int |
39
|
|
|
*/ |
40
|
10 |
|
public function getRangeCount(): int |
41
|
|
|
{ |
42
|
10 |
|
return \count($this->ancestors); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
10 |
|
public function rangesEqual(int $thisIndex, RangeComparatorInterface $other, int $otherIndex): bool |
49
|
|
|
{ |
50
|
10 |
|
if ($other instanceof AncestorComparator) { |
51
|
10 |
|
return $other->getAncestor($otherIndex)->isSameTag($this->getAncestor($thisIndex)); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return false; // @codeCoverageIgnore |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
4 |
|
public function skipRangeComparison(int $length, int $maxLength, RangeComparatorInterface $other): bool |
61
|
|
|
{ |
62
|
4 |
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param int $index |
67
|
|
|
* @return TagNode|null |
68
|
|
|
* |
69
|
|
|
* @throws \OutOfBoundsException |
70
|
|
|
*/ |
71
|
10 |
|
public function getAncestor(int $index): ?TagNode |
72
|
|
|
{ |
73
|
10 |
|
if (!isset($this->ancestors[$index])) { |
74
|
|
|
throw new \OutOfBoundsException(\sprintf('Index: %d, Size: %d', $index, \count($this->ancestors))); |
75
|
|
|
} |
76
|
|
|
|
77
|
10 |
|
return $this->ancestors[$index]; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getCompareTxt(): string |
84
|
|
|
{ |
85
|
|
|
return $this->compareText; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param AncestorComparator $other |
90
|
|
|
* @return AncestorComparatorResult |
91
|
|
|
*/ |
92
|
10 |
|
public function getResult(AncestorComparator $other): AncestorComparatorResult |
93
|
|
|
{ |
94
|
10 |
|
$result = new AncestorComparatorResult(); |
95
|
|
|
|
96
|
|
|
/** @var RangeDifference[] */ |
97
|
10 |
|
$differences = RangeDifferencer::findDifferences($other, $this); |
98
|
|
|
|
99
|
10 |
|
if (empty($differences)) { |
100
|
7 |
|
return $result; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// $changeText = new ChangeTextGenerator($this, $other); |
104
|
|
|
|
105
|
4 |
|
$result->setChanged(true); |
106
|
|
|
// $result->setChanges($changeText->getChanged($differences)->__toString()); |
107
|
|
|
// $result->setHtmlLayoutChanges($changeText->getHtmlLayoutChanges()); |
108
|
|
|
|
109
|
4 |
|
return $result; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|