Completed
Pull Request — master (#614)
by Bene
10:35 queued 07:28
created

Property::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 1
eloc 3
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\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 69
		PropertyId $id = null,
54
		Fingerprint $fingerprint = null,
55
		$dataTypeId,
56
		StatementList $statements = null
57
	) {
58
		$this->id = $id;
59 69
		$this->fingerprint = $fingerprint ?: new Fingerprint();
60 69
		$this->setDataTypeId( $dataTypeId );
61 69
		$this->statements = $statements ?: new StatementList();
62 68
	}
63 68
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 13
		return $this->id;
73 13
	}
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 4
		if ( $id === null || $id instanceof PropertyId ) {
86 4
			$this->id = $id;
87 1
		} elseif ( is_int( $id ) ) {
88 4
			$this->id = PropertyId::newFromNumber( $id );
89 3
		} else {
90 3
			throw new InvalidArgumentException( '$id must be an instance of PropertyId, an integer,'
91
				. ' or null' );
92
		}
93
	}
94 4
95
	/**
96
	 * @since 0.7.3
97
	 *
98
	 * @return Fingerprint
99
	 */
100
	public function getFingerprint() {
101 58
		return $this->fingerprint;
102 58
	}
103
104
	/**
105
	 * @since 0.7.3
106
	 *
107
	 * @param Fingerprint $fingerprint
108
	 */
109
	public function setFingerprint( Fingerprint $fingerprint ) {
110 3
		$this->fingerprint = $fingerprint;
111 3
	}
112 3
113
	/**
114
	 * @param string $languageCode
115
	 * @param string $value
116
	 *
117
	 * @throws InvalidArgumentException
118
	 */
119
	public function setLabel( $languageCode, $value ) {
120 12
		$this->fingerprint->setLabel( $languageCode, $value );
121 12
	}
122 12
123
	/**
124
	 * @param string $languageCode
125
	 * @param string $value
126
	 *
127
	 * @throws InvalidArgumentException
128
	 */
129
	public function setDescription( $languageCode, $value ) {
130 11
		$this->fingerprint->setDescription( $languageCode, $value );
131 11
	}
132 11
133
	/**
134
	 * @param string $languageCode
135
	 * @param string[] $aliases
136
	 *
137
	 * @throws InvalidArgumentException
138
	 */
139
	public function setAliases( $languageCode, array $aliases ) {
140 31
		$this->fingerprint->setAliasGroup( $languageCode, $aliases );
141 31
	}
142 31
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 69
	public function setDataTypeId( $dataTypeId ) {
152 69
		if ( !is_string( $dataTypeId ) ) {
153 1
			throw new InvalidArgumentException( '$dataTypeId must be a string' );
154
		}
155
156 68
		$this->dataTypeId = $dataTypeId;
157 68
	}
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 4
	 */
165 4
	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 66
	 * @return self
187 66
	 */
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 18
		}
205 18
206
		return $target instanceof self
207
			&& $this->dataTypeId === $target->dataTypeId
208
			&& $this->fingerprint->equals( $target->fingerprint )
209
			&& $this->statements->equals( $target->statements );
210 18
	}
211 18
212 18
	/**
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 4
	}
224 4
225 4
	/**
226
	 * @since 1.1
227
	 *
228
	 * @return StatementList
229
	 */
230
	public function getStatements() {
231
		return $this->statements;
232
	}
233
234 1
	/**
235 1
	 * @since 1.1
236 1
	 *
237
	 * @param StatementList $statements
238
	 */
239
	public function setStatements( StatementList $statements ) {
240
		$this->statements = $statements;
241
	}
242
243 5
	/**
244 5
	 * @see EntityDocument::copy
245
	 *
246
	 * @since 0.1
247
	 *
248
	 * @return self
249
	 */
250
	public function copy() {
251
		return clone $this;
252 2
	}
253 2
254 2
	/**
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 4
		$this->statements = clone $this->statements;
262 4
	}
263
264
}
265