Completed
Pull Request — master (#631)
by Bene
57:56 queued 50:39
created

Property::copy()   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

Importance

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