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 MessageLocalizer $messageLocalizer |
50 | */ |
51 | public function __construct( |
52 | array $path, |
53 | Diff $diff, |
54 | ClaimDiffer $claimDiffer, |
55 | ClaimDifferenceVisualizer $claimDiffVisualizer, |
56 | ItemReferenceDifferenceVisualizer $itemReferenceDifferenceVisualizer, |
57 | MessageLocalizer $messageLocalizer |
58 | ) { |
59 | parent::__construct( $path, $diff ); |
60 | |
61 | $this->claimDiffer = $claimDiffer; |
62 | $this->claimDiffVisualizer = $claimDiffVisualizer; |
63 | $this->itemReferenceDifferenceVisualizer = $itemReferenceDifferenceVisualizer; |
64 | $this->messageLocalizer = $messageLocalizer; |
65 | } |
66 | |
67 | /** |
68 | * @param string[] $path |
69 | * @param DiffOp $op |
70 | * |
71 | * @return string HTML |
72 | */ |
73 | protected function generateOpHtml( array $path, DiffOp $op ) { |
74 | if ( $op->isAtomic() ) { |
75 | return parent::generateOpHtml( $path, $op ); |
76 | } |
77 | |
78 | $html = ''; |
79 | |
80 | // @phan-suppress-next-line PhanTypeNoPropertiesForeach |
81 | foreach ( $op as $key => $subOp ) { |
82 | if ( $subOp instanceof FormDiff ) { |
83 | $html .= $this->generateFormOpHtml( $path, $subOp, $key ); |
84 | } else { |
85 | $html .= $this->generateOpHtml( array_merge( $path, [ $key ] ), $subOp ); |
86 | } |
87 | } |
88 | |
89 | return $html; |
90 | } |
91 | |
92 | private function generateFormOpHtml( array $path, FormDiff $op, $key ) { |
93 | $html = ''; |
94 | |
95 | $html .= parent::generateOpHtml( |
96 | array_merge( |
97 | $path, |
98 | [ $key, $this->messageLocalizer->msg( 'wikibaselexeme-diffview-representation' )->text() ] |
99 | ), |
100 | $op->getRepresentationDiff() |
101 | ); |
102 | |
103 | $html .= ( new GrammaticalFeatureDiffVisualizer( |
104 | $this->itemReferenceDifferenceVisualizer |
105 | ) )->visualize( |
106 | array_merge( |
107 | $path, |
108 | [ |
109 | $key, |
110 | $this->messageLocalizer->msg( 'wikibaselexeme-diffview-grammatical-feature' )->text(), |
111 | ] |
112 | ), |
113 | $op->getGrammaticalFeaturesDiff() |
114 | ); |
115 | |
116 | foreach ( $op->getStatementsDiff() as $claimDiffOp ) { |
117 | $html .= $this->getClaimDiffHtml( |
118 | $claimDiffOp, |
119 | array_merge( $path, [ $key ] ) |
120 | ); |
121 | } |
122 | |
123 | return $html; |
124 | } |
125 | |
126 | /** |
127 | * @param DiffOp $diffOp |
128 | * |
129 | * @return string HTML |
130 | */ |
131 | private function getClaimDiffHtml( DiffOp $diffOp, array $path ) { |
132 | switch ( true ) { |
133 | case $diffOp instanceof DiffOpChange: |
134 | return $this->claimDiffVisualizer->visualizeClaimChange( |
135 | $this->claimDiffer->diffClaims( |
136 | $diffOp->getOldValue(), |
137 | $diffOp->getNewValue() |
138 | ), |
139 | $diffOp->getNewValue(), |
140 | $path |
141 | ); |
142 | |
143 | case $diffOp instanceof DiffOpAdd: |
144 | return $this->claimDiffVisualizer->visualizeNewClaim( $diffOp->getNewValue(), $path ); |
145 | |
146 | case $diffOp instanceof DiffOpRemove: |
147 | return $this->claimDiffVisualizer->visualizeRemovedClaim( $diffOp->getOldValue(), $path ); |
148 | |
149 | default: |
150 | throw new InvalidArgumentException( 'Encountered an unexpected diff operation type for a claim' ); |
151 | } |
152 | } |
153 | |
154 | } |