Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
92.00% |
23 / 25 |
|
75.00% |
6 / 8 |
CRAP | |
0.00% |
0 / 1 |
MediaInfoDiffer | |
92.00% |
23 / 25 |
|
75.00% |
6 / 8 |
9.04 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
canDiffEntityType | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
diffEntities | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
assertIsMediaInfo | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
2.50 | |||
diffMediaInfos | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
toDiffArray | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getConstructionDiff | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
getDestructionDiff | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace Wikibase\MediaInfo\DataModel\Services\Diff; |
4 | |
5 | use Diff\Differ\MapDiffer; |
6 | use InvalidArgumentException; |
7 | use Wikibase\DataModel\Entity\EntityDocument; |
8 | use Wikibase\DataModel\Services\Diff\EntityDiff; |
9 | use Wikibase\DataModel\Services\Diff\EntityDifferStrategy; |
10 | use Wikibase\DataModel\Services\Diff\StatementListDiffer; |
11 | use Wikibase\MediaInfo\DataModel\MediaInfo; |
12 | |
13 | /** |
14 | * @since 0.1 |
15 | * |
16 | * @license GPL-2.0-or-later |
17 | * @author Jeroen De Dauw < jeroendedauw@gmail.com > |
18 | * @author Thiemo Kreuz |
19 | */ |
20 | class MediaInfoDiffer implements EntityDifferStrategy { |
21 | |
22 | /** |
23 | * @var MapDiffer |
24 | */ |
25 | private $recursiveMapDiffer; |
26 | |
27 | /** |
28 | * @var StatementListDiffer |
29 | */ |
30 | private $statementListDiffer; |
31 | |
32 | public function __construct() { |
33 | $this->recursiveMapDiffer = new MapDiffer( true ); |
34 | $this->statementListDiffer = new StatementListDiffer(); |
35 | } |
36 | |
37 | /** |
38 | * @param string $entityType |
39 | * |
40 | * @return bool |
41 | */ |
42 | public function canDiffEntityType( $entityType ) { |
43 | return $entityType === 'mediainfo'; |
44 | } |
45 | |
46 | /** |
47 | * @param EntityDocument $from |
48 | * @param EntityDocument $to |
49 | * |
50 | * @return EntityDiff |
51 | * @throws InvalidArgumentException |
52 | */ |
53 | public function diffEntities( EntityDocument $from, EntityDocument $to ) { |
54 | $this->assertIsMediaInfo( $from ); |
55 | $this->assertIsMediaInfo( $to ); |
56 | |
57 | return $this->diffMediaInfos( $from, $to ); |
58 | } |
59 | |
60 | /** |
61 | * @param EntityDocument $mediaInfo |
62 | * @phan-assert MediaInfo $mediaInfo |
63 | */ |
64 | private function assertIsMediaInfo( EntityDocument $mediaInfo ) { |
65 | if ( !( $mediaInfo instanceof MediaInfo ) ) { |
66 | throw new InvalidArgumentException( '$mediaInfo must be an instance of MediaInfo' ); |
67 | } |
68 | } |
69 | |
70 | public function diffMediaInfos( MediaInfo $from, MediaInfo $to ) { |
71 | $diffOps = $this->recursiveMapDiffer->doDiff( |
72 | $this->toDiffArray( $from ), |
73 | $this->toDiffArray( $to ) |
74 | ); |
75 | |
76 | $diffOps['claim'] = $this->statementListDiffer->getDiff( |
77 | $from->getStatements(), |
78 | $to->getStatements() |
79 | ); |
80 | |
81 | return new EntityDiff( $diffOps ); |
82 | } |
83 | |
84 | private function toDiffArray( MediaInfo $mediaInfo ) { |
85 | $array = []; |
86 | |
87 | $array['label'] = $mediaInfo->getLabels()->toTextArray(); |
88 | $array['description'] = $mediaInfo->getDescriptions()->toTextArray(); |
89 | |
90 | return $array; |
91 | } |
92 | |
93 | /** |
94 | * @param EntityDocument $entity |
95 | * |
96 | * @return EntityDiff |
97 | * @throws InvalidArgumentException |
98 | */ |
99 | public function getConstructionDiff( EntityDocument $entity ) { |
100 | $this->assertIsMediaInfo( $entity ); |
101 | return $this->diffEntities( new MediaInfo(), $entity ); |
102 | } |
103 | |
104 | /** |
105 | * @param EntityDocument $entity |
106 | * |
107 | * @return EntityDiff |
108 | * @throws InvalidArgumentException |
109 | */ |
110 | public function getDestructionDiff( EntityDocument $entity ) { |
111 | $this->assertIsMediaInfo( $entity ); |
112 | return $this->diffEntities( $entity, new MediaInfo() ); |
113 | } |
114 | |
115 | } |