-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathStatementListDiffer.php
More file actions
61 lines (50 loc) · 1.25 KB
/
StatementListDiffer.php
File metadata and controls
61 lines (50 loc) · 1.25 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
<?php
namespace Wikibase\DataModel\Services\Diff;
use Diff\Differ\MapDiffer;
use Diff\DiffOp\Diff\Diff;
use UnexpectedValueException;
use Wikibase\DataModel\Statement\Statement;
use Wikibase\DataModel\Statement\StatementList;
/**
* @since 3.6
*
* @license GPL-2.0+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class StatementListDiffer {
/**
* @since 1.0
*
* @param StatementList $fromStatements
* @param StatementList $toStatements
*
* @return Diff
* @throws UnexpectedValueException
*/
public function getDiff( StatementList $fromStatements, StatementList $toStatements ) {
return new Diff(
$this->newDiffer()->doDiff(
$this->toDiffArray( $fromStatements ),
$this->toDiffArray( $toStatements )
),
true
);
}
private function newDiffer() {
$differ = new MapDiffer();
$differ->setComparisonCallback( function( Statement $fromStatement, Statement $toStatement ) {
return $fromStatement->equals( $toStatement );
} );
return $differ;
}
private function toDiffArray( StatementList $statementList ) {
$statementArray = array();
/**
* @var Statement $statement
*/
foreach ( $statementList as $statement ) {
$statementArray[$statement->getGuid()] = $statement;
}
return $statementArray;
}
}