Passed
Push — itemGetSiteLinks ( a80808 )
by no
03:14
created

Item::getSiteLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Wikibase\DataModel\Entity;
4
5
use InvalidArgumentException;
6
use OutOfBoundsException;
7
use Wikibase\DataModel\SiteLink;
8
use Wikibase\DataModel\SiteLinkList;
9
use Wikibase\DataModel\Statement\StatementList;
10
use Wikibase\DataModel\Statement\StatementListHolder;
11
use Wikibase\DataModel\Term\AliasesProvider;
12
use Wikibase\DataModel\Term\AliasGroupList;
13
use Wikibase\DataModel\Term\DescriptionsProvider;
14
use Wikibase\DataModel\Term\Fingerprint;
15
use Wikibase\DataModel\Term\FingerprintProvider;
16
use Wikibase\DataModel\Term\LabelsProvider;
17
use Wikibase\DataModel\Term\TermList;
18
19
/**
20
 * Represents a single Wikibase item.
21
 * See https://www.mediawiki.org/wiki/Wikibase/DataModel#Items
22
 *
23
 * @since 0.1
24
 *
25
 * @license GPL-2.0+
26
 * @author Jeroen De Dauw < [email protected] >
27
 * @author Bene* < [email protected] >
28
 */
29
class Item implements EntityDocument, FingerprintProvider, StatementListHolder,
0 ignored issues
show
Deprecated Code introduced by
The interface Wikibase\DataModel\Statement\StatementListHolder has been deprecated with message: since 5.1, will be removed in favor of StatementListProvider, which gives the guarantee to return an object by reference. Changes to that object change the entity.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
30
	LabelsProvider, DescriptionsProvider, AliasesProvider {
31
32
	const ENTITY_TYPE = 'item';
33
34
	/**
35
	 * @var ItemId|null
36
	 */
37
	private $id;
38
39
	/**
40
	 * @var Fingerprint
41
	 */
42
	private $fingerprint;
43
44
	/**
45
	 * @var SiteLinkList
46
	 */
47
	private $siteLinks;
48
49
	/**
50
	 * @var StatementList
51
	 */
52
	private $statements;
53
54
	/**
55
	 * @since 1.0
56
	 *
57
	 * @param ItemId|null $id
58
	 * @param Fingerprint|null $fingerprint
59
	 * @param SiteLinkList|null $siteLinks
60
	 * @param StatementList|null $statements
61
	 */
62
	public function __construct(
63
		ItemId $id = null,
64
		Fingerprint $fingerprint = null,
65
		SiteLinkList $siteLinks = null,
66
		StatementList $statements = null
67
	) {
68
		$this->id = $id;
69
		$this->fingerprint = $fingerprint ?: new Fingerprint();
70
		$this->siteLinks = $siteLinks ?: new SiteLinkList();
71
		$this->statements = $statements ?: new StatementList();
72
	}
73
74
	/**
75
	 * Returns the id of the entity or null if it does not have one.
76
	 *
77
	 * @since 0.1 return type changed in 0.3
78
	 *
79
	 * @return ItemId|null
80
	 */
81
	public function getId() {
82
		return $this->id;
83
	}
84
85
	/**
86
	 * Can be integer since 0.1.
87
	 * Can be ItemId since 0.5.
88
	 * Can be null since 1.0.
89
	 *
90
	 * @param ItemId|int|null $id
91
	 *
92
	 * @throws InvalidArgumentException
93
	 */
94
	public function setId( $id ) {
95
		if ( $id === null || $id instanceof ItemId ) {
96
			$this->id = $id;
97
		} elseif ( is_int( $id ) ) {
98
			$this->id = ItemId::newFromNumber( $id );
99
		} else {
100
			throw new InvalidArgumentException( '$id must be an instance of ItemId, an integer,'
101
				. ' or null' );
102
		}
103
	}
104
105
	/**
106
	 * @since 0.7.3
107
	 *
108
	 * @return Fingerprint
109
	 */
110
	public function getFingerprint() {
111
		return $this->fingerprint;
112
	}
113
114
	/**
115
	 * @since 0.7.3
116
	 *
117
	 * @param Fingerprint $fingerprint
118
	 */
119
	public function setFingerprint( Fingerprint $fingerprint ) {
120
		$this->fingerprint = $fingerprint;
121
	}
122
123
	/**
124
	 * @see LabelsProvider::getLabels
125
	 *
126
	 * @since 6.0
127
	 *
128
	 * @return TermList
129
	 */
130
	public function getLabels() {
131
		return $this->fingerprint->getLabels();
132
	}
133
134
	/**
135
	 * @see DescriptionsProvider::getDescriptions
136
	 *
137
	 * @since 6.0
138
	 *
139
	 * @return TermList
140
	 */
141
	public function getDescriptions() {
142
		return $this->fingerprint->getDescriptions();
143
	}
144
145
	/**
146
	 * @see AliasesProvider::getAliasGroups
147
	 *
148
	 * @since 6.0
149
	 *
150
	 * @return AliasGroupList
151
	 */
152
	public function getAliasGroups() {
153
		return $this->fingerprint->getAliasGroups();
154
	}
155
156
	/**
157
	 * @param string $languageCode
158
	 * @param string $value
159
	 *
160
	 * @throws InvalidArgumentException
161
	 */
162
	public function setLabel( $languageCode, $value ) {
163
		$this->fingerprint->setLabel( $languageCode, $value );
164
	}
165
166
	/**
167
	 * @param string $languageCode
168
	 * @param string $value
169
	 *
170
	 * @throws InvalidArgumentException
171
	 */
172
	public function setDescription( $languageCode, $value ) {
173
		$this->fingerprint->setDescription( $languageCode, $value );
174
	}
175
176
	/**
177
	 * @param string $languageCode
178
	 * @param string[] $aliases
179
	 *
180
	 * @throws InvalidArgumentException
181
	 */
182
	public function setAliases( $languageCode, array $aliases ) {
183
		$this->fingerprint->setAliasGroup( $languageCode, $aliases );
184
	}
185
186
	/**
187
	 * @since 0.8
188
	 *
189
	 * @return SiteLinkList
190
	 */
191
	public function getSiteLinkList() {
192
		return $this->siteLinks;
193
	}
194
195
	/**
196
	 * @since 0.8
197
	 *
198
	 * @param SiteLinkList $siteLinks
199
	 */
200
	public function setSiteLinkList( SiteLinkList $siteLinks ) {
201
		$this->siteLinks = $siteLinks;
202
	}
203
204
	/**
205
	 * Adds a site link to the list of site links.
206
	 * If there already is a site link with the site id of the provided site link,
207
	 * then that one will be overridden by the provided one.
208
	 *
209
	 * @deprecated since 0.8, use getSiteLinkList()->addSiteLink() instead.
210
	 * @since 0.6
211
	 *
212
	 * @param SiteLink $siteLink
213
	 */
214
	public function addSiteLink( SiteLink $siteLink ) {
215
		if ( $this->siteLinks->hasLinkWithSiteId( $siteLink->getSiteId() ) ) {
216
			$this->siteLinks->removeLinkWithSiteId( $siteLink->getSiteId() );
217
		}
218
219
		$this->siteLinks->addSiteLink( $siteLink );
220
	}
221
222
	/**
223
	 * Removes the sitelink with specified site ID if the Item has such a sitelink.
224
	 *
225
	 * @deprecated since 0.8, use getSiteLinkList()->removeLinkWithSiteId() instead.
226
	 * @since 0.1
227
	 *
228
	 * @param string $siteId the target site's id
229
	 */
230
	public function removeSiteLink( $siteId ) {
231
		$this->siteLinks->removeLinkWithSiteId( $siteId );
232
	}
233
234
	/**
235
	 * @deprecated since 0.8, use getSiteLinkList()->getBySiteId() instead.
236
	 * @since 0.6
237
	 *
238
	 * @param string $siteId
239
	 *
240
	 * @return SiteLink
241
	 * @throws OutOfBoundsException
242
	 */
243
	public function getSiteLink( $siteId ) {
244
		return $this->siteLinks->getBySiteId( $siteId );
245
	}
246
247
	/**
248
	 * @deprecated since 0.8, use getSiteLinkList()->hasLinkWithSiteId() instead.
249
	 * @since 0.4
250
	 *
251
	 * @param string $siteId
252
	 *
253
	 * @return bool
254
	 */
255
	public function hasLinkToSite( $siteId ) {
256
		return $this->siteLinks->hasLinkWithSiteId( $siteId );
257
	}
258
259
	/**
260
	 * @deprecated since 0.8, use getSiteLinkList()->isEmpty() instead.
261
	 * @since 0.5
262
	 *
263
	 * @return bool
264
	 */
265
	public function hasSiteLinks() {
266
		return !$this->siteLinks->isEmpty();
267
	}
268
269
	/**
270
	 * @deprecated since 2.5, use new Item() instead.
271
	 *
272
	 * @return self
273
	 */
274
	public static function newEmpty() {
275
		return new self();
276
	}
277
278
	/**
279
	 * @see Entity::getType
280
	 *
281
	 * @since 0.1
282
	 *
283
	 * @return string Returns the entity type "item".
284
	 */
285
	public function getType() {
286
		return self::ENTITY_TYPE;
287
	}
288
289
	/**
290
	 * Returns if the Item has no content.
291
	 * Having an id set does not count as having content.
292
	 *
293
	 * @since 0.1
294
	 *
295
	 * @return bool
296
	 */
297
	public function isEmpty() {
298
		return $this->fingerprint->isEmpty()
299
			&& $this->statements->isEmpty()
300
			&& $this->siteLinks->isEmpty();
301
	}
302
303
	/**
304
	 * @since 1.0
305
	 *
306
	 * @return StatementList
307
	 */
308
	public function getStatements() {
309
		return $this->statements;
310
	}
311
312
	/**
313
	 * @since 1.0
314
	 *
315
	 * @param StatementList $statements
316
	 */
317
	public function setStatements( StatementList $statements ) {
318
		$this->statements = $statements;
319
	}
320
321
	/**
322
	 * @see EntityDocument::equals
323
	 *
324
	 * @since 0.1
325
	 *
326
	 * @param mixed $target
327
	 *
328
	 * @return bool
329
	 */
330
	public function equals( $target ) {
331
		if ( $this === $target ) {
332
			return true;
333
		}
334
335
		return $target instanceof self
336
			&& $this->fingerprint->equals( $target->fingerprint )
337
			&& $this->siteLinks->equals( $target->siteLinks )
338
			&& $this->statements->equals( $target->statements );
339
	}
340
341
	/**
342
	 * @see EntityDocument::copy
343
	 *
344
	 * @since 0.1
345
	 *
346
	 * @return self
347
	 */
348
	public function copy() {
349
		return clone $this;
350
	}
351
352
	/**
353
	 * @see http://php.net/manual/en/language.oop5.cloning.php
354
	 *
355
	 * @since 5.1
356
	 */
357
	public function __clone() {
358
		$this->fingerprint = clone $this->fingerprint;
359
		// SiteLinkList is mutable, but SiteLink is not. No deeper cloning necessary.
360
		$this->siteLinks = clone $this->siteLinks;
361
		$this->statements = clone $this->statements;
362
	}
363
364
}
365