-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPropertyDiffer.php
More file actions
132 lines (108 loc) · 3.21 KB
/
PropertyDiffer.php
File metadata and controls
132 lines (108 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
namespace Wikibase\DataModel\Services\Diff;
use Diff\Differ\MapDiffer;
use Diff\DiffOp\DiffOp;
use InvalidArgumentException;
use Wikibase\DataModel\Entity\EntityDocument;
use Wikibase\DataModel\Entity\Property;
use Wikibase\DataModel\Statement\StatementList;
/**
* @since 1.0
*
* @license GPL-2.0+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class PropertyDiffer implements EntityDifferStrategy {
/**
* @var MapDiffer
*/
private $recursiveMapDiffer;
/**
* @var StatementListDiffer
*/
private $statementListDiffer;
public function __construct() {
$this->recursiveMapDiffer = new MapDiffer( true );
$this->statementListDiffer = new StatementListDiffer();
}
/**
* @param string $entityType
*
* @return bool
*/
public function canDiffEntityType( $entityType ) {
return $entityType === 'property';
}
/**
* @param EntityDocument $from
* @param EntityDocument $to
*
* @return EntityDiff
* @throws InvalidArgumentException
*/
public function diffEntities( EntityDocument $from, EntityDocument $to ) {
$this->assertIsProperty( $from );
$this->assertIsProperty( $to );
return $this->diffProperties( $from, $to );
}
private function assertIsProperty( EntityDocument $property ) {
if ( !( $property instanceof Property ) ) {
throw new InvalidArgumentException( '$property must be an instance of Property' );
}
}
public function diffProperties( Property $from, Property $to ) {
$diffOps = $this->diffPropertyArrays(
$this->toDiffArray( $from ),
$this->toDiffArray( $to )
);
$diffOps['claim'] = $this->statementListDiffer->getDiff( $from->getStatements(), $to->getStatements() );
return new EntityDiff( $diffOps );
}
/**
* @param array[] $from
* @param array[] $to
*
* @return DiffOp[]
*/
private function diffPropertyArrays( array $from, array $to ) {
return $this->recursiveMapDiffer->doDiff( $from, $to );
}
/**
* @param Property $property
*
* @return array[]
*/
private function toDiffArray( Property $property ) {
$array = array();
$array['aliases'] = $property->getFingerprint()->getAliasGroups()->toTextArray();
$array['label'] = $property->getFingerprint()->getLabels()->toTextArray();
$array['description'] = $property->getFingerprint()->getDescriptions()->toTextArray();
return $array;
}
/**
* @param EntityDocument $entity
*
* @return EntityDiff
* @throws InvalidArgumentException
*/
public function getConstructionDiff( EntityDocument $entity ) {
$this->assertIsProperty( $entity );
/** @var Property $entity */
$diffOps = $this->diffPropertyArrays( array(), $this->toDiffArray( $entity ) );
$diffOps['claim'] = $this->statementListDiffer->getDiff( new StatementList(), $entity->getStatements() );
return new EntityDiff( $diffOps );
}
/**
* @param EntityDocument $entity
*
* @return EntityDiff
* @throws InvalidArgumentException
*/
public function getDestructionDiff( EntityDocument $entity ) {
$this->assertIsProperty( $entity );
/** @var Property $entity */
$diffOps = $this->diffPropertyArrays( $this->toDiffArray( $entity ), array() );
$diffOps['claim'] = $this->statementListDiffer->getDiff( $entity->getStatements(), new StatementList() );
return new EntityDiff( $diffOps );
}
}