Item::setSiteLinkList()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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