Passed
Push — entity-clear ( 938e18...1a048b )
by Bene
07:28 queued 04:00
created

Property::isEmpty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 2
eloc 3
nc 2
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\Fingerprint;
9
10
/**
11
 * Represents a single Wikibase property.
12
 * See https://www.mediawiki.org/wiki/Wikibase/DataModel#Properties
13
 *
14
 * @since 0.1
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class Property 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...
Deprecated Code introduced by
The interface Wikibase\DataModel\Statement\StatementListHolder has been deprecated with message: since 5.1

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...
20
21
	const ENTITY_TYPE = 'property';
22
23
	/**
24
	 * @var PropertyId|null
25
	 */
26
	private $id;
27
28
	/**
29
	 * @var Fingerprint
30
	 */
31
	private $fingerprint;
32
33
	/**
34
	 * @var string The data type of the property.
35
	 */
36
	private $dataTypeId;
37
38
	/**
39
	 * @var StatementList
40
	 */
41
	private $statements;
42
43
	/**
44
	 * @since 1.0
45
	 *
46
	 * @param PropertyId|null $id
47
	 * @param Fingerprint|null $fingerprint
48
	 * @param string $dataTypeId The data type of the property. Not to be confused with the data
49
	 *  value type.
50
	 * @param StatementList|null $statements Since 1.1
51
	 */
52
	public function __construct(
53
		PropertyId $id = null,
54
		Fingerprint $fingerprint = null,
55
		$dataTypeId,
56
		StatementList $statements = null
57
	) {
58
		$this->id = $id;
59
		$this->fingerprint = $fingerprint ?: new Fingerprint();
60
		$this->setDataTypeId( $dataTypeId );
61
		$this->statements = $statements ?: new StatementList();
62
	}
63
64
	/**
65
	 * Returns the id of the entity or null if it does not have one.
66
	 *
67
	 * @since 0.1 return type changed in 0.3
68
	 *
69
	 * @return PropertyId|null
70
	 */
71
	public function getId() {
72
		return $this->id;
73
	}
74
75
	/**
76
	 * Can be integer since 0.1.
77
	 * Can be PropertyId since 0.5.
78
	 * Can be null since 1.0.
79
	 *
80
	 * @param PropertyId|int|null $id
81
	 *
82
	 * @throws InvalidArgumentException
83
	 */
84
	public function setId( $id ) {
85
		if ( $id === null || $id instanceof PropertyId ) {
86
			$this->id = $id;
87
		} elseif ( is_int( $id ) ) {
88
			$this->id = PropertyId::newFromNumber( $id );
89
		} else {
90
			throw new InvalidArgumentException( '$id must be an instance of PropertyId, an integer,'
91
				. ' or null' );
92
		}
93
	}
94
95
	/**
96
	 * @since 0.7.3
97
	 *
98
	 * @return Fingerprint
99
	 */
100
	public function getFingerprint() {
101
		return $this->fingerprint;
102
	}
103
104
	/**
105
	 * @since 0.7.3
106
	 *
107
	 * @param Fingerprint $fingerprint
108
	 */
109
	public function setFingerprint( Fingerprint $fingerprint ) {
110
		$this->fingerprint = $fingerprint;
111
	}
112
113
	/**
114
	 * @param string $languageCode
115
	 * @param string $value
116
	 *
117
	 * @throws InvalidArgumentException
118
	 */
119
	public function setLabel( $languageCode, $value ) {
120
		$this->fingerprint->setLabel( $languageCode, $value );
121
	}
122
123
	/**
124
	 * @param string $languageCode
125
	 * @param string $value
126
	 *
127
	 * @throws InvalidArgumentException
128
	 */
129
	public function setDescription( $languageCode, $value ) {
130
		$this->fingerprint->setDescription( $languageCode, $value );
131
	}
132
133
	/**
134
	 * @param string $languageCode
135
	 * @param string[] $aliases
136
	 *
137
	 * @throws InvalidArgumentException
138
	 */
139
	public function setAliases( $languageCode, array $aliases ) {
140
		$this->fingerprint->setAliasGroup( $languageCode, $aliases );
141
	}
142
143
	/**
144
	 * @since 0.4
145
	 *
146
	 * @param string $dataTypeId The data type of the property. Not to be confused with the data
147
	 *  value type.
148
	 *
149
	 * @throws InvalidArgumentException
150
	 */
151
	public function setDataTypeId( $dataTypeId ) {
152
		if ( !is_string( $dataTypeId ) ) {
153
			throw new InvalidArgumentException( '$dataTypeId must be a string' );
154
		}
155
156
		$this->dataTypeId = $dataTypeId;
157
	}
158
159
	/**
160
	 * @since 0.4
161
	 *
162
	 * @return string Returns the data type of the property (property type). Not to be confused with
163
	 *  the data value type.
164
	 */
165
	public function getDataTypeId() {
166
		return $this->dataTypeId;
167
	}
168
169
	/**
170
	 * @see Entity::getType
171
	 *
172
	 * @since 0.1
173
	 *
174
	 * @return string Returns the entity type "property".
175
	 */
176
	public function getType() {
177
		return self::ENTITY_TYPE;
178
	}
179
180
	/**
181
	 * @since 0.3
182
	 *
183
	 * @param string $dataTypeId The data type of the property. Not to be confused with the data
184
	 *  value type.
185
	 *
186
	 * @return self
187
	 */
188
	public static function newFromType( $dataTypeId ) {
189
		return new self( null, null, $dataTypeId );
190
	}
191
192
	/**
193
	 * @see EntityDocument::equals
194
	 *
195
	 * @since 0.1
196
	 *
197
	 * @param mixed $target
198
	 *
199
	 * @return bool
200
	 */
201
	public function equals( $target ) {
202
		if ( $this === $target ) {
203
			return true;
204
		}
205
206
		return $target instanceof self
207
			&& $this->dataTypeId === $target->dataTypeId
208
			&& $this->fingerprint->equals( $target->fingerprint )
209
			&& $this->statements->equals( $target->statements );
210
	}
211
212
	/**
213
	 * Returns if the Property has no content.
214
	 * Having an id and type set does not count as having content.
215
	 *
216
	 * @since 0.1
217
	 *
218
	 * @return bool
219
	 */
220
	public function isEmpty() {
221
		return $this->fingerprint->isEmpty()
222
			&& $this->statements->isEmpty();
223
	}
224
225
	/**
226
	 * @since 1.1
227
	 *
228
	 * @return StatementList
229
	 */
230
	public function getStatements() {
231
		return $this->statements;
232
	}
233
234
	/**
235
	 * @since 1.1
236
	 *
237
	 * @param StatementList $statements
238
	 */
239
	public function setStatements( StatementList $statements ) {
240
		$this->statements = $statements;
241
	}
242
243
	/**
244
	 * @see EntityDocument::copy
245
	 *
246
	 * @since 0.1
247
	 *
248
	 * @return self
249
	 */
250
	public function copy() {
251
		return clone $this;
252
	}
253
254
	/**
255
	 * @see http://php.net/manual/en/language.oop5.cloning.php
256
	 *
257
	 * @since 5.1
258
	 */
259
	public function __clone() {
260
		$this->fingerprint = clone $this->fingerprint;
261
		$this->statements = clone $this->statements;
262
	}
263
264
}
265