Completed
Pull Request — master (#632)
by Bene
06:30 queued 03:23
created

Property::getFingerprint()   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
Metric Value
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 Wikibase\DataModel\Statement\StatementList;
7
use Wikibase\DataModel\Statement\StatementListHolder;
8
use Wikibase\DataModel\Term\AliasesProvider;
9
use Wikibase\DataModel\Term\AliasGroupList;
10
use Wikibase\DataModel\Term\DescriptionsProvider;
11
use Wikibase\DataModel\Term\Fingerprint;
12
use Wikibase\DataModel\Term\FingerprintHolder;
13
use Wikibase\DataModel\Term\LabelsProvider;
14
use Wikibase\DataModel\Term\TermList;
15
16
/**
17
 * Represents a single Wikibase property.
18
 * See https://www.mediawiki.org/wiki/Wikibase/DataModel#Properties
19
 *
20
 * @since 0.1
21
 *
22
 * @licence GNU GPL v2+
23
 * @author Jeroen De Dauw < [email protected] >
24
 * @author Bene* < [email protected] >
25
 */
26
class Property implements EntityDocument, FingerprintHolder, StatementListHolder,
0 ignored issues
show
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...
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...
27
	LabelsProvider, DescriptionsProvider, AliasesProvider {
28
29
	const ENTITY_TYPE = 'property';
30
31
	/**
32
	 * @var PropertyId|null
33
	 */
34
	private $id;
35
36
	/**
37
	 * @var Fingerprint
38
	 */
39
	private $fingerprint;
40
41
	/**
42
	 * @var string The data type of the property.
43
	 */
44
	private $dataTypeId;
45
46
	/**
47
	 * @var StatementList
48
	 */
49
	private $statements;
50
51
	/**
52
	 * @since 1.0
53 69
	 *
54
	 * @param PropertyId|null $id
55
	 * @param Fingerprint|null $fingerprint
56
	 * @param string $dataTypeId The data type of the property. Not to be confused with the data
57
	 *  value type.
58
	 * @param StatementList|null $statements Since 1.1
59 69
	 */
60 69
	public function __construct(
61 69
		PropertyId $id = null,
62 68
		Fingerprint $fingerprint = null,
63 68
		$dataTypeId,
64
		StatementList $statements = null
65
	) {
66
		$this->id = $id;
67
		$this->fingerprint = $fingerprint ?: new Fingerprint();
68
		$this->setDataTypeId( $dataTypeId );
69
		$this->statements = $statements ?: new StatementList();
70
	}
71
72 13
	/**
73 13
	 * Returns the id of the entity or null if it does not have one.
74
	 *
75
	 * @since 0.1 return type changed in 0.3
76
	 *
77
	 * @return PropertyId|null
78
	 */
79
	public function getId() {
80
		return $this->id;
81
	}
82
83
	/**
84
	 * Can be integer since 0.1.
85 4
	 * Can be PropertyId since 0.5.
86 4
	 * Can be null since 1.0.
87 1
	 *
88 4
	 * @param PropertyId|int|null $id
89 3
	 *
90 3
	 * @throws InvalidArgumentException
91
	 */
92
	public function setId( $id ) {
93
		if ( $id === null || $id instanceof PropertyId ) {
94 4
			$this->id = $id;
95
		} elseif ( is_int( $id ) ) {
96
			$this->id = PropertyId::newFromNumber( $id );
97
		} else {
98
			throw new InvalidArgumentException( '$id must be an instance of PropertyId, an integer,'
99
				. ' or null' );
100
		}
101 58
	}
102 58
103
	/**
104
	 * @since 0.7.3
105
	 *
106
	 * @return Fingerprint
107
	 */
108
	public function getFingerprint() {
109
		return $this->fingerprint;
110 3
	}
111 3
112 3
	/**
113
	 * @since 0.7.3
114
	 *
115
	 * @param Fingerprint $fingerprint
116
	 */
117
	public function setFingerprint( Fingerprint $fingerprint ) {
118
		$this->fingerprint = $fingerprint;
119
	}
120 12
121 12
	/**
122 12
	 * @see LabelsProvider::getLabels
123
	 *
124
	 * @since 6.0
125
	 *
126
	 * @return TermList
127
	 */
128
	public function getLabels() {
129
		return $this->fingerprint->getLabels();
130 11
	}
131 11
132 11
	/**
133
	 * @see DescriptionsProvider::getDescriptions
134
	 *
135
	 * @since 6.0
136
	 *
137
	 * @return TermList
138
	 */
139
	public function getDescriptions() {
140 31
		return $this->fingerprint->getDescriptions();
141 31
	}
142 31
143
	/**
144
	 * @see AliasesProvider::getAliasGroups
145
	 *
146
	 * @since 6.0
147
	 *
148
	 * @return AliasGroupList
149
	 */
150
	public function getAliasGroups() {
151 69
		return $this->fingerprint->getAliasGroups();
152 69
	}
153 1
154
	/**
155
	 * @param string $languageCode
156 68
	 * @param string $value
157 68
	 *
158
	 * @throws InvalidArgumentException
159
	 */
160
	public function setLabel( $languageCode, $value ) {
161
		$this->fingerprint->setLabel( $languageCode, $value );
162
	}
163
164 4
	/**
165 4
	 * @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
	 * @param string $languageCode
176
	 * @param string[] $aliases
177
	 *
178
	 * @throws InvalidArgumentException
179
	 */
180
	public function setAliases( $languageCode, array $aliases ) {
181
		$this->fingerprint->setAliasGroup( $languageCode, $aliases );
182
	}
183
184
	/**
185
	 * @since 0.4
186 66
	 *
187 66
	 * @param string $dataTypeId The data type of the property. Not to be confused with the data
188
	 *  value type.
189
	 *
190
	 * @throws InvalidArgumentException
191
	 */
192
	public function setDataTypeId( $dataTypeId ) {
193
		if ( !is_string( $dataTypeId ) ) {
194
			throw new InvalidArgumentException( '$dataTypeId must be a string' );
195
		}
196
197
		$this->dataTypeId = $dataTypeId;
198
	}
199
200
	/**
201
	 * @since 0.4
202
	 *
203
	 * @return string Returns the data type of the property (property type). Not to be confused with
204 18
	 *  the data value type.
205 18
	 */
206
	public function getDataTypeId() {
207
		return $this->dataTypeId;
208
	}
209
210 18
	/**
211 18
	 * @see Entity::getType
212 18
	 *
213
	 * @since 0.1
214
	 *
215
	 * @return string Returns the entity type "property".
216
	 */
217
	public function getType() {
218
		return self::ENTITY_TYPE;
219
	}
220
221
	/**
222
	 * @since 0.3
223 4
	 *
224 4
	 * @param string $dataTypeId The data type of the property. Not to be confused with the data
225 4
	 *  value type.
226
	 *
227
	 * @return self
228
	 */
229
	public static function newFromType( $dataTypeId ) {
230
		return new self( null, null, $dataTypeId );
231
	}
232
233
	/**
234 1
	 * @see EntityDocument::equals
235 1
	 *
236 1
	 * @since 0.1
237
	 *
238
	 * @param mixed $target
239
	 *
240
	 * @return bool
241
	 */
242
	public function equals( $target ) {
243 5
		if ( $this === $target ) {
244 5
			return true;
245
		}
246
247
		return $target instanceof self
248
			&& $this->dataTypeId === $target->dataTypeId
249
			&& $this->fingerprint->equals( $target->fingerprint )
250
			&& $this->statements->equals( $target->statements );
251
	}
252 2
253 2
	/**
254 2
	 * Returns if the Property has no content.
255
	 * Having an id and type set does not count as having content.
256
	 *
257
	 * @since 0.1
258
	 *
259
	 * @return bool
260
	 */
261 4
	public function isEmpty() {
262 4
		return $this->fingerprint->isEmpty()
263
			&& $this->statements->isEmpty();
264
	}
265
266
	/**
267
	 * Removes all content from the Property.
268
	 * The id and the type are not part of the content.
269
	 *
270 1
	 * @since 0.1
271 1
	 */
272 1
	public function clear() {
273
		$this->fingerprint = new Fingerprint();
274
		$this->statements = new StatementList();
275
	}
276
277
	/**
278
	 * @since 1.1
279
	 *
280
	 * @return StatementList
281
	 */
282
	public function getStatements() {
283
		return $this->statements;
284
	}
285
286
	/**
287
	 * @since 1.1
288
	 *
289
	 * @param StatementList $statements
290
	 */
291
	public function setStatements( StatementList $statements ) {
292
		$this->statements = $statements;
293
	}
294
295
	/**
296
	 * @see EntityDocument::copy
297
	 *
298
	 * @since 0.1
299
	 *
300
	 * @return self
301
	 */
302
	public function copy() {
303
		return clone $this;
304
	}
305
306
	/**
307
	 * @see http://php.net/manual/en/language.oop5.cloning.php
308
	 *
309
	 * @since 5.1
310
	 */
311
	public function __clone() {
312
		$this->fingerprint = clone $this->fingerprint;
313
		$this->statements = clone $this->statements;
314
	}
315
316
}
317