Test Failed
Push — langAssertions ( ff6a2b )
by no
13:42 queued 09:16
created

AliasGroupList::assertIsLanguageCode()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 5
rs 9.4285
cc 3
eloc 3
nc 2
nop 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
 * @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 OutOfBoundsException
77
	 */
78
	public function getByLanguage( $languageCode ) {
79
		if ( !array_key_exists( $languageCode, $this->groups ) ) {
80
			throw new OutOfBoundsException( 'AliasGroup with languageCode "' . $languageCode . '" not found' );
81
		}
82
83
		return $this->groups[$languageCode];
84
	}
85
86
	/**
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
97
	/**
98
	 * @param string $languageCode
99
	 */
100
	public function removeByLanguage( $languageCode ) {
101
		unset( $this->groups[$languageCode] );
102
	}
103
104
	/**
105
	 * If the group is empty, it will not be stored.
106
	 * In case the language of that group had an associated group, that group will be removed.
107
	 *
108
	 * @param AliasGroup $group
109
	 */
110
	public function setGroup( AliasGroup $group ) {
111
		if ( $group->isEmpty() ) {
112
			unset( $this->groups[$group->getLanguageCode()] );
113
		}
114
		else {
115
			$this->groups[$group->getLanguageCode()] = $group;
116
		}
117
	}
118
119
	/**
120
	 * @see Comparable::equals
121
	 *
122
	 * @since 0.7.4
123
	 *
124
	 * @param mixed $target
125
	 *
126
	 * @return bool
127
	 */
128
	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
		foreach ( $this->groups as $group ) {
140
			if ( !$target->hasAliasGroup( $group ) ) {
141
				return false;
142
			}
143
		}
144
145
		return true;
146
	}
147
148
	/**
149
	 * @since 2.4.0
150
	 *
151
	 * @return bool
152
	 */
153
	public function isEmpty() {
154
		return empty( $this->groups );
155
	}
156
157
	/**
158
	 * @since 0.7.4
159
	 *
160
	 * @param AliasGroup $group
161
	 *
162
	 * @return boolean
163
	 */
164
	public function hasAliasGroup( AliasGroup $group ) {
165
		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
	 */
176
	public function hasGroupForLanguage( $languageCode ) {
177
		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
		$this->setGroup( new AliasGroup( $languageCode, $aliases ) );
188
	}
189
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
		$array = array();
199
200
		foreach ( $this->groups as $group ) {
201
			$array[$group->getLanguageCode()] = $group->getAliases();
202
		}
203
204
		return $array;
205
	}
206
207
}
208