Passed
Push — langAssertions ( 0e6369...680829 )
by no
27:39 queued 24:23
created

Fingerprint   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 275
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 32
c 1
b 0
f 1
lcom 1
cbo 4
dl 0
loc 275
rs 9.6

23 Methods

Rating   Name   Duplication   Size   Complexity  
A newEmpty() 0 3 1
A __construct() 0 9 4
A getLabels() 0 3 1
A hasLabel() 0 3 1
A getLabel() 0 3 1
A setLabel() 0 3 1
A removeLabel() 0 3 1
A getDescriptions() 0 3 1
A hasDescription() 0 3 1
A getDescription() 0 3 1
A setDescription() 0 3 1
A removeDescription() 0 3 1
A getAliasGroups() 0 3 1
A hasAliasGroup() 0 3 1
A getAliasGroup() 0 3 1
A setAliasGroup() 0 3 1
A removeAliasGroup() 0 3 1
A setLabels() 0 3 1
A setDescriptions() 0 3 1
A setAliasGroups() 0 3 1
B equals() 0 10 5
A isEmpty() 0 5 3
A __clone() 0 7 1
1
<?php
2
3
namespace Wikibase\DataModel\Term;
4
5
use Comparable;
6
use InvalidArgumentException;
7
use OutOfBoundsException;
8
9
/**
10
 * A container for all labels, all descriptions and all aliases (in all languages) of entities that
11
 * support all three term types. Should not be used for entity types that only support one or two of
12
 * these term types.
13
 *
14
 * @since 0.7.3
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 * @author Thiemo Mättig
19
 */
20
class Fingerprint implements Comparable, LabelsProvider, DescriptionsProvider, AliasesProvider {
21
22
	/**
23
	 * @deprecated since 2.5, use new Fingerprint() instead.
24
	 *
25
	 * @return self
26
	 */
27
	public static function newEmpty() {
28
		return new self();
29
	}
30
31
	/**
32
	 * @var TermList
33
	 */
34
	private $labels;
35
36
	/**
37
	 * @var TermList
38
	 */
39
	private $descriptions;
40
41
	/**
42
	 * @var AliasGroupList
43
	 */
44
	private $aliasGroups;
45
46
	/**
47
	 * @param TermList|null $labels
48
	 * @param TermList|null $descriptions
49
	 * @param AliasGroupList|null $aliasGroups
50
	 */
51
	public function __construct(
52
		TermList $labels = null,
53
		TermList $descriptions = null,
54
		AliasGroupList $aliasGroups = null
55
	) {
56
		$this->labels = $labels ?: new TermList();
57
		$this->descriptions = $descriptions ?: new TermList();
58
		$this->aliasGroups = $aliasGroups ?: new AliasGroupList();
59
	}
60
61
	/**
62
	 * @since 0.7.3
63
	 *
64
	 * @return TermList
65
	 */
66
	public function getLabels() {
67
		return $this->labels;
68
	}
69
70
	/**
71
	 * @since 0.9
72
	 *
73
	 * @param string $languageCode
74
	 *
75
	 * @return boolean
76
	 */
77
	public function hasLabel( $languageCode ) {
78
		return $this->labels->hasTermForLanguage( $languageCode );
79
	}
80
81
	/**
82
	 * @since 0.7.4
83
	 *
84
	 * @param string $languageCode
85
	 *
86
	 * @return Term
87
	 * @throws OutOfBoundsException
88
	 * @throws InvalidArgumentException
89
	 */
90
	public function getLabel( $languageCode ) {
91
		return $this->labels->getByLanguage( $languageCode );
92
	}
93
94
	/**
95
	 * @since 1.0
96
	 *
97
	 * @param string $languageCode
98
	 * @param string $labelText
99
	 *
100
	 * @throws InvalidArgumentException
101
	 */
102
	public function setLabel( $languageCode, $labelText ) {
103
		$this->labels->setTerm( new Term( $languageCode, $labelText ) );
104
	}
105
106
	/**
107
	 * @since 0.7.4
108
	 *
109
	 * @param string $languageCode
110
	 */
111
	public function removeLabel( $languageCode ) {
112
		$this->labels->removeByLanguage( $languageCode );
113
	}
114
115
	/**
116
	 * @since 0.7.3
117
	 *
118
	 * @return TermList
119
	 */
120
	public function getDescriptions() {
121
		return $this->descriptions;
122
	}
123
124
	/**
125
	 * @since 0.9
126
	 *
127
	 * @param string $languageCode
128
	 *
129
	 * @return boolean
130
	 */
131
	public function hasDescription( $languageCode ) {
132
		return $this->descriptions->hasTermForLanguage( $languageCode );
133
	}
134
135
	/**
136
	 * @since 0.7.4
137
	 *
138
	 * @param string $languageCode
139
	 *
140
	 * @return Term
141
	 * @throws OutOfBoundsException
142
	 * @throws InvalidArgumentException
143
	 */
144
	public function getDescription( $languageCode ) {
145
		return $this->descriptions->getByLanguage( $languageCode );
146
	}
147
148
	/**
149
	 * @since 1.0
150
	 *
151
	 * @param string $languageCode
152
	 * @param string $descriptionText
153
	 *
154
	 * @throws InvalidArgumentException
155
	 */
156
	public function setDescription( $languageCode, $descriptionText ) {
157
		$this->descriptions->setTerm( new Term( $languageCode, $descriptionText ) );
158
	}
159
160
	/**
161
	 * @since 0.7.4
162
	 *
163
	 * @param string $languageCode
164
	 */
165
	public function removeDescription( $languageCode ) {
166
		$this->descriptions->removeByLanguage( $languageCode );
167
	}
168
169
	/**
170
	 * @since 0.7.4
171
	 *
172
	 * @return AliasGroupList
173
	 */
174
	public function getAliasGroups() {
175
		return $this->aliasGroups;
176
	}
177
178
	/**
179
	 * @since 0.9
180
	 *
181
	 * @param string $languageCode
182
	 *
183
	 * @return boolean
184
	 */
185
	public function hasAliasGroup( $languageCode ) {
186
		return $this->aliasGroups->hasGroupForLanguage( $languageCode );
187
	}
188
189
	/**
190
	 * @since 0.7.4
191
	 *
192
	 * @param string $languageCode
193
	 *
194
	 * @return AliasGroup
195
	 * @throws OutOfBoundsException
196
	 * @throws InvalidArgumentException
197
	 */
198
	public function getAliasGroup( $languageCode ) {
199
		return $this->aliasGroups->getByLanguage( $languageCode );
200
	}
201
202
	/**
203
	 * @since 1.0
204
	 *
205
	 * @param string $languageCode
206
	 * @param string[] $aliases
207
	 *
208
	 * @throws InvalidArgumentException
209
	 */
210
	public function setAliasGroup( $languageCode, array $aliases ) {
211
		$this->aliasGroups->setGroup( new AliasGroup( $languageCode, $aliases ) );
212
	}
213
214
	/**
215
	 * @since 0.7.4
216
	 *
217
	 * @param string $languageCode
218
	 */
219
	public function removeAliasGroup( $languageCode ) {
220
		$this->aliasGroups->removeByLanguage( $languageCode );
221
	}
222
223
	/**
224
	 * @see Comparable::equals
225
	 *
226
	 * @since 0.7.4
227
	 *
228
	 * @param mixed $target
229
	 *
230
	 * @return bool
231
	 */
232
	public function equals( $target ) {
233
		if ( $this === $target ) {
234
			return true;
235
		}
236
237
		return $target instanceof self
238
			&& $this->descriptions->equals( $target->getDescriptions() )
239
			&& $this->labels->equals( $target->getLabels() )
240
			&& $this->aliasGroups->equals( $target->getAliasGroups() );
241
	}
242
243
	/**
244
	 * @since 0.7.4
245
	 *
246
	 * @return bool
247
	 */
248
	public function isEmpty() {
249
		return $this->labels->isEmpty()
250
			&& $this->descriptions->isEmpty()
251
			&& $this->aliasGroups->isEmpty();
252
	}
253
254
	/**
255
	 * @since 0.7.4
256
	 *
257
	 * @param TermList $labels
258
	 */
259
	public function setLabels( TermList $labels ) {
260
		$this->labels = $labels;
261
	}
262
263
	/**
264
	 * @since 0.7.4
265
	 *
266
	 * @param TermList $descriptions
267
	 */
268
	public function setDescriptions( TermList $descriptions ) {
269
		$this->descriptions = $descriptions;
270
	}
271
272
	/**
273
	 * @since 0.7.4
274
	 *
275
	 * @param AliasGroupList $groups
276
	 */
277
	public function setAliasGroups( AliasGroupList $groups ) {
278
		$this->aliasGroups = $groups;
279
	}
280
281
	/**
282
	 * @see http://php.net/manual/en/language.oop5.cloning.php
283
	 *
284
	 * @since 5.1
285
	 */
286
	public function __clone() {
287
		// TermList is mutable, but Term is not. No deeper cloning necesarry.
288
		$this->labels = clone $this->labels;
289
		$this->descriptions = clone $this->descriptions;
290
		// AliasGroupList is mutable, but AliasGroup is not. No deeper cloning necesarry.
291
		$this->aliasGroups = clone $this->aliasGroups;
292
	}
293
294
}
295