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