Completed
Push — entity-clear ( afee34...e01dde )
by Bene
03:31
created

Property::equals()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 8.8571
cc 5
eloc 7
nc 5
nop 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...
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 Property
187
	 */
188
	public static function newFromType( $dataTypeId ) {
189
		return new self( null, null, $dataTypeId );
190
	}
191
192
	/**
193
	 * @see Comparable::equals
194
	 *
195
	 * Two properties are considered equal if they are of the same
196
	 * type and have the same value. The value does not include
197
	 * the id, so entities with the same value but different id
198
	 * are considered equal.
199
	 *
200
	 * @since 0.1
201
	 *
202
	 * @param mixed $target
203
	 *
204
	 * @return bool
205
	 */
206
	public function equals( $target ) {
207
		if ( $this === $target ) {
208
			return true;
209
		}
210
211
		return $target instanceof self
212
			&& $this->dataTypeId === $target->dataTypeId
213
			&& $this->fingerprint->equals( $target->fingerprint )
214
			&& $this->statements->equals( $target->statements );
215
	}
216
217
	/**
218
	 * Returns if the Property has no content.
219
	 * Having an id and type set does not count as having content.
220
	 *
221
	 * @since 0.1
222
	 *
223
	 * @return bool
224
	 */
225
	public function isEmpty() {
226
		return $this->fingerprint->isEmpty()
227
			&& $this->statements->isEmpty();
228
	}
229
230
	/**
231
	 * @since 1.1
232
	 *
233
	 * @return StatementList
234
	 */
235
	public function getStatements() {
236
		return $this->statements;
237
	}
238
239
	/**
240
	 * @since 1.1
241
	 *
242
	 * @param StatementList $statements
243
	 */
244
	public function setStatements( StatementList $statements ) {
245
		$this->statements = $statements;
246
	}
247
248
}
249