-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathStatement.php
More file actions
311 lines (275 loc) · 6.01 KB
/
Statement.php
File metadata and controls
311 lines (275 loc) · 6.01 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
namespace Wikibase\DataModel\Statement;
use Comparable;
use Hashable;
use InvalidArgumentException;
use Wikibase\DataModel\Entity\PropertyId;
use Wikibase\DataModel\PropertyIdProvider;
use Wikibase\DataModel\Reference;
use Wikibase\DataModel\ReferenceList;
use Wikibase\DataModel\Snak\Snak;
use Wikibase\DataModel\Snak\SnakList;
/**
* Class representing a Wikibase statement.
* See https://www.mediawiki.org/wiki/Wikibase/DataModel#Statements
*
* @since 0.1
*
* @license GPL-2.0+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
* @author Bene* < benestar.wikimedia@gmail.com >
*/
class Statement implements Hashable, Comparable, PropertyIdProvider {
/**
* @deprecated since 4.4, use StatementRank::PREFERRED instead.
*/
const RANK_PREFERRED = StatementRank::PREFERRED;
/**
* @deprecated since 4.4, use StatementRank::NORMAL instead.
*/
const RANK_NORMAL = StatementRank::NORMAL;
/**
* @deprecated since 4.4, use StatementRank::DEPRECATED instead.
*/
const RANK_DEPRECATED = StatementRank::DEPRECATED;
/**
* @var string|null
*/
private $guid = null;
/**
* @var Snak
*/
private $mainSnak;
/**
* The property value snaks making up the qualifiers for this statement.
*
* @var SnakList
*/
private $qualifiers;
/**
* @var ReferenceList
*/
private $references;
/**
* @var int One of the StatementRank::... constants.
*/
private $rank = StatementRank::NORMAL;
/**
* @since 2.0
*
* @param Snak $mainSnak
* @param SnakList|null $qualifiers
* @param ReferenceList|null $references
* @param string|null $guid
*/
public function __construct(
Snak $mainSnak,
SnakList $qualifiers = null,
ReferenceList $references = null,
$guid = null
) {
$this->mainSnak = $mainSnak;
$this->qualifiers = $qualifiers ?: new SnakList();
$this->references = $references ?: new ReferenceList();
$this->setGuid( $guid );
}
/**
* @since 0.2
*
* @return string|null
*/
public function getGuid() {
return $this->guid;
}
/**
* Sets the GUID of this statement.
*
* @since 0.2
*
* @param string|null $guid
*
* @throws InvalidArgumentException
*/
public function setGuid( $guid ) {
if ( !is_string( $guid ) && $guid !== null ) {
throw new InvalidArgumentException( '$guid must be a string or null' );
}
$this->guid = $guid;
}
/**
* Returns the main value snak of this statement.
*
* @since 0.1
*
* @return Snak
*/
public function getMainSnak() {
return $this->mainSnak;
}
/**
* Sets the main value snak of this statement.
*
* @since 0.1
*
* @param Snak $mainSnak
*/
public function setMainSnak( Snak $mainSnak ) {
$this->mainSnak = $mainSnak;
}
/**
* Returns the property value snaks making up the qualifiers for this statement.
*
* @since 0.1
*
* @return SnakList
*/
public function getQualifiers() {
return $this->qualifiers;
}
/**
* Sets the property value snaks making up the qualifiers for this statement.
*
* @since 0.1
*
* @param SnakList $propertySnaks
*/
public function setQualifiers( SnakList $propertySnaks ) {
$this->qualifiers = $propertySnaks;
}
/**
* Returns the references attached to this statement.
*
* @since 0.1
*
* @return ReferenceList
*/
public function getReferences() {
return $this->references;
}
/**
* Sets the references attached to this statement.
*
* @since 0.1
*
* @param ReferenceList $references
*/
public function setReferences( ReferenceList $references ) {
$this->references = $references;
}
/**
* @since 2.0
*
* @param Snak[]|Snak $snaks
* @param Snak [$snak2,...]
*
* @throws InvalidArgumentException
*/
public function addNewReference( $snaks = [] /*...*/ ) {
if ( $snaks instanceof Snak ) {
$snaks = func_get_args();
}
$this->references->addNewReference( $snaks );
}
/**
* Sets the rank of the statement.
*
* @since 0.1
*
* @param int $rank One of the StatementRank::... constants.
*
* @throws InvalidArgumentException
*/
public function setRank( $rank ) {
StatementRank::assertIsValid( $rank );
$this->rank = $rank;
}
/**
* @since 0.1
*
* @return integer
*/
public function getRank() {
return $this->rank;
}
/**
* @see Hashable::getHash
*
* @since 0.1
*
* @return string
*/
public function getHash() {
return sha1( implode(
'|',
[
sha1( $this->mainSnak->getHash() . $this->qualifiers->getHash() ),
$this->rank,
$this->references->getValueHash(),
]
) );
}
/**
* Returns the id of the property of the main snak.
* Short for ->getMainSnak()->getPropertyId()
*
* @see PropertyIdProvider::getPropertyId
*
* @since 0.2
*
* @return PropertyId
*/
public function getPropertyId() {
return $this->getMainSnak()->getPropertyId();
}
/**
* Returns a list of all Snaks on this statement. This includes the main snak and all snaks
* from qualifiers and references.
*
* This is a convenience method for use in code that needs to operate on all snaks, e.g.
* to find all referenced Entities.
*
* @return Snak[]
*/
public function getAllSnaks() {
$snaks = [ $this->mainSnak ];
foreach ( $this->qualifiers as $qualifier ) {
$snaks[] = $qualifier;
}
/* @var Reference $reference */
foreach ( $this->getReferences() as $reference ) {
foreach ( $reference->getSnaks() as $referenceSnak ) {
$snaks[] = $referenceSnak;
}
}
return $snaks;
}
/**
* @see Comparable::equals
*
* @since 0.7.4
*
* @param mixed $target
*
* @return bool
*/
public function equals( $target ) {
if ( $this === $target ) {
return true;
}
return $target instanceof self
&& $this->guid === $target->guid
&& $this->rank === $target->rank
&& $this->mainSnak->equals( $target->mainSnak )
&& $this->qualifiers->equals( $target->qualifiers )
&& $this->references->equals( $target->references );
}
/**
* @see http://php.net/manual/en/language.oop5.cloning.php
*
* @since 5.1
*/
public function __clone() {
$this->qualifiers = clone $this->qualifiers;
$this->references = clone $this->references;
}
}