Passed
Push — master ( c7b7b3...d0017a )
by Daniel
07:28
created

AliasGroupList::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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