Completed
Push — 7.x ( b1de80...30e315 )
by adam
19:03 queued 16:07
created

Property::getDataTypeId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
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\FingerprintProvider;
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
 * @license GPL-2.0+
23
 * @author Jeroen De Dauw < [email protected] >
24
 * @author Bene* < [email protected] >
25
 */
26
class Property 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...
27
	LabelsProvider, DescriptionsProvider, AliasesProvider, ClearableEntity {
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
	 *
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
	 */
60
	public function __construct(
61
		PropertyId $id = null,
62
		Fingerprint $fingerprint = null,
63
		$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
	/**
73
	 * 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
	 * Can be PropertyId since 0.5.
86
	 * Can be null since 1.0.
87
	 *
88
	 * @param PropertyId|int|null $id
89
	 *
90
	 * @throws InvalidArgumentException
91
	 */
92
	public function setId( $id ) {
93
		if ( $id === null || $id instanceof PropertyId ) {
94
			$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
	}
102
103
	/**
104
	 * @since 0.7.3
105
	 *
106
	 * @return Fingerprint
107
	 */
108
	public function getFingerprint() {
109
		return $this->fingerprint;
110
	}
111
112
	/**
113
	 * @since 0.7.3
114
	 *
115
	 * @param Fingerprint $fingerprint
116
	 */
117
	public function setFingerprint( Fingerprint $fingerprint ) {
118
		$this->fingerprint = $fingerprint;
119
	}
120
121
	/**
122
	 * @see LabelsProvider::getLabels
123
	 *
124
	 * @since 6.0
125
	 *
126
	 * @return TermList
127
	 */
128
	public function getLabels() {
129
		return $this->fingerprint->getLabels();
130
	}
131
132
	/**
133
	 * @see DescriptionsProvider::getDescriptions
134
	 *
135
	 * @since 6.0
136
	 *
137
	 * @return TermList
138
	 */
139
	public function getDescriptions() {
140
		return $this->fingerprint->getDescriptions();
141
	}
142
143
	/**
144
	 * @see AliasesProvider::getAliasGroups
145
	 *
146
	 * @since 6.0
147
	 *
148
	 * @return AliasGroupList
149
	 */
150
	public function getAliasGroups() {
151
		return $this->fingerprint->getAliasGroups();
152
	}
153
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
	 * @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
	 *
187
	 * @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
	 *  the data value type.
205
	 */
206
	public function getDataTypeId() {
207
		return $this->dataTypeId;
208
	}
209
210
	/**
211
	 * @see Entity::getType
212
	 *
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
	 *
224
	 * @param string $dataTypeId The data type of the property. Not to be confused with the data
225
	 *  value type.
226
	 *
227
	 * @return self
228
	 */
229
	public static function newFromType( $dataTypeId ) {
230
		return new self( null, null, $dataTypeId );
231
	}
232
233
	/**
234
	 * @see EntityDocument::equals
235
	 *
236
	 * @since 0.1
237
	 *
238
	 * @param mixed $target
239
	 *
240
	 * @return bool
241
	 */
242
	public function equals( $target ) {
243
		if ( $this === $target ) {
244
			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
253
	/**
254
	 * 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
	public function isEmpty() {
262
		return $this->fingerprint->isEmpty()
263
			&& $this->statements->isEmpty();
264
	}
265
266
	/**
267
	 * @since 1.1
268
	 *
269
	 * @return StatementList
270
	 */
271
	public function getStatements() {
272
		return $this->statements;
273
	}
274
275
	/**
276
	 * @since 1.1
277
	 *
278
	 * @param StatementList $statements
279
	 */
280
	public function setStatements( StatementList $statements ) {
281
		$this->statements = $statements;
282
	}
283
284
	/**
285
	 * @see EntityDocument::copy
286
	 *
287
	 * @since 0.1
288
	 *
289
	 * @return self
290
	 */
291
	public function copy() {
292
		return clone $this;
293
	}
294
295
	/**
296
	 * @see http://php.net/manual/en/language.oop5.cloning.php
297
	 *
298
	 * @since 5.1
299
	 */
300
	public function __clone() {
301
		$this->fingerprint = clone $this->fingerprint;
302
		$this->statements = clone $this->statements;
303
	}
304
305
	/**
306
	 * @since 7.5
307
	 */
308
	public function clear() {
309
		$this->fingerprint = new Fingerprint();
310
		$this->statements = new StatementList();
311
	}
312
313
}
314