1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Services\Diff; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use RuntimeException; |
7
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @since 1.0 |
11
|
|
|
* |
12
|
|
|
* @license GPL-2.0-or-later |
13
|
|
|
* @author Jeroen De Dauw < [email protected] > |
14
|
|
|
*/ |
15
|
|
|
class EntityDiffer { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var EntityDifferStrategy[] |
19
|
|
|
*/ |
20
|
|
|
private $differStrategies; |
21
|
|
|
|
22
|
5 |
|
public function __construct() { |
23
|
5 |
|
$this->registerEntityDifferStrategy( new ItemDiffer() ); |
24
|
5 |
|
$this->registerEntityDifferStrategy( new PropertyDiffer() ); |
25
|
5 |
|
} |
26
|
|
|
|
27
|
5 |
|
public function registerEntityDifferStrategy( EntityDifferStrategy $differStrategy ) { |
28
|
5 |
|
$this->differStrategies[] = $differStrategy; |
29
|
5 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param EntityDocument $from |
33
|
|
|
* @param EntityDocument $to |
34
|
|
|
* |
35
|
|
|
* @return EntityDiff |
36
|
|
|
* @throws InvalidArgumentException |
37
|
|
|
* @throws RuntimeException |
38
|
|
|
*/ |
39
|
3 |
|
public function diffEntities( EntityDocument $from, EntityDocument $to ) { |
40
|
3 |
|
$this->assertTypesMatch( $from, $to ); |
41
|
|
|
|
42
|
2 |
|
return $this->getDiffStrategy( $from->getType() )->diffEntities( $from, $to ); |
43
|
|
|
} |
44
|
|
|
|
45
|
3 |
|
private function assertTypesMatch( EntityDocument $from, EntityDocument $to ) { |
46
|
3 |
|
if ( $from->getType() !== $to->getType() ) { |
47
|
1 |
|
throw new InvalidArgumentException( 'Can only diff two entities of the same type' ); |
48
|
|
|
} |
49
|
2 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $entityType |
53
|
|
|
* |
54
|
|
|
* @throws RuntimeException |
55
|
|
|
* @return EntityDifferStrategy |
56
|
|
|
*/ |
57
|
4 |
|
private function getDiffStrategy( $entityType ) { |
58
|
4 |
|
foreach ( $this->differStrategies as $diffStrategy ) { |
59
|
4 |
|
if ( $diffStrategy->canDiffEntityType( $entityType ) ) { |
60
|
1 |
|
return $diffStrategy; |
61
|
|
|
} |
62
|
3 |
|
} |
63
|
|
|
|
64
|
3 |
|
throw new RuntimeException( 'Diffing the provided types of entities is not supported' ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param EntityDocument $entity |
69
|
|
|
* |
70
|
|
|
* @return EntityDiff |
71
|
|
|
* @throws InvalidArgumentException |
72
|
|
|
*/ |
73
|
1 |
|
public function getConstructionDiff( EntityDocument $entity ) { |
74
|
1 |
|
return $this->getDiffStrategy( $entity->getType() )->getConstructionDiff( $entity ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param EntityDocument $entity |
79
|
|
|
* |
80
|
|
|
* @return EntityDiff |
81
|
|
|
* @throws InvalidArgumentException |
82
|
|
|
*/ |
83
|
1 |
|
public function getDestructionDiff( EntityDocument $entity ) { |
84
|
1 |
|
return $this->getDiffStrategy( $entity->getType() )->getDestructionDiff( $entity ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
} |
88
|
|
|
|