|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\EntityStore\DataModel; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Wikibase\DataModel\Entity\EntityDocument; |
|
7
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* An entity containing its serialization |
|
11
|
|
|
* |
|
12
|
|
|
* @licence GPLv2+ |
|
13
|
|
|
* @author Thomas Pellissier Tanon |
|
14
|
|
|
*/ |
|
15
|
|
|
class SerializedEntity implements EntityDocument { |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var EntityId|null |
|
19
|
|
|
*/ |
|
20
|
|
|
private $entityId; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var array |
|
24
|
|
|
*/ |
|
25
|
|
|
private $serialization; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @param EntityId|null $entityId |
|
29
|
|
|
* @param array $serialization |
|
30
|
|
|
*/ |
|
31
|
8 |
|
public function __construct( EntityId $entityId = null, array $serialization ) { |
|
32
|
8 |
|
if( !array_key_exists( 'type', $serialization ) ) { |
|
33
|
1 |
|
throw new InvalidArgumentException( 'The entity serialization does not have a type.' ); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
7 |
|
$this->entityId = $entityId; |
|
37
|
7 |
|
$this->serialization = $serialization; |
|
38
|
7 |
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @see EntityDocument::getId |
|
42
|
|
|
*/ |
|
43
|
3 |
|
public function getId() { |
|
44
|
3 |
|
return $this->entityId; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @see EntityDocument::setId |
|
49
|
|
|
*/ |
|
50
|
|
|
public function setId( $id ) { |
|
51
|
|
|
throw new InvalidArgumentException( 'Read only entity' ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @see EntityDocument::getType |
|
56
|
|
|
*/ |
|
57
|
2 |
|
public function getType() { |
|
58
|
2 |
|
return $this->serialization['type']; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @see EntityDocument::isEmpty |
|
63
|
|
|
*/ |
|
64
|
2 |
|
public function isEmpty() { |
|
65
|
2 |
|
foreach( array_keys( $this->serialization ) as $key ) { |
|
66
|
2 |
|
if( !in_array( $key, [ 'type', 'id' ] ) ) { |
|
67
|
1 |
|
return false; |
|
68
|
|
|
} |
|
69
|
2 |
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
return true; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return array |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function getSerialization() { |
|
78
|
2 |
|
return $this->serialization; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @see Comparable::equals |
|
83
|
|
|
*/ |
|
84
|
|
|
public function equals( $target ) { |
|
85
|
|
|
$target instanceof SerializedEntity && $this->serialization == $target->serialization; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @see EntityDocument::copy |
|
90
|
|
|
*/ |
|
91
|
|
|
public function copy() { |
|
92
|
|
|
return $this; // Read only entity |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|