Passed
Push — revert-interface-change ( ccb51e )
by Jakob
06:40 queued 34s
created

Item::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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