Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
79.25% |
42 / 53 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
FormDiffView | |
79.25% |
42 / 53 |
|
75.00% |
3 / 4 |
13.29 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
generateOpHtml | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
generateFormOpHtml | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
2 | |||
getClaimDiffHtml | |
21.43% |
3 / 14 |
|
0.00% |
0 / 1 |
17.13 |
1 | <?php |
2 | |
3 | namespace Wikibase\Lexeme\Presentation\Diff; |
4 | |
5 | use Diff\DiffOp\Diff\Diff; |
6 | use Diff\DiffOp\DiffOp; |
7 | use Diff\DiffOp\DiffOpAdd; |
8 | use Diff\DiffOp\DiffOpChange; |
9 | use Diff\DiffOp\DiffOpRemove; |
10 | use InvalidArgumentException; |
11 | use MessageLocalizer; |
12 | use Wikibase\Lexeme\Domain\Diff\FormDiff; |
13 | use Wikibase\Repo\Diff\BasicDiffView; |
14 | use Wikibase\Repo\Diff\ClaimDiffer; |
15 | use Wikibase\Repo\Diff\ClaimDifferenceVisualizer; |
16 | |
17 | /** |
18 | * Class for generating views of DiffOp objects of forms. |
19 | * |
20 | * @license GPL-2.0-or-later |
21 | */ |
22 | class FormDiffView extends BasicDiffView { |
23 | |
24 | /** |
25 | * @var ClaimDiffer |
26 | */ |
27 | private $claimDiffer; |
28 | |
29 | /** |
30 | * @var ClaimDifferenceVisualizer |
31 | */ |
32 | private $claimDiffVisualizer; |
33 | |
34 | /** |
35 | * @var ItemReferenceDifferenceVisualizer |
36 | */ |
37 | private $itemReferenceDifferenceVisualizer; |
38 | |
39 | /** |
40 | * @var MessageLocalizer |
41 | */ |
42 | private $messageLocalizer; |
43 | |
44 | /** |
45 | * @param string[] $path |
46 | * @param Diff $diff |
47 | * @param ClaimDiffer $claimDiffer |
48 | * @param ClaimDifferenceVisualizer $claimDiffVisualizer |
49 | * @param ItemReferenceDifferenceVisualizer $itemReferenceDifferenceVisualizer |
50 | * @param MessageLocalizer $messageLocalizer |
51 | */ |
52 | public function __construct( |
53 | array $path, |
54 | Diff $diff, |
55 | ClaimDiffer $claimDiffer, |
56 | ClaimDifferenceVisualizer $claimDiffVisualizer, |
57 | ItemReferenceDifferenceVisualizer $itemReferenceDifferenceVisualizer, |
58 | MessageLocalizer $messageLocalizer |
59 | ) { |
60 | parent::__construct( $path, $diff ); |
61 | |
62 | $this->claimDiffer = $claimDiffer; |
63 | $this->claimDiffVisualizer = $claimDiffVisualizer; |
64 | $this->itemReferenceDifferenceVisualizer = $itemReferenceDifferenceVisualizer; |
65 | $this->messageLocalizer = $messageLocalizer; |
66 | } |
67 | |
68 | /** |
69 | * @param string[] $path |
70 | * @param DiffOp $op |
71 | * |
72 | * @return string HTML |
73 | */ |
74 | protected function generateOpHtml( array $path, DiffOp $op ) { |
75 | if ( $op->isAtomic() ) { |
76 | return parent::generateOpHtml( $path, $op ); |
77 | } |
78 | |
79 | $html = ''; |
80 | |
81 | // @phan-suppress-next-line PhanTypeNoPropertiesForeach |
82 | foreach ( $op as $key => $subOp ) { |
83 | if ( $subOp instanceof FormDiff ) { |
84 | $html .= $this->generateFormOpHtml( $path, $subOp, $key ); |
85 | } else { |
86 | $html .= $this->generateOpHtml( array_merge( $path, [ $key ] ), $subOp ); |
87 | } |
88 | } |
89 | |
90 | return $html; |
91 | } |
92 | |
93 | private function generateFormOpHtml( array $path, FormDiff $op, string $key ): string { |
94 | $html = ''; |
95 | |
96 | $html .= parent::generateOpHtml( |
97 | array_merge( |
98 | $path, |
99 | [ $key, $this->messageLocalizer->msg( 'wikibaselexeme-diffview-representation' )->text() ] |
100 | ), |
101 | $op->getRepresentationDiff() |
102 | ); |
103 | |
104 | $html .= ( new GrammaticalFeatureDiffVisualizer( |
105 | $this->itemReferenceDifferenceVisualizer |
106 | ) )->visualize( |
107 | array_merge( |
108 | $path, |
109 | [ |
110 | $key, |
111 | $this->messageLocalizer->msg( 'wikibaselexeme-diffview-grammatical-feature' )->text(), |
112 | ] |
113 | ), |
114 | $op->getGrammaticalFeaturesDiff() |
115 | ); |
116 | |
117 | foreach ( $op->getStatementsDiff() as $claimDiffOp ) { |
118 | $html .= $this->getClaimDiffHtml( |
119 | $claimDiffOp, |
120 | array_merge( $path, [ $key ] ) |
121 | ); |
122 | } |
123 | |
124 | return $html; |
125 | } |
126 | |
127 | /** |
128 | * @param DiffOp $diffOp |
129 | * |
130 | * @return string HTML |
131 | */ |
132 | private function getClaimDiffHtml( DiffOp $diffOp, array $path ) { |
133 | switch ( true ) { |
134 | case $diffOp instanceof DiffOpChange: |
135 | return $this->claimDiffVisualizer->visualizeClaimChange( |
136 | $this->claimDiffer->diffClaims( |
137 | $diffOp->getOldValue(), |
138 | $diffOp->getNewValue() |
139 | ), |
140 | $diffOp->getNewValue(), |
141 | $path |
142 | ); |
143 | |
144 | case $diffOp instanceof DiffOpAdd: |
145 | return $this->claimDiffVisualizer->visualizeNewClaim( $diffOp->getNewValue(), $path ); |
146 | |
147 | case $diffOp instanceof DiffOpRemove: |
148 | return $this->claimDiffVisualizer->visualizeRemovedClaim( $diffOp->getOldValue(), $path ); |
149 | |
150 | default: |
151 | throw new InvalidArgumentException( 'Encountered an unexpected diff operation type for a claim' ); |
152 | } |
153 | } |
154 | |
155 | } |