Completed
Pull Request — master (#631)
by Bene
79:32 queued 76:44
created

Item::copy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
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\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 {
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
	 * Removes all content from the Item.
275
	 * The id is not part of the content.
276
	 *
277
	 * @since 0.1
278
	 */
279
	public function clear() {
280 2
		$this->fingerprint = new Fingerprint();
281 2
		$this->siteLinks = new SiteLinkList();
282 2
		$this->statements = new StatementList();
283 2
	}
284 2
285
	/**
286
	 * @since 1.0
287
	 *
288
	 * @return StatementList
289
	 */
290
	public function getStatements() {
291 7
		return $this->statements;
292 7
	}
293
294
	/**
295
	 * @since 1.0
296
	 *
297
	 * @param StatementList $statements
298
	 */
299
	public function setStatements( StatementList $statements ) {
300 1
		$this->statements = $statements;
301 1
	}
302 1
303
	/**
304
	 * @see EntityDocument::equals
305
	 *
306
	 * @since 0.1
307
	 *
308
	 * @param mixed $target
309 4
	 *
310 4
	 * @return bool
311
	 */
312
	public function equals( $target ) {
313
		if ( $this === $target ) {
314
			return true;
315
		}
316
317
		return $target instanceof self
318 1
			&& $this->fingerprint->equals( $target->fingerprint )
319 1
			&& $this->siteLinks->equals( $target->siteLinks )
320 1
			&& $this->statements->equals( $target->statements );
321
	}
322
323
	/**
324
	 * @see EntityDocument::copy
325
	 *
326
	 * @since 0.1
327
	 *
328
	 * @return self
329
	 */
330
	public function copy() {
331
		return unserialize( serialize( $this ) );
332
	}
333
334
}
335