AliasGroupList::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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