-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathTermList.php
More file actions
199 lines (173 loc) · 3.73 KB
/
TermList.php
File metadata and controls
199 lines (173 loc) · 3.73 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
<?php
namespace Wikibase\DataModel\Term;
use ArrayIterator;
use Comparable;
use Countable;
use InvalidArgumentException;
use IteratorAggregate;
use OutOfBoundsException;
use Traversable;
/**
* Unordered list of Term objects.
* If multiple terms with the same language code are provided, only the last one will be retained.
* Empty terms are skipped and treated as non-existing.
*
* @since 0.7.3
*
* @license GPL-2.0+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
class TermList implements Countable, IteratorAggregate, Comparable {
/**
* @var Term[]
*/
private $terms = [];
/**
* @param Term[] $terms
* @throws InvalidArgumentException
*/
public function __construct( array $terms = [] ) {
foreach ( $terms as $term ) {
if ( !( $term instanceof Term ) ) {
throw new InvalidArgumentException( 'Every element in $terms must be an instance of Term' );
}
$this->setTerm( $term );
}
}
/**
* @see Countable::count
* @return int
*/
public function count() {
return count( $this->terms );
}
/**
* Returns an array with language codes as keys and the term text as values.
*
* @return string[]
*/
public function toTextArray() {
$array = [];
foreach ( $this->terms as $term ) {
$array[$term->getLanguageCode()] = $term->getText();
}
return $array;
}
/**
* @see IteratorAggregate::getIterator
* @return Traversable|Term[]
*/
public function getIterator() {
return new ArrayIterator( $this->terms );
}
/**
* @param string $languageCode
*
* @return Term
* @throws OutOfBoundsException
*/
public function getByLanguage( $languageCode ) {
if ( !array_key_exists( $languageCode, $this->terms ) ) {
throw new OutOfBoundsException( 'Term with languageCode "' . $languageCode . '" not found' );
}
return $this->terms[$languageCode];
}
/**
* @since 2.5
*
* @param string[] $languageCodes
*
* @return self
*/
public function getWithLanguages( array $languageCodes ) {
return new self( array_intersect_key( $this->terms, array_flip( $languageCodes ) ) );
}
/**
* @param string $languageCode
*/
public function removeByLanguage( $languageCode ) {
unset( $this->terms[$languageCode] );
}
/**
* @param string $languageCode
*
* @return bool
*/
public function hasTermForLanguage( $languageCode ) {
return array_key_exists( $languageCode, $this->terms );
}
/**
* Replaces non-empty or removes empty terms.
*
* @param Term $term
*/
public function setTerm( Term $term ) {
if ( $term->getText() === '' ) {
unset( $this->terms[$term->getLanguageCode()] );
}
else {
$this->terms[$term->getLanguageCode()] = $term;
}
}
/**
* @since 0.8
*
* @param string $languageCode
* @param string $termText
*/
public function setTextForLanguage( $languageCode, $termText ) {
$this->setTerm( new Term( $languageCode, $termText ) );
}
/**
* @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->terms as $term ) {
if ( !$target->hasTerm( $term ) ) {
return false;
}
}
return true;
}
/**
* @since 2.4.0
*
* @return bool
*/
public function isEmpty() {
return empty( $this->terms );
}
/**
* @since 0.7.4
*
* @param Term $term
*
* @return boolean
*/
public function hasTerm( Term $term ) {
return array_key_exists( $term->getLanguageCode(), $this->terms )
&& $this->terms[$term->getLanguageCode()]->equals( $term );
}
/**
* Removes all terms from this list.
*
* @since 6.0
*/
public function clear() {
$this->terms = [];
}
}