Completed
Pull Request — master (#614)
by Bene
05:58 queued 03:03
created

Item::getStatements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

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