Completed
Push — master ( c8b4d1...73e2db )
by adam
06:49
created

Property::setStatements()   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 0
Metric Value
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
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 {
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
	 * @since 0.5, can be null since 1.0
85 4
	 *
86 4
	 * @param PropertyId|null $id
87 1
	 *
88 4
	 * @throws InvalidArgumentException
89 3
	 */
90 3
	public function setId( $id ) {
91
		if ( !( $id instanceof PropertyId ) && $id !== null ) {
92
			throw new InvalidArgumentException( '$id must be a PropertyId or null' );
93
		}
94 4
95
		$this->id = $id;
96
	}
97
98
	/**
99
	 * @since 0.7.3
100
	 *
101 58
	 * @return Fingerprint
102 58
	 */
103
	public function getFingerprint() {
104
		return $this->fingerprint;
105
	}
106
107
	/**
108
	 * @since 0.7.3
109
	 *
110 3
	 * @param Fingerprint $fingerprint
111 3
	 */
112 3
	public function setFingerprint( Fingerprint $fingerprint ) {
113
		$this->fingerprint = $fingerprint;
114
	}
115
116
	/**
117
	 * @see LabelsProvider::getLabels
118
	 *
119
	 * @since 6.0
120 12
	 *
121 12
	 * @return TermList
122 12
	 */
123
	public function getLabels() {
124
		return $this->fingerprint->getLabels();
125
	}
126
127
	/**
128
	 * @see DescriptionsProvider::getDescriptions
129
	 *
130 11
	 * @since 6.0
131 11
	 *
132 11
	 * @return TermList
133
	 */
134
	public function getDescriptions() {
135
		return $this->fingerprint->getDescriptions();
136
	}
137
138
	/**
139
	 * @see AliasesProvider::getAliasGroups
140 31
	 *
141 31
	 * @since 6.0
142 31
	 *
143
	 * @return AliasGroupList
144
	 */
145
	public function getAliasGroups() {
146
		return $this->fingerprint->getAliasGroups();
147
	}
148
149
	/**
150
	 * @param string $languageCode
151 69
	 * @param string $value
152 69
	 *
153 1
	 * @throws InvalidArgumentException
154
	 */
155
	public function setLabel( $languageCode, $value ) {
156 68
		$this->fingerprint->setLabel( $languageCode, $value );
157 68
	}
158
159
	/**
160
	 * @param string $languageCode
161
	 * @param string $value
162
	 *
163
	 * @throws InvalidArgumentException
164 4
	 */
165 4
	public function setDescription( $languageCode, $value ) {
166
		$this->fingerprint->setDescription( $languageCode, $value );
167
	}
168
169
	/**
170
	 * @param string $languageCode
171
	 * @param string[] $aliases
172
	 *
173
	 * @throws InvalidArgumentException
174
	 */
175
	public function setAliases( $languageCode, array $aliases ) {
176
		$this->fingerprint->setAliasGroup( $languageCode, $aliases );
177
	}
178
179
	/**
180
	 * @since 0.4
181
	 *
182
	 * @param string $dataTypeId The data type of the property. Not to be confused with the data
183
	 *  value type.
184
	 *
185
	 * @throws InvalidArgumentException
186 66
	 */
187 66
	public function setDataTypeId( $dataTypeId ) {
188
		if ( !is_string( $dataTypeId ) ) {
189
			throw new InvalidArgumentException( '$dataTypeId must be a string' );
190
		}
191
192
		$this->dataTypeId = $dataTypeId;
193
	}
194
195
	/**
196
	 * @since 0.4
197
	 *
198
	 * @return string Returns the data type of the property (property type). Not to be confused with
199
	 *  the data value type.
200
	 */
201
	public function getDataTypeId() {
202
		return $this->dataTypeId;
203
	}
204 18
205 18
	/**
206
	 * @see Entity::getType
207
	 *
208
	 * @since 0.1
209
	 *
210 18
	 * @return string Returns the entity type "property".
211 18
	 */
212 18
	public function getType() {
213
		return self::ENTITY_TYPE;
214
	}
215
216
	/**
217
	 * @since 0.3
218
	 *
219
	 * @param string $dataTypeId The data type of the property. Not to be confused with the data
220
	 *  value type.
221
	 *
222
	 * @return self
223 4
	 */
224 4
	public static function newFromType( $dataTypeId ) {
225 4
		return new self( null, null, $dataTypeId );
226
	}
227
228
	/**
229
	 * @see EntityDocument::equals
230
	 *
231
	 * @since 0.1
232
	 *
233
	 * @param mixed $target
234 1
	 *
235 1
	 * @return bool
236 1
	 */
237
	public function equals( $target ) {
238
		if ( $this === $target ) {
239
			return true;
240
		}
241
242
		return $target instanceof self
243 5
			&& $this->dataTypeId === $target->dataTypeId
244 5
			&& $this->fingerprint->equals( $target->fingerprint )
245
			&& $this->statements->equals( $target->statements );
246
	}
247
248
	/**
249
	 * Returns if the Property has no content.
250
	 * Having an id and type set does not count as having content.
251
	 *
252 2
	 * @since 0.1
253 2
	 *
254 2
	 * @return bool
255
	 */
256
	public function isEmpty() {
257
		return $this->fingerprint->isEmpty()
258
			&& $this->statements->isEmpty();
259
	}
260
261 4
	/**
262 4
	 * @since 1.1
263
	 *
264
	 * @return StatementList
265
	 */
266
	public function getStatements() {
267
		return $this->statements;
268
	}
269
270 1
	/**
271 1
	 * @since 1.1
272 1
	 *
273
	 * @param StatementList $statements
274
	 */
275
	public function setStatements( StatementList $statements ) {
276
		$this->statements = $statements;
277
	}
278
279
	/**
280
	 * @see EntityDocument::copy
281
	 *
282
	 * @since 0.1
283
	 *
284
	 * @return self
285
	 */
286
	public function copy() {
287
		return clone $this;
288
	}
289
290
	/**
291
	 * @see http://php.net/manual/en/language.oop5.cloning.php
292
	 *
293
	 * @since 5.1
294
	 */
295
	public function __clone() {
296
		$this->fingerprint = clone $this->fingerprint;
297
		$this->statements = clone $this->statements;
298
	}
299
300
	/**
301
	 * @since 7.4
302
	 */
303
	public function clear() {
304
		$this->fingerprint = new Fingerprint();
305
		$this->statements = new StatementList();
306
	}
307
308
}
309