Passed
Push — currentLimits ( 193e29...261cca )
by no
06:29 queued 02:03
created

Item::__construct()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.2
cc 4
eloc 9
nc 8
nop 4
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|null $id
91
	 *
92
	 * @throws InvalidArgumentException
93
	 */
94
	public function setId( $id ) {
95
		if ( $id === null || $id instanceof ItemId ) {
96
			$this->id = $id;
97
		} else {
98
			throw new InvalidArgumentException( '$id must be an instance of ItemId, an integer,'
99
				. ' or null' );
100
		}
101
	}
102
103
	/**
104
	 * @since 0.7.3
105
	 *
106
	 * @return Fingerprint
107
	 */
108
	public function getFingerprint() {
109
		return $this->fingerprint;
110
	}
111
112
	/**
113
	 * @since 0.7.3
114
	 *
115
	 * @param Fingerprint $fingerprint
116
	 */
117
	public function setFingerprint( Fingerprint $fingerprint ) {
118
		$this->fingerprint = $fingerprint;
119
	}
120
121
	/**
122
	 * @see LabelsProvider::getLabels
123
	 *
124
	 * @since 6.0
125
	 *
126
	 * @return TermList
127
	 */
128
	public function getLabels() {
129
		return $this->fingerprint->getLabels();
130
	}
131
132
	/**
133
	 * @see DescriptionsProvider::getDescriptions
134
	 *
135
	 * @since 6.0
136
	 *
137
	 * @return TermList
138
	 */
139
	public function getDescriptions() {
140
		return $this->fingerprint->getDescriptions();
141
	}
142
143
	/**
144
	 * @see AliasesProvider::getAliasGroups
145
	 *
146
	 * @since 6.0
147
	 *
148
	 * @return AliasGroupList
149
	 */
150
	public function getAliasGroups() {
151
		return $this->fingerprint->getAliasGroups();
152
	}
153
154
	/**
155
	 * @param string $languageCode
156
	 * @param string $value
157
	 *
158
	 * @throws InvalidArgumentException
159
	 */
160
	public function setLabel( $languageCode, $value ) {
161
		$this->fingerprint->setLabel( $languageCode, $value );
162
	}
163
164
	/**
165
	 * @param string $languageCode
166
	 * @param string $value
167
	 *
168
	 * @throws InvalidArgumentException
169
	 */
170
	public function setDescription( $languageCode, $value ) {
171
		$this->fingerprint->setDescription( $languageCode, $value );
172
	}
173
174
	/**
175
	 * @param string $languageCode
176
	 * @param string[] $aliases
177
	 *
178
	 * @throws InvalidArgumentException
179
	 */
180
	public function setAliases( $languageCode, array $aliases ) {
181
		$this->fingerprint->setAliasGroup( $languageCode, $aliases );
182
	}
183
184
	/**
185
	 * @since 0.8
186
	 *
187
	 * @return SiteLinkList
188
	 */
189
	public function getSiteLinkList() {
190
		return $this->siteLinks;
191
	}
192
193
	/**
194
	 * @since 0.8
195
	 *
196
	 * @param SiteLinkList $siteLinks
197
	 */
198
	public function setSiteLinkList( SiteLinkList $siteLinks ) {
199
		$this->siteLinks = $siteLinks;
200
	}
201
202
	/**
203
	 * Adds a site link to the list of site links.
204
	 * If there already is a site link with the site id of the provided site link,
205
	 * then that one will be overridden by the provided one.
206
	 *
207
	 * @since 0.6
208
	 *
209
	 * @param SiteLink $siteLink
210
	 */
211
	public function addSiteLink( SiteLink $siteLink ) {
212
		if ( $this->siteLinks->hasLinkWithSiteId( $siteLink->getSiteId() ) ) {
213
			$this->siteLinks->removeLinkWithSiteId( $siteLink->getSiteId() );
214
		}
215
216
		$this->siteLinks->addSiteLink( $siteLink );
217
	}
218
219
	/**
220
	 * Removes the sitelink with specified site ID if the Item has such a sitelink.
221
	 *
222
	 * @since 0.1
223
	 *
224
	 * @param string $siteId the target site's id
225
	 */
226
	public function removeSiteLink( $siteId ) {
227
		$this->siteLinks->removeLinkWithSiteId( $siteId );
228
	}
229
230
	/**
231
	 * @since 0.6
232
	 *
233
	 * @param string $siteId
234
	 *
235
	 * @return SiteLink
236
	 * @throws OutOfBoundsException
237
	 */
238
	public function getSiteLink( $siteId ) {
239
		return $this->siteLinks->getBySiteId( $siteId );
240
	}
241
242
	/**
243
	 * @since 0.4
244
	 *
245
	 * @param string $siteId
246
	 *
247
	 * @return bool
248
	 */
249
	public function hasLinkToSite( $siteId ) {
250
		return $this->siteLinks->hasLinkWithSiteId( $siteId );
251
	}
252
253
	/**
254
	 * @deprecated since 2.5, use new Item() instead.
255
	 *
256
	 * @return self
257
	 */
258
	public static function newEmpty() {
259
		return new self();
260
	}
261
262
	/**
263
	 * @see Entity::getType
264
	 *
265
	 * @since 0.1
266
	 *
267
	 * @return string Returns the entity type "item".
268
	 */
269
	public function getType() {
270
		return self::ENTITY_TYPE;
271
	}
272
273
	/**
274
	 * Returns if the Item has no content.
275
	 * Having an id set does not count as having content.
276
	 *
277
	 * @since 0.1
278
	 *
279
	 * @return bool
280
	 */
281
	public function isEmpty() {
282
		return $this->fingerprint->isEmpty()
283
			&& $this->statements->isEmpty()
284
			&& $this->siteLinks->isEmpty();
285
	}
286
287
	/**
288
	 * @since 1.0
289
	 *
290
	 * @return StatementList
291
	 */
292
	public function getStatements() {
293
		return $this->statements;
294
	}
295
296
	/**
297
	 * @since 1.0
298
	 *
299
	 * @param StatementList $statements
300
	 */
301
	public function setStatements( StatementList $statements ) {
302
		$this->statements = $statements;
303
	}
304
305
	/**
306
	 * @see EntityDocument::equals
307
	 *
308
	 * @since 0.1
309
	 *
310
	 * @param mixed $target
311
	 *
312
	 * @return bool
313
	 */
314
	public function equals( $target ) {
315
		if ( $this === $target ) {
316
			return true;
317
		}
318
319
		return $target instanceof self
320
			&& $this->fingerprint->equals( $target->fingerprint )
321
			&& $this->siteLinks->equals( $target->siteLinks )
322
			&& $this->statements->equals( $target->statements );
323
	}
324
325
	/**
326
	 * @see EntityDocument::copy
327
	 *
328
	 * @since 0.1
329
	 *
330
	 * @return self
331
	 */
332
	public function copy() {
333
		return clone $this;
334
	}
335
336
	/**
337
	 * @see http://php.net/manual/en/language.oop5.cloning.php
338
	 *
339
	 * @since 5.1
340
	 */
341
	public function __clone() {
342
		$this->fingerprint = clone $this->fingerprint;
343
		// SiteLinkList is mutable, but SiteLink is not. No deeper cloning necessary.
344
		$this->siteLinks = clone $this->siteLinks;
345
		$this->statements = clone $this->statements;
346
	}
347
348
}
349