Completed
Push — entity-clear ( afee34...e01dde )
by Bene
03:31
created

Item::getStatements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
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\Fingerprint;
12
13
/**
14
 * Represents a single Wikibase item.
15
 * See https://www.mediawiki.org/wiki/Wikibase/DataModel#Items
16
 *
17
 * @since 0.1
18
 *
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 */
22
class Item extends Entity implements StatementListHolder {
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\Entity\Entity has been deprecated with message: since 1.0 - do not type hint against Entity. See
https://lists.wikimedia.org/pipermail/wikidata-tech/2014-June/000489.html

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...
23
24
	const ENTITY_TYPE = 'item';
25
26
	/**
27
	 * @var ItemId|null
28
	 */
29
	private $id;
30
31
	/**
32
	 * @var Fingerprint
33
	 */
34
	private $fingerprint;
35
36
	/**
37
	 * @var SiteLinkList
38
	 */
39
	private $siteLinks;
40
41
	/**
42
	 * @var StatementList
43
	 */
44
	private $statements;
45
46
	/**
47
	 * @since 1.0
48
	 *
49
	 * @param ItemId|null $id
50
	 * @param Fingerprint|null $fingerprint
51
	 * @param SiteLinkList|null $siteLinks
52
	 * @param StatementList|null $statements
53
	 */
54
	public function __construct(
55
		ItemId $id = null,
56
		Fingerprint $fingerprint = null,
57
		SiteLinkList $siteLinks = null,
58
		StatementList $statements = null
59
	) {
60
		$this->id = $id;
61
		$this->fingerprint = $fingerprint ?: new Fingerprint();
62
		$this->siteLinks = $siteLinks ?: new SiteLinkList();
63
		$this->statements = $statements ?: new StatementList();
64
	}
65
66
	/**
67
	 * Returns the id of the entity or null if it does not have one.
68
	 *
69
	 * @since 0.1 return type changed in 0.3
70
	 *
71
	 * @return ItemId|null
72
	 */
73
	public function getId() {
74
		return $this->id;
75
	}
76
77
	/**
78
	 * Can be integer since 0.1.
79
	 * Can be ItemId since 0.5.
80
	 * Can be null since 1.0.
81
	 *
82
	 * @param ItemId|int|null $id
83
	 *
84
	 * @throws InvalidArgumentException
85
	 */
86
	public function setId( $id ) {
87
		if ( $id === null || $id instanceof ItemId ) {
88
			$this->id = $id;
89
		} elseif ( is_int( $id ) ) {
90
			$this->id = ItemId::newFromNumber( $id );
91
		} else {
92
			throw new InvalidArgumentException( '$id must be an instance of ItemId, an integer,'
93
				. ' or null' );
94
		}
95
	}
96
97
	/**
98
	 * @since 0.7.3
99
	 *
100
	 * @return Fingerprint
101
	 */
102
	public function getFingerprint() {
103
		return $this->fingerprint;
104
	}
105
106
	/**
107
	 * @since 0.7.3
108
	 *
109
	 * @param Fingerprint $fingerprint
110
	 */
111
	public function setFingerprint( Fingerprint $fingerprint ) {
112
		$this->fingerprint = $fingerprint;
113
	}
114
115
	/**
116
	 * @param string $languageCode
117
	 * @param string $value
118
	 *
119
	 * @throws InvalidArgumentException
120
	 */
121
	public function setLabel( $languageCode, $value ) {
122
		$this->fingerprint->setLabel( $languageCode, $value );
123
	}
124
125
	/**
126
	 * @param string $languageCode
127
	 * @param string $value
128
	 *
129
	 * @throws InvalidArgumentException
130
	 */
131
	public function setDescription( $languageCode, $value ) {
132
		$this->fingerprint->setDescription( $languageCode, $value );
133
	}
134
135
	/**
136
	 * @param string $languageCode
137
	 * @param string[] $aliases
138
	 *
139
	 * @throws InvalidArgumentException
140
	 */
141
	public function setAliases( $languageCode, array $aliases ) {
142
		$this->fingerprint->setAliasGroup( $languageCode, $aliases );
143
	}
144
145
	/**
146
	 * @since 0.8
147
	 *
148
	 * @return SiteLinkList
149
	 */
150
	public function getSiteLinkList() {
151
		return $this->siteLinks;
152
	}
153
154
	/**
155
	 * @since 0.8
156
	 *
157
	 * @param SiteLinkList $siteLinks
158
	 */
159
	public function setSiteLinkList( SiteLinkList $siteLinks ) {
160
		$this->siteLinks = $siteLinks;
161
	}
162
163
	/**
164
	 * Adds a site link to the list of site links.
165
	 * If there already is a site link with the site id of the provided site link,
166
	 * then that one will be overridden by the provided one.
167
	 *
168
	 * @deprecated since 0.8, use getSiteLinkList()->addSiteLink() instead.
169
	 * @since 0.6
170
	 *
171
	 * @param SiteLink $siteLink
172
	 */
173
	public function addSiteLink( SiteLink $siteLink ) {
174
		if ( $this->siteLinks->hasLinkWithSiteId( $siteLink->getSiteId() ) ) {
175
			$this->siteLinks->removeLinkWithSiteId( $siteLink->getSiteId() );
176
		}
177
178
		$this->siteLinks->addSiteLink( $siteLink );
179
	}
180
181
	/**
182
	 * Removes the sitelink with specified site ID if the Item has such a sitelink.
183
	 *
184
	 * @deprecated since 0.8, use getSiteLinkList()->removeLinkWithSiteId() instead.
185
	 * @since 0.1
186
	 *
187
	 * @param string $siteId the target site's id
188
	 */
189
	public function removeSiteLink( $siteId ) {
190
		$this->siteLinks->removeLinkWithSiteId( $siteId );
191
	}
192
193
	/**
194
	 * @deprecated since 0.8, use getSiteLinkList() instead,
195
	 * @since 0.6
196
	 *
197
	 * @return SiteLink[]
198
	 */
199
	public function getSiteLinks() {
200
		return array_values( iterator_to_array( $this->siteLinks ) );
201
	}
202
203
	/**
204
	 * @deprecated since 0.8, use getSiteLinkList()->getBySiteId() instead.
205
	 * @since 0.6
206
	 *
207
	 * @param string $siteId
208
	 *
209
	 * @return SiteLink
210
	 * @throws OutOfBoundsException
211
	 */
212
	public function getSiteLink( $siteId ) {
213
		return $this->siteLinks->getBySiteId( $siteId );
214
	}
215
216
	/**
217
	 * @deprecated since 0.8, use getSiteLinkList()->hasLinkWithSiteId() instead.
218
	 * @since 0.4
219
	 *
220
	 * @param string $siteId
221
	 *
222
	 * @return bool
223
	 */
224
	public function hasLinkToSite( $siteId ) {
225
		return $this->siteLinks->hasLinkWithSiteId( $siteId );
226
	}
227
228
	/**
229
	 * @deprecated since 0.8, use getSiteLinkList()->isEmpty() instead.
230
	 * @since 0.5
231
	 *
232
	 * @return bool
233
	 */
234
	public function hasSiteLinks() {
235
		return !$this->siteLinks->isEmpty();
236
	}
237
238
	/**
239
	 * @deprecated since 2.5, use new Item() instead.
240
	 *
241
	 * @return Item
242
	 */
243
	public static function newEmpty() {
244
		return new self();
245
	}
246
247
	/**
248
	 * @see Entity::getType
249
	 *
250
	 * @since 0.1
251
	 *
252
	 * @return string Returns the entity type "item".
253
	 */
254
	public function getType() {
255
		return self::ENTITY_TYPE;
256
	}
257
258
	/**
259
	 * Returns if the Item has no content.
260
	 * Having an id set does not count as having content.
261
	 *
262
	 * @since 0.1
263
	 *
264
	 * @return bool
265
	 */
266
	public function isEmpty() {
267
		return $this->fingerprint->isEmpty()
268
			&& $this->statements->isEmpty()
269
			&& $this->siteLinks->isEmpty();
270
	}
271
272
	/**
273
	 * @since 1.0
274
	 *
275
	 * @return StatementList
276
	 */
277
	public function getStatements() {
278
		return $this->statements;
279
	}
280
281
	/**
282
	 * @since 1.0
283
	 *
284
	 * @param StatementList $statements
285
	 */
286
	public function setStatements( StatementList $statements ) {
287
		$this->statements = $statements;
288
	}
289
290
	/**
291
	 * @see Comparable::equals
292
	 *
293
	 * Two items are considered equal if they are of the same
294
	 * type and have the same value. The value does not include
295
	 * the id, so entities with the same value but different id
296
	 * are considered equal.
297
	 *
298
	 * @since 0.1
299
	 *
300
	 * @param mixed $target
301
	 *
302
	 * @return bool
303
	 */
304
	public function equals( $target ) {
305
		if ( $this === $target ) {
306
			return true;
307
		}
308
309
		return $target instanceof self
310
			&& $this->fingerprint->equals( $target->fingerprint )
311
			&& $this->siteLinks->equals( $target->siteLinks )
312
			&& $this->statements->equals( $target->statements );
313
	}
314
315
}
316