Passed
Push — revert-interface-change ( ccb51e...f9a91e )
by Jakob
06:03 queued 01:30
created

Item::getSiteLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
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() instead,
236
	 * @since 0.6
237
	 *
238
	 * @return SiteLink[]
239
	 */
240
	public function getSiteLinks() {
241
		return array_values( iterator_to_array( $this->siteLinks ) );
242
	}
243
244
	/**
245
	 * @deprecated since 0.8, use getSiteLinkList()->getBySiteId() instead.
246
	 * @since 0.6
247
	 *
248
	 * @param string $siteId
249
	 *
250
	 * @return SiteLink
251
	 * @throws OutOfBoundsException
252
	 */
253
	public function getSiteLink( $siteId ) {
254
		return $this->siteLinks->getBySiteId( $siteId );
255
	}
256
257
	/**
258
	 * @deprecated since 0.8, use getSiteLinkList()->hasLinkWithSiteId() instead.
259
	 * @since 0.4
260
	 *
261
	 * @param string $siteId
262
	 *
263
	 * @return bool
264
	 */
265
	public function hasLinkToSite( $siteId ) {
266
		return $this->siteLinks->hasLinkWithSiteId( $siteId );
267
	}
268
269
	/**
270
	 * @deprecated since 0.8, use getSiteLinkList()->isEmpty() instead.
271
	 * @since 0.5
272
	 *
273
	 * @return bool
274
	 */
275
	public function hasSiteLinks() {
276
		return !$this->siteLinks->isEmpty();
277
	}
278
279
	/**
280
	 * @deprecated since 2.5, use new Item() instead.
281
	 *
282
	 * @return self
283
	 */
284
	public static function newEmpty() {
285
		return new self();
286
	}
287
288
	/**
289
	 * @see Entity::getType
290
	 *
291
	 * @since 0.1
292
	 *
293
	 * @return string Returns the entity type "item".
294
	 */
295
	public function getType() {
296
		return self::ENTITY_TYPE;
297
	}
298
299
	/**
300
	 * Returns if the Item has no content.
301
	 * Having an id set does not count as having content.
302
	 *
303
	 * @since 0.1
304
	 *
305
	 * @return bool
306
	 */
307
	public function isEmpty() {
308
		return $this->fingerprint->isEmpty()
309
			&& $this->statements->isEmpty()
310
			&& $this->siteLinks->isEmpty();
311
	}
312
313
	/**
314
	 * @since 1.0
315
	 *
316
	 * @return StatementList
317
	 */
318
	public function getStatements() {
319
		return $this->statements;
320
	}
321
322
	/**
323
	 * @since 1.0
324
	 *
325
	 * @param StatementList $statements
326
	 */
327
	public function setStatements( StatementList $statements ) {
328
		$this->statements = $statements;
329
	}
330
331
	/**
332
	 * @see EntityDocument::equals
333
	 *
334
	 * @since 0.1
335
	 *
336
	 * @param mixed $target
337
	 *
338
	 * @return bool
339
	 */
340
	public function equals( $target ) {
341
		if ( $this === $target ) {
342
			return true;
343
		}
344
345
		return $target instanceof self
346
			&& $this->fingerprint->equals( $target->fingerprint )
347
			&& $this->siteLinks->equals( $target->siteLinks )
348
			&& $this->statements->equals( $target->statements );
349
	}
350
351
	/**
352
	 * @see EntityDocument::copy
353
	 *
354
	 * @since 0.1
355
	 *
356
	 * @return self
357
	 */
358
	public function copy() {
359
		return clone $this;
360
	}
361
362
	/**
363
	 * @see http://php.net/manual/en/language.oop5.cloning.php
364
	 *
365
	 * @since 5.1
366
	 */
367
	public function __clone() {
368
		$this->fingerprint = clone $this->fingerprint;
369
		// SiteLinkList is mutable, but SiteLink is not. No deeper cloning necessary.
370
		$this->siteLinks = clone $this->siteLinks;
371
		$this->statements = clone $this->statements;
372
	}
373
374
}
375