1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Snak; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
7
|
|
|
use Wikibase\DataModel\Entity\Property; |
8
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Base class for snaks. |
12
|
|
|
* See https://www.mediawiki.org/wiki/Wikibase/DataModel#Snaks |
13
|
|
|
* |
14
|
|
|
* @since 0.1 |
15
|
|
|
* |
16
|
|
|
* @license GPL-2.0+ |
17
|
|
|
* @author Jeroen De Dauw < [email protected] > |
18
|
|
|
*/ |
19
|
|
|
abstract class SnakObject implements Snak { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @since 0.1 |
23
|
|
|
* |
24
|
|
|
* @var PropertyId |
25
|
|
|
*/ |
26
|
|
|
protected $propertyId; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Support for passing in an EntityId instance that is not a PropertyId instance has |
30
|
|
|
* been deprecated since 0.5. |
31
|
|
|
* |
32
|
|
|
* @since 0.1 |
33
|
|
|
* |
34
|
|
|
* @param PropertyId|EntityId|int $propertyId |
35
|
|
|
* |
36
|
|
|
* @throws InvalidArgumentException |
37
|
|
|
*/ |
38
|
|
|
public function __construct( $propertyId ) { |
39
|
|
|
if ( is_int( $propertyId ) ) { |
40
|
|
|
$propertyId = PropertyId::newFromNumber( $propertyId ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if ( !( $propertyId instanceof EntityId ) ) { |
44
|
|
|
throw new InvalidArgumentException( '$propertyId must be an instance of EntityId' ); |
45
|
|
|
} |
46
|
|
|
|
47
|
40 |
|
if ( $propertyId->getEntityType() !== Property::ENTITY_TYPE ) { |
48
|
40 |
|
throw new InvalidArgumentException( '$propertyId must have an entityType of ' . Property::ENTITY_TYPE ); |
49
|
4 |
|
} |
50
|
4 |
|
|
51
|
|
|
if ( !( $propertyId instanceof PropertyId ) ) { |
52
|
40 |
|
$propertyId = new PropertyId( $propertyId->getSerialization() ); |
53
|
9 |
|
} |
54
|
|
|
|
55
|
|
|
$this->propertyId = $propertyId; |
56
|
31 |
|
} |
57
|
3 |
|
|
58
|
|
|
/** |
59
|
|
|
* @see PropertyIdProvider::getPropertyId |
60
|
28 |
|
* |
61
|
|
|
* @since 0.1 |
62
|
|
|
* |
63
|
|
|
* @return PropertyId |
64
|
28 |
|
*/ |
65
|
28 |
|
public function getPropertyId() { |
66
|
|
|
return $this->propertyId; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @see Hashable::getHash |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
3 |
|
public function getHash() { |
75
|
3 |
|
return sha1( serialize( $this ) ); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @see Comparable::equals |
80
|
|
|
* |
81
|
|
|
* @since 0.3 |
82
|
|
|
* |
83
|
16 |
|
* @param mixed $target |
84
|
16 |
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function equals( $target ) { |
88
|
|
|
if ( $this === $target ) { |
89
|
|
|
return true; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return is_object( $target ) |
93
|
|
|
&& get_called_class() === get_class( $target ) |
94
|
|
|
&& $this->getHash() === $target->getHash(); |
95
|
|
|
} |
96
|
12 |
|
|
97
|
12 |
|
/** |
98
|
|
|
* @see Serializable::serialize |
99
|
|
|
* |
100
|
|
|
* @since 0.1 |
101
|
12 |
|
* |
102
|
12 |
|
* @return string |
103
|
12 |
|
*/ |
104
|
|
|
public function serialize() { |
105
|
|
|
return $this->propertyId->getSerialization(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @see Serializable::unserialize |
110
|
|
|
* |
111
|
|
|
* @since 0.1 |
112
|
|
|
* |
113
|
12 |
|
* @param string $serialized |
114
|
12 |
|
*/ |
115
|
|
|
public function unserialize( $serialized ) { |
116
|
|
|
$this->propertyId = self::newPropertyId( $serialized ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @param string $serialized |
121
|
|
|
* |
122
|
|
|
* @return PropertyId |
123
|
|
|
*/ |
124
|
2 |
|
protected static function newPropertyId( $serialized ) { |
125
|
2 |
|
if ( is_int( $serialized ) ) { |
126
|
2 |
|
// numeric id, already unserialized |
127
|
|
|
return PropertyId::newFromNumber( $serialized ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
try { |
131
|
|
|
// full ID, as a string, not serialized |
132
|
|
|
return new PropertyId( $serialized ); |
133
|
|
|
} catch ( InvalidArgumentException $ex ) { |
134
|
|
|
// noop |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
$unserialized = unserialize( $serialized ); |
138
|
|
|
if ( is_int( $unserialized ) ) { |
139
|
|
|
// numeric id, not previously unserialized |
140
|
|
|
return PropertyId::newFromNumber( $unserialized ); |
141
|
|
|
} else { |
142
|
|
|
throw new InvalidArgumentException( 'unexpected property ID serialization' ); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
|