Passed
Push — clear ( 11b8fa )
by Bene
03:57
created

AliasGroupList::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Term;
4
5
use ArrayIterator;
6
use Countable;
7
use InvalidArgumentException;
8
use IteratorAggregate;
9
use OutOfBoundsException;
10
use Traversable;
11
12
/**
13
 * Unordered list of AliasGroup objects.
14
 * Only one group per language code. If multiple groups with the same language code
15
 * are provided, only the last one will be retained.
16
 *
17
 * Empty groups are not stored.
18
 *
19
 * @since 0.7.3
20
 *
21
 * @licence GNU GPL v2+
22
 * @author Jeroen De Dauw < [email protected] >
23
 */
24
class AliasGroupList implements Countable, IteratorAggregate {
25
26
	/**
27
	 * @var AliasGroup[]
28
	 */
29
	private $groups = array();
30
31
	/**
32
	 * @param AliasGroup[] $aliasGroups
33
	 * @throws InvalidArgumentException
34
	 */
35
	public function __construct( array $aliasGroups = array() ) {
36
		foreach ( $aliasGroups as $aliasGroup ) {
37
			if ( !( $aliasGroup instanceof AliasGroup ) ) {
38
				throw new InvalidArgumentException( 'Every element in $aliasGroups must be an instance of AliasGroup' );
39
			}
40
41
			$this->setGroup( $aliasGroup );
42
		}
43
	}
44
45
	/**
46
	 * @see Countable::count
47
	 * @return int
48
	 */
49
	public function count() {
50
		return count( $this->groups );
51
	}
52
53
	/**
54
	 * @see IteratorAggregate::getIterator
55
	 * @return Traversable|AliasGroup[]
56
	 */
57
	public function getIterator() {
58
		return new ArrayIterator( $this->groups );
59
	}
60
61
	/**
62
	 * The array keys are the language codes of their associated AliasGroup.
63
	 *
64
	 * @since 2.3
65
	 *
66
	 * @return AliasGroup[] Array indexed by language code.
67
	 */
68
	public function toArray() {
69
		return $this->groups;
70
	}
71
72
	/**
73
	 * @param string $languageCode
74
	 *
75
	 * @return AliasGroup
76
	 * @throws InvalidArgumentException
77
	 * @throws OutOfBoundsException
78
	 */
79
	public function getByLanguage( $languageCode ) {
80
		$this->assertIsLanguageCode( $languageCode );
81
82
		if ( !array_key_exists( $languageCode, $this->groups ) ) {
83
			throw new OutOfBoundsException( 'AliasGroup with languageCode "' . $languageCode . '" not found' );
84
		}
85
86
		return $this->groups[$languageCode];
87
	}
88
89
	/**
90
	 * @since 2.5
91
	 *
92
	 * @param string[] $languageCodes
93
	 *
94
	 * @return self
95
	 */
96
	public function getWithLanguages( array $languageCodes ) {
97
		return new self( array_intersect_key( $this->groups, array_flip( $languageCodes ) ) );
98
	}
99
100
	/**
101
	 * @param string $languageCode
102
	 * @throws InvalidArgumentException
103
	 */
104
	public function removeByLanguage( $languageCode ) {
105
		$this->assertIsLanguageCode( $languageCode );
106
		unset( $this->groups[$languageCode] );
107
	}
108
109
	private function assertIsLanguageCode( $languageCode ) {
110
		if ( !is_string( $languageCode ) || $languageCode === '' ) {
111
			throw new InvalidArgumentException( '$languageCode must be a non-empty string' );
112
		}
113
	}
114
115
	/**
116
	 * If the group is empty, it will not be stored.
117
	 * In case the language of that group had an associated group, that group will be removed.
118
	 *
119
	 * @param AliasGroup $group
120
	 */
121
	public function setGroup( AliasGroup $group ) {
122
		if ( $group->isEmpty() ) {
123
			unset( $this->groups[$group->getLanguageCode()] );
124
		}
125
		else {
126
			$this->groups[$group->getLanguageCode()] = $group;
127
		}
128
	}
129
130
	/**
131
	 * @see Comparable::equals
132
	 *
133
	 * @since 0.7.4
134
	 *
135
	 * @param mixed $target
136
	 *
137
	 * @return bool
138
	 */
139
	public function equals( $target ) {
140
		if ( $this === $target ) {
141
			return true;
142
		}
143
144
		if ( !( $target instanceof self )
145
			|| $this->count() !== $target->count()
146
		) {
147
			return false;
148
		}
149
150
		foreach ( $this->groups as $group ) {
151
			if ( !$target->hasAliasGroup( $group ) ) {
152
				return false;
153
			}
154
		}
155
156
		return true;
157
	}
158
159
	/**
160
	 * @since 2.4.0
161
	 *
162
	 * @return bool
163
	 */
164
	public function isEmpty() {
165
		return empty( $this->groups );
166
	}
167
168
	/**
169
	 * @since 0.7.4
170
	 *
171
	 * @param AliasGroup $group
172
	 *
173
	 * @return boolean
174
	 */
175
	public function hasAliasGroup( AliasGroup $group ) {
176
		return array_key_exists( $group->getLanguageCode(), $this->groups )
177
			&& $this->groups[$group->getLanguageCode()]->equals( $group );
178
	}
179
180
	/**
181
	 * @since 0.8
182
	 *
183
	 * @param string $languageCode
184
	 *
185
	 * @return boolean
186
	 */
187
	public function hasGroupForLanguage( $languageCode ) {
188
		$this->assertIsLanguageCode( $languageCode );
189
		return array_key_exists( $languageCode, $this->groups );
190
	}
191
192
	/**
193
	 * @since 0.8
194
	 *
195
	 * @param string $languageCode
196
	 * @param string[] $aliases
197
	 */
198
	public function setAliasesForLanguage( $languageCode, array $aliases ) {
199
		$this->setGroup( new AliasGroup( $languageCode, $aliases ) );
200
	}
201
202
	/**
203
	 * Returns an array with language codes as keys the aliases as array values.
204
	 *
205
	 * @since 2.5
206
	 *
207
	 * @return array[]
208
	 */
209
	public function toTextArray() {
210
		$array = array();
211
212
		foreach ( $this->groups as $group ) {
213
			$array[$group->getLanguageCode()] = $group->getAliases();
214
		}
215
216
		return $array;
217
	}
218
219
	/**
220
	 * Removes all alias groups from this list.
221
	 *
222
	 * @since 5.1
223
	 */
224
	public function clear() {
225
		$this->groups = array();
226
	}
227
228
}
229