Fingerprint::newEmpty()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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