Completed
Pull Request — master (#755)
by Leszek
04:25 queued 02:04
created

Item   B

Complexity

Total Complexity 37

Size/Duplication

Total Lines 324
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Test Coverage

Coverage 76.83%

Importance

Changes 0
Metric Value
wmc 37
lcom 2
cbo 4
dl 0
loc 324
ccs 63
cts 82
cp 0.7683
rs 8.6
c 0
b 0
f 0

25 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 4
A getId() 0 3 1
A removeSiteLink() 0 3 1
A setId() 0 8 3
A getFingerprint() 0 3 1
A setFingerprint() 0 3 1
A getLabels() 0 3 1
A getDescriptions() 0 3 1
A getAliasGroups() 0 3 1
A setLabel() 0 3 1
A setDescription() 0 3 1
A setAliases() 0 3 1
A getSiteLinkList() 0 3 1
A setSiteLinkList() 0 3 1
A addSiteLink() 0 7 2
A getSiteLink() 0 3 1
A hasLinkToSite() 0 3 1
A newEmpty() 0 3 1
A getType() 0 3 1
A isEmpty() 0 5 3
A getStatements() 0 3 1
A setStatements() 0 3 1
B equals() 0 10 5
A copy() 0 3 1
A __clone() 0 6 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+
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 81
	 *
57
	 * @param ItemId|null $id
58
	 * @param Fingerprint|null $fingerprint
59
	 * @param SiteLinkList|null $siteLinks
60
	 * @param StatementList|null $statements
61
	 */
62 81
	public function __construct(
63 81
		ItemId $id = null,
64 81
		Fingerprint $fingerprint = null,
65 81
		SiteLinkList $siteLinks = null,
66 81
		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 12
	 * Returns the id of the entity or null if it does not have one.
76 12
	 *
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 2
	 * Can be null since 1.0.
89 2
	 *
90 1
	 * @param ItemId|null $id
91 2
	 *
92 1
	 * @throws InvalidArgumentException
93 1
	 */
94
	public function setId( $id ) {
95
		if ( $id === null || $id instanceof ItemId ) {
96
			$this->id = $id;
97 2
		} else {
98
			throw new InvalidArgumentException( '$id must be an instance of ItemId, an integer,'
99
				. ' or null' );
100
		}
101
	}
102
103
	/**
104 58
	 * @since 0.7.3
105 58
	 *
106
	 * @return Fingerprint
107
	 */
108
	public function getFingerprint() {
109
		return $this->fingerprint;
110
	}
111
112
	/**
113 3
	 * @since 0.7.3
114 3
	 *
115 3
	 * @param Fingerprint $fingerprint
116
	 */
117
	public function setFingerprint( Fingerprint $fingerprint ) {
118
		$this->fingerprint = $fingerprint;
119
	}
120
121
	/**
122
	 * @see LabelsProvider::getLabels
123 12
	 *
124 12
	 * @since 6.0
125 12
	 *
126
	 * @return TermList
127
	 */
128
	public function getLabels() {
129
		return $this->fingerprint->getLabels();
130
	}
131
132
	/**
133 11
	 * @see DescriptionsProvider::getDescriptions
134 11
	 *
135 11
	 * @since 6.0
136
	 *
137
	 * @return TermList
138
	 */
139
	public function getDescriptions() {
140
		return $this->fingerprint->getDescriptions();
141
	}
142
143 31
	/**
144 31
	 * @see AliasesProvider::getAliasGroups
145 31
	 *
146
	 * @since 6.0
147
	 *
148
	 * @return AliasGroupList
149
	 */
150
	public function getAliasGroups() {
151
		return $this->fingerprint->getAliasGroups();
152 17
	}
153 17
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 1
	 * @param string $languageCode
176 1
	 * @param string[] $aliases
177 1
	 *
178 1
	 * @throws InvalidArgumentException
179
	 */
180 1
	public function setAliases( $languageCode, array $aliases ) {
181 1
		$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 4
202 4
	/**
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
	 * @deprecated since 0.8, use getSiteLinkList()->addSiteLink() instead.
208
	 * @since 0.6
209
	 *
210
	 * @param SiteLink $siteLink
211
	 */
212
	public function addSiteLink( SiteLink $siteLink ) {
213
		if ( $this->siteLinks->hasLinkWithSiteId( $siteLink->getSiteId() ) ) {
214
			$this->siteLinks->removeLinkWithSiteId( $siteLink->getSiteId() );
215
		}
216
217
		$this->siteLinks->addSiteLink( $siteLink );
218
	}
219
220
	/**
221
	 * Removes the sitelink with specified site ID if the Item has such a sitelink.
222
	 *
223
	 * @deprecated since 0.8, use getSiteLinkList()->removeLinkWithSiteId() instead.
224
	 * @since 0.1
225
	 *
226
	 * @param string $siteId the target site's id
227
	 */
228
	public function removeSiteLink( $siteId ) {
229
		$this->siteLinks->removeLinkWithSiteId( $siteId );
230
	}
231
232
	/**
233
	 * @deprecated since 0.8, use getSiteLinkList()->getBySiteId() instead.
234
	 * @since 0.6
235
	 *
236
	 * @param string $siteId
237
	 *
238
	 * @return SiteLink
239
	 * @throws OutOfBoundsException
240
	 */
241
	public function getSiteLink( $siteId ) {
242
		return $this->siteLinks->getBySiteId( $siteId );
243
	}
244
245
	/**
246
	 * @deprecated since 0.8, use getSiteLinkList()->hasLinkWithSiteId() instead.
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