Statement::getRank()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Wikibase\DataModel\Statement;
4
5
use InvalidArgumentException;
6
use Wikibase\DataModel\Entity\PropertyId;
7
use Wikibase\DataModel\PropertyIdProvider;
8
use Wikibase\DataModel\ReferenceList;
9
use Wikibase\DataModel\Snak\Snak;
10
use Wikibase\DataModel\Snak\SnakList;
11
12
/**
13
 * Class representing a Wikibase statement.
14
 * See https://www.mediawiki.org/wiki/Wikibase/DataModel#Statements
15
 *
16
 * @since 0.1
17
 *
18
 * @license GPL-2.0-or-later
19
 * @author Jeroen De Dauw < [email protected] >
20
 * @author Bene* < [email protected] >
21
 */
22
class Statement implements PropertyIdProvider {
23
24
	/**
25
	 * Rank enum. Higher values are more preferred.
26
	 *
27
	 * @since 2.0
28
	 */
29
	public const RANK_PREFERRED = 2;
30
	public const RANK_NORMAL = 1;
31
	public const RANK_DEPRECATED = 0;
32
33
	/**
34
	 * @var string|null
35
	 */
36
	private $guid = null;
37
38
	/**
39
	 * @var Snak
40
	 */
41
	private $mainSnak;
42
43
	/**
44
	 * The property value snaks making up the qualifiers for this statement.
45
	 *
46
	 * @var SnakList
47
	 */
48
	private $qualifiers;
49
50
	/**
51
	 * @var ReferenceList
52
	 */
53
	private $references;
54
55
	/**
56
	 * @var int element of the Statement::RANK_ enum
57
	 */
58
	private $rank = self::RANK_NORMAL;
59
60
	/**
61
	 * @since 2.0
62
	 *
63
	 * @param Snak $mainSnak
64
	 * @param SnakList|null $qualifiers
65
	 * @param ReferenceList|null $references
66
	 * @param string|null $guid
67
	 */
68
	public function __construct(
69
		Snak $mainSnak,
70
		SnakList $qualifiers = null,
71
		ReferenceList $references = null,
72
		$guid = null
73
	) {
74
		$this->mainSnak = $mainSnak;
75
		$this->qualifiers = $qualifiers ?: new SnakList();
76
		$this->references = $references ?: new ReferenceList();
77
		$this->setGuid( $guid );
78
	}
79
80 23
	/**
81
	 * @since 0.2
82
	 *
83
	 * @return string|null
84
	 */
85
	public function getGuid() {
86 23
		return $this->guid;
87 23
	}
88 23
89 23
	/**
90 19
	 * Sets the GUID of this statement.
91
	 *
92
	 * @since 0.2
93
	 *
94
	 * @param string|null $guid
95
	 *
96
	 * @throws InvalidArgumentException
97
	 */
98
	public function setGuid( $guid ) {
99 11
		if ( !is_string( $guid ) && $guid !== null ) {
100 11
			throw new InvalidArgumentException( '$guid must be a string or null' );
101
		}
102
103
		$this->guid = $guid;
104
	}
105
106
	/**
107
	 * Returns the main value snak of this statement.
108
	 *
109
	 * @since 0.1
110
	 *
111
	 * @return Snak
112 31
	 */
113 31
	public function getMainSnak() {
114 8
		return $this->mainSnak;
115
	}
116
117 27
	/**
118 27
	 * Sets the main value snak of this statement.
119
	 *
120
	 * @since 0.1
121
	 *
122
	 * @param Snak $mainSnak
123
	 */
124
	public function setMainSnak( Snak $mainSnak ) {
125
		$this->mainSnak = $mainSnak;
126
	}
127 9
128 9
	/**
129
	 * Returns the property value snaks making up the qualifiers for this statement.
130
	 *
131
	 * @since 0.1
132
	 *
133
	 * @return SnakList
134
	 */
135
	public function getQualifiers() {
136
		return $this->qualifiers;
137
	}
138
139
	/**
140
	 * Sets the property value snaks making up the qualifiers for this statement.
141
	 *
142
	 * @since 0.1
143
	 *
144
	 * @param SnakList $propertySnaks
145
	 */
146
	public function setQualifiers( SnakList $propertySnaks ) {
147
		$this->qualifiers = $propertySnaks;
148
	}
149 8
150 8
	/**
151
	 * Returns the references attached to this statement.
152
	 *
153
	 * @since 0.1
154
	 *
155
	 * @return ReferenceList
156
	 */
157
	public function getReferences() {
158
		return $this->references;
159
	}
160
161
	/**
162
	 * Sets the references attached to this statement.
163
	 *
164
	 * @since 0.1
165
	 *
166
	 * @param ReferenceList $references
167
	 */
168
	public function setReferences( ReferenceList $references ) {
169
		$this->references = $references;
170
	}
171 23
172 23
	/**
173
	 * @since 2.0
174
	 *
175
	 * @param Snak ...$snaks
176
	 * (passing a single Snak[] is still supported but deprecated)
177
	 *
178
	 * @throws InvalidArgumentException
179
	 */
180
	public function addNewReference( ...$snaks ) {
181
		if ( count( $snaks ) === 1 && is_array( $snaks[0] ) ) {
182 4
			// TODO stop supporting this
183 4
			$snaks = $snaks[0];
184 4
		}
185
186
		$this->references->addNewReference( $snaks );
187
	}
188
189
	/**
190
	 * Sets the rank of the statement.
191
	 * The rank is an element of the Statement::RANK_ enum.
192
	 *
193
	 * @since 0.1
194 8
	 *
195 8
	 * @param integer $rank
196 4
	 * @throws InvalidArgumentException
197 4
	 */
198
	public function setRank( $rank ) {
199 8
		$ranks = [ self::RANK_DEPRECATED, self::RANK_NORMAL, self::RANK_PREFERRED ];
200 8
201
		if ( !in_array( $rank, $ranks, true ) ) {
202
			throw new InvalidArgumentException( 'Invalid rank specified for statement: ' . var_export( $rank, true ) );
203
		}
204
205
		$this->rank = $rank;
206
	}
207
208
	/**
209
	 * @since 0.1
210
	 *
211 9
	 * @return integer
212 9
	 */
213
	public function getRank() {
214 9
		return $this->rank;
215 4
	}
216
217
	/**
218 5
	 * @since 0.1
219 5
	 *
220
	 * @return string
221
	 */
222
	public function getHash() {
223
		return sha1( implode(
224
			'|',
225
			[
226 8
				sha1( $this->mainSnak->getHash() . $this->qualifiers->getHash() ),
227 8
				$this->rank,
228
				$this->references->getValueHash(),
229
			]
230
		) );
231
	}
232
233
	/**
234
	 * Returns the id of the property of the main snak.
235
	 * Short for ->getMainSnak()->getPropertyId()
236
	 *
237 5
	 * @see PropertyIdProvider::getPropertyId
238 5
	 *
239 5
	 * @since 0.2
240
	 *
241 5
	 * @return PropertyId
242 5
	 */
243 5
	public function getPropertyId() {
244
		return $this->getMainSnak()->getPropertyId();
245 5
	}
246
247
	/**
248
	 * Returns a list of all Snaks on this statement. This includes the main snak and all snaks
249
	 * from qualifiers and references.
250
	 *
251
	 * This is a convenience method for use in code that needs to operate on all snaks, e.g.
252
	 * to find all referenced Entities.
253
	 *
254
	 * @return Snak[]
255
	 */
256
	public function getAllSnaks() {
257
		$snaks = [ $this->mainSnak ];
258 4
259 4
		foreach ( $this->qualifiers as $qualifier ) {
260
			$snaks[] = $qualifier;
261
		}
262
263
		/* @var Reference $reference */
264
		foreach ( $this->getReferences() as $reference ) {
265
			foreach ( $reference->getSnaks() as $referenceSnak ) {
266
				$snaks[] = $referenceSnak;
267
			}
268
		}
269
270
		return $snaks;
271 4
	}
272 4
273
	/**
274 4
	 *
275
	 * @since 0.7.4
276 4
	 *
277
	 * @param mixed $target
278
	 *
279 4
	 * @return bool
280 1
	 */
281 1
	public function equals( $target ) {
282 1
		if ( $this === $target ) {
283 4
			return true;
284
		}
285 4
286
		return $target instanceof self
287
			&& $this->guid === $target->guid
288
			&& $this->rank === $target->rank
289
			&& $this->mainSnak->equals( $target->mainSnak )
290
			&& $this->qualifiers->equals( $target->qualifiers )
291
			&& $this->references->equals( $target->references );
292
	}
293
294
	/**
295
	 * @see http://php.net/manual/en/language.oop5.cloning.php
296
	 *
297 12
	 * @since 5.1
298 12
	 */
299 1
	public function __clone() {
300
		$this->qualifiers = clone $this->qualifiers;
301
		$this->references = clone $this->references;
302
	}
303 12
304
}
305